From 30ff4ff17224e2f6c8e6daf10594bb37f2a997c2 Mon Sep 17 00:00:00 2001 From: zph Date: Fri, 8 Jan 2021 17:54:16 +0800 Subject: [PATCH] add VOA --- docs/traditional-media.md | 20 +++++++++++++++ lib/router.js | 3 +++ lib/routes/voa/index.js | 53 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 lib/routes/voa/index.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 94db156ae..65cf425e6 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -247,6 +247,26 @@ Solidot 提供的 feed: +## Voice of America (VOA) + +透過提取全文,以獲得更好的閱讀體驗 + + + +`语言` + +| 粵語 | 中文 | 藏語 | +| --------- | ------- | ------- | +| cantonese | chinese | tibetan | + +`频道` + +可於各語言官網聚合新聞處 (如 ) 獲取 + +例如 `https://www.voacantonese.com/api/zyrtyequty` 將對應 `/voa/cantonese/zyrtyequty` + + + ## Yahoo ### 新聞 diff --git a/lib/router.js b/lib/router.js index 5bb0f656c..723e7893e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3780,4 +3780,7 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog')); // Caixin Latest router.get('/caixin/latest', require('./routes/caixin/latest')); +// Voice of America +router.get('/voa/:language/:channel?', require('./routes/voa/index')); + module.exports = router; diff --git a/lib/routes/voa/index.js b/lib/routes/voa/index.js new file mode 100644 index 000000000..f113c6860 --- /dev/null +++ b/lib/routes/voa/index.js @@ -0,0 +1,53 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + let baseURL; + switch (ctx.params.language) { + case 'cantonese': + baseURL = 'https://www.voacantonese.com/'; + break; + case 'chinese': + baseURL = 'https://www.voachinese.com/'; + break; + case 'tibetan': + baseURL = 'https://www.voatibetan.com/'; + break; + } + + const response = await got.get(baseURL + 'api/' + ctx.params.channel || ''); + const $ = cheerio.load(response.data); + + const list = []; + $('item').each(function (_, e) { + const item = {}; + item.title = $(e).find('title').first().text(); + item.link = $(e).find('guid').first().text(); + item.pubDate = $(e).find('pubDate').first().text(); + item.description = $(e).find('description').first().text(); + list.push(item); + }); + + const result = await Promise.all( + list + .filter((item) => item.link) + .map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const content = await got.get(item.link); + const description = cheerio.load(content.data); + description('.wsw__embed').remove(); + + item.description = description('div#article-content div').first().html() ? description('div.cover-media') + description('div#article-content div').first().html() : item.description; + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('channel title').first().text(), + link: baseURL, + item: result, + }; +};