feat(route): add swissinfo (#8466)

This commit is contained in:
Ethan Shen 2021-11-27 17:13:41 +08:00 committed by GitHub
parent 4ba85efba2
commit 330ec7ad63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 0 deletions

View File

@ -486,6 +486,12 @@ Compared to the official one, this feed:
<RouteEn author="emdoe" example="/simonsfoundation/recommend" path="/simonsfoundation/recommend"/>
## swissinfo
### Category
<RouteEn author="nczitzk" example="/swissinfo/eng/latest-news" path="/swissinfo/:language?/:category?" :paramsDesc="['Language, eng by default', 'Category, Latest News by default']"/>
## Sixth Tone
### News

View File

@ -902,6 +902,12 @@ IPFS 网关有可能失效,那时候换成其他网关。
<Route author="emdoe" example="/simonsfoundation/recommend" path="/simonsfoundation/recommend"/>
## swissinfo
### 分类
<Route author="nczitzk" example="/swissinfo/chi/latest-news" path="/swissinfo/:language?/:category?" :paramsDesc="['语言,默认为 eng', '分类,默认为 Latest News']"/>
## Sixth Tone
### 最新文章

68
lib/v2/swissinfo/index.js Normal file
View File

@ -0,0 +1,68 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const language = ctx.params.language ?? 'eng';
const category = ctx.params.category ?? 'latest-news';
const rootUrl = 'https://www.swissinfo.ch';
const currentUrl = `${rootUrl}/${language}/${category}`;
const response = await got({
method: 'get',
url: currentUrl,
});
let $ = cheerio.load(response.data);
const title = $('title').text();
const fragmentResponse = await got({
method: 'get',
url: `${rootUrl}${$('main div[data-fragment-placeholder]').attr('data-fragment-placeholder')}`,
});
$ = cheerio.load(fragmentResponse.data);
let items = $('.si-teaser__link')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 20)
.toArray()
.map((item) => {
item = $(item);
return {
link: `${rootUrl}${item.attr('href')}`,
title: item.find('.si-teaser__title').text(),
pubDate: parseDate(item.find('.si-teaser__date').attr('datetime')),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
content('picture').each(function () {
content(this).html(`<img src="${content(this).find('source').first().attr('srcset')}">`);
});
item.description = content('.si-detail__content').html();
item.author = content('meta[name="author"]').attr('content');
return item;
})
)
);
ctx.state.data = {
title,
link: currentUrl,
item: items,
};
};

View File

@ -0,0 +1,3 @@
module.exports = {
'/:language?/:category?': ['nczitzk'],
};

13
lib/v2/swissinfo/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'swissinfo.ch': {
_name: 'swissinfo',
'.': [
{
title: 'Category',
docs: 'https://docs.rsshub.app/new-media.html#swissinfo-category',
source: ['/:language/:category', '/'],
target: '/swissinfo/:language?/:category?',
},
],
},
};

View File

@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:language?/:category?', require('./index'));
};