From 93e22bba30acccd9faa01493077a7a6ebf073481 Mon Sep 17 00:00:00 2001 From: BenYip Date: Wed, 19 Jul 2023 20:25:17 +0800 Subject: [PATCH] feat(route): add NPR (#12827) * feat(route): add NPR * fix(route/npr): docs * fix(route/npr): remove duplicate images * fix(route/npr): ignore item until audio is available * fix(route/npr): remove duplicate captions * fix(route/npr): caption * fix(route/npr): handle multiple audios --- docs/en/traditional-media.md | 12 +++++ docs/traditional-media.md | 10 ++++ lib/v2/npr/full.js | 89 ++++++++++++++++++++++++++++++++++++ lib/v2/npr/maintainer.js | 3 ++ lib/v2/npr/radar.js | 11 +++++ lib/v2/npr/router.js | 3 ++ 6 files changed, 128 insertions(+) create mode 100644 lib/v2/npr/full.js create mode 100644 lib/v2/npr/maintainer.js create mode 100644 lib/v2/npr/radar.js create mode 100644 lib/v2/npr/router.js diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 8c0b9e838..f5c01348c 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -784,3 +784,15 @@ Free articles only. | 皇室 | koushitsu | + +## NPR + +### News + + + +Provide full article RSS for CBC topics. + + + + diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 800d0e7e9..c3f361d4c 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -2715,3 +2715,13 @@ category 对应的关键词有 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | + +## NPR + +### 新闻 + + + +通过提取文章全文,以提供比官方源更佳的阅读体验。 + + diff --git a/lib/v2/npr/full.js b/lib/v2/npr/full.js new file mode 100644 index 000000000..faf84a4ef --- /dev/null +++ b/lib/v2/npr/full.js @@ -0,0 +1,89 @@ +const parser = require('@/utils/rss-parser'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const getArticleDetail = (link, ctx) => + ctx.cache.tryGet(link, async () => { + const response = await got(link); + const $ = cheerio.load(response.data); + + const categories = $('.slug').text().trim(); + + // ignore item until audio is available + if ($('.audio-availability-message').length > 0) { + return null; + } + + // handle multiple audios + $('.audio-module') + .toArray() + .forEach((x) => { + const audioLink = $($(x).find('.audio-tool-download a')).attr('href'); + const audio_tag = ``; + $(x).replaceWith(audio_tag); + }); + + // primaryaudio is not in `.storytext`, prepend to it + const primaryaudio = $('#primaryaudio'); + const audio = primaryaudio.length > 0 ? $('#primaryaudio').html() : ''; + + // replace video + const regex = /\?storyId=(\d+)&mediaId=(\d+)/; + const m = $.html().match(regex); + if (m) { + const storyId = m[1]; + const mediaId = m[2]; + const video_tag = ``; + const nprVideo = $('.npr-video'); + if (nprVideo.length === 1) { + nprVideo.replaceWith(video_tag); + } + } + + // remove duplicate caption and images + $('.enlarge_measure').remove(); + $('.enlarge_html').remove(); + $('.hide-caption').remove(); + $('.toggle-caption').remove(); + $('.credit-caption b.credit').remove(); + $('.credit-caption span.credit').wrap('
'); + $('.caption > p').wrap('
'); + + const article = + audio + + $('.storytext') + .toArray() + .map((el) => $(el).html()) + .join(''); + + return { article, categories }; + }); + +module.exports = async (ctx) => { + const endpoint = ctx.params.endpoint || '1001'; + const feed = await parser.parseURL(`https://feeds.npr.org/${endpoint}/rss.xml`); + + const items = ( + await Promise.all( + feed.items.map(async (item) => { + const itemDetails = await getArticleDetail(item.link, ctx); + if (itemDetails === null) { + return null; + } + return { + ...item, + description: itemDetails.article, + category: itemDetails.categories, + }; + }) + ) + ).filter(Boolean); + + ctx.state.data = { + title: feed.title, + link: feed.link, + description: feed.description, + icon: 'https://media.npr.org/images/podcasts/primary/npr_generic_image_300.jpg?s=200', + item: items, + }; +}; diff --git a/lib/v2/npr/maintainer.js b/lib/v2/npr/maintainer.js new file mode 100644 index 000000000..4d5d4a2ce --- /dev/null +++ b/lib/v2/npr/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:endpoint?': ['bennyyip'], +}; diff --git a/lib/v2/npr/radar.js b/lib/v2/npr/radar.js new file mode 100644 index 000000000..3f753077a --- /dev/null +++ b/lib/v2/npr/radar.js @@ -0,0 +1,11 @@ +module.exports = { + 'npr.org': { + _name: 'NPR', + '.': [ + { + title: '全文 RSS', + docs: 'https://docs.rsshub.app/traditional-media.html#npr', + }, + ], + }, +}; diff --git a/lib/v2/npr/router.js b/lib/v2/npr/router.js new file mode 100644 index 000000000..ba56db90b --- /dev/null +++ b/lib/v2/npr/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/:endpoint?', require('./full')); +};