diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 2796ac751..6703a34ae 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -486,6 +486,12 @@ Compared to the official one, this feed: +## swissinfo + +### Category + + + ## Sixth Tone ### News diff --git a/docs/new-media.md b/docs/new-media.md index d8dfc2ba1..7d9493155 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -902,6 +902,12 @@ IPFS 网关有可能失效,那时候换成其他网关。 +## swissinfo + +### 分类 + + + ## Sixth Tone ### 最新文章 diff --git a/lib/v2/swissinfo/index.js b/lib/v2/swissinfo/index.js new file mode 100644 index 000000000..ce9d748c9 --- /dev/null +++ b/lib/v2/swissinfo/index.js @@ -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(``); + }); + + 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, + }; +}; diff --git a/lib/v2/swissinfo/maintainer.js b/lib/v2/swissinfo/maintainer.js new file mode 100644 index 000000000..87f9717ce --- /dev/null +++ b/lib/v2/swissinfo/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:language?/:category?': ['nczitzk'], +}; diff --git a/lib/v2/swissinfo/radar.js b/lib/v2/swissinfo/radar.js new file mode 100644 index 000000000..44b0831d3 --- /dev/null +++ b/lib/v2/swissinfo/radar.js @@ -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?', + }, + ], + }, +}; diff --git a/lib/v2/swissinfo/router.js b/lib/v2/swissinfo/router.js new file mode 100644 index 000000000..59015c113 --- /dev/null +++ b/lib/v2/swissinfo/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:language?/:category?', require('./index')); +};