From 171ee3b00681a47f09b74715bb3bc82c011c063b Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 23 Feb 2022 21:55:13 +0800 Subject: [PATCH] fix(route): World Health Organization Newsroom (#8012) * fix(route): World Health Organization Newsroom * refactor: migrate to v2 Co-authored-by: TonyRL --- docs/en/government.md | 44 +++++++++++++++++ docs/en/new-media.md | 30 ------------ docs/government.md | 44 +++++++++++++++++ docs/new-media.md | 30 ------------ lib/router.js | 8 ++-- lib/routes/who/news-room.js | 56 ---------------------- lib/v2/who/maintainer.js | 5 ++ lib/v2/who/news-room.js | 76 ++++++++++++++++++++++++++++++ lib/{routes => v2}/who/news.js | 2 +- lib/v2/who/radar.js | 25 ++++++++++ lib/v2/who/router.js | 5 ++ lib/{routes => v2}/who/speeches.js | 0 12 files changed, 204 insertions(+), 121 deletions(-) delete mode 100644 lib/routes/who/news-room.js create mode 100644 lib/v2/who/maintainer.js create mode 100644 lib/v2/who/news-room.js rename lib/{routes => v2}/who/news.js (96%) create mode 100644 lib/v2/who/radar.js create mode 100644 lib/v2/who/router.js rename lib/{routes => v2}/who/speeches.js (100%) diff --git a/docs/en/government.md b/docs/en/government.md index 172348c62..ad7f12075 100644 --- a/docs/en/government.md +++ b/docs/en/government.md @@ -127,6 +127,50 @@ Category +## World Health Organization | WHO + +### News + + + +Language + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | ---- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + +### Newsroom + + + +Category + +| Feature stories | Commentaries | +| --------------- | ------------ | +| feature-stories | commentaries | + +Language + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | ---- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + +### Speeches + + + +Language + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | ---- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + ## World Trade Organization ### Dispute settlement news diff --git a/docs/en/new-media.md b/docs/en/new-media.md index 0812c7f78..0abb5ac16 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -647,33 +647,3 @@ Provides all of the Thrillist articles with the specified tag. ### Archive - -## World Health Organization | WHO - -### News - - - -Language - -| English | العربية | 中文 | Français | Русский | Español | Português | -| ------- | ------- | ---- | -------- | ------- | ------- | --------- | -| en | ar | zh | fr | ru | es | pt | - - - -### Newsroom - - - -### Speeches - - - -Language - -| English | العربية | 中文 | Français | Русский | Español | Português | -| ------- | ------- | ---- | -------- | ------- | ------- | --------- | -| en | ar | zh | fr | ru | es | pt | - - diff --git a/docs/government.md b/docs/government.md index 5d86e2883..bf95b5dbd 100644 --- a/docs/government.md +++ b/docs/government.md @@ -314,6 +314,50 @@ pageClass: routes +## 世界卫生组织 WHO + +### 新闻稿 + + + +语言 + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | -- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + +### 媒体中心 + + + +分类 + +| 特写故事 | 评论 | +| --------------- | ------------ | +| feature-stories | commentaries | + +语言 + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | -- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + +### 总干事的讲话 + + + +语言 + +| English | العربية | 中文 | Français | Русский | Español | Português | +| ------- | ------- | -- | -------- | ------- | ------- | --------- | +| en | ar | zh | fr | ru | es | pt | + + + ## 苏州市人民政府 ### 政府新闻 diff --git a/docs/new-media.md b/docs/new-media.md index 886e16d52..d91eca4e8 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2938,36 +2938,6 @@ column 为 third 时可选的 category: -## 世界卫生组织 WHO - -### 新闻稿 - - - -语言 - -| English | العربية | 中文 | Français | Русский | Español | Português | -| ------- | ------- | -- | -------- | ------- | ------- | --------- | -| en | ar | zh | fr | ru | es | pt | - - - -### 媒体中心 - - - -### 总干事的讲话 - - - -语言 - -| English | العربية | 中文 | Français | Русский | Español | Português | -| ------- | ------- | -- | -------- | ------- | ------- | --------- | -| en | ar | zh | fr | ru | es | pt | - - - ## 数英网 ### 数英网最新文章 diff --git a/lib/router.js b/lib/router.js index 792dadbf1..7b5f5bd44 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1615,10 +1615,10 @@ router.get('/nintendo/news/china', lazyloadRouteHandler('./routes/nintendo/news_ router.get('/nintendo/direct', lazyloadRouteHandler('./routes/nintendo/direct')); router.get('/nintendo/system-update', lazyloadRouteHandler('./routes/nintendo/system-update')); -// 世界卫生组织 -router.get('/who/news-room/:type', lazyloadRouteHandler('./routes/who/news-room')); -router.get('/who/speeches/:language?', lazyloadRouteHandler('./routes/who/speeches')); -router.get('/who/news/:language?', lazyloadRouteHandler('./routes/who/news')); +// 世界卫生组织 migrated to v2 +// router.get('/who/news-room/:category?/:language?', lazyloadRouteHandler('./routes/who/news-room')); +// router.get('/who/speeches/:language?', lazyloadRouteHandler('./routes/who/speeches')); +// router.get('/who/news/:language?', lazyloadRouteHandler('./routes/who/news')); // 福利资源-met.red router.get('/metred/fuli', lazyloadRouteHandler('./routes/metred/fuli')); diff --git a/lib/routes/who/news-room.js b/lib/routes/who/news-room.js deleted file mode 100644 index 9f32ded87..000000000 --- a/lib/routes/who/news-room.js +++ /dev/null @@ -1,56 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const url = require('url'); -const { parseDate } = require('@/utils/parse-date'); - -const host = 'https://www.who.int/'; - -module.exports = async (ctx) => { - const type = ctx.params.type; - - const link = url.resolve('https://www.who.int/zh/news-room/', type); - const response = await got.get(encodeURI(link)); - const $ = cheerio.load(response.data); - - const list = $('div.list-view--item'); - const title = $('ul.sf-breadscrumb.breadcrumb li.active').text(); - const out = await Promise.all( - list - .map(async (index, elem) => { - const $elem = $(elem); - const title = $elem.find('p').text(); - const link = url.resolve(host, $elem.find('a').attr('href')); - const pubDate = parseDate($elem.find('.timestamp').text(), 'YYYY年M月D日'); - - const item = { - title, - pubDate, - link: encodeURI(link), - }; - - const key = link; - const value = await ctx.cache.get(key); - if (value) { - item.description = value; - } else { - const detail = await got.get(item.link); - const data = detail.data; - const $ = cheerio.load(data); - - item.description = $('article') - .html() - .replace(/src="\//g, `src="${host}`); - ctx.cache.set(key, item.description); - } - - return item; - }) - .get() - ); - - ctx.state.data = { - title: `世界卫生组织媒体中心-${title}`, - link, - item: out, - }; -}; diff --git a/lib/v2/who/maintainer.js b/lib/v2/who/maintainer.js new file mode 100644 index 000000000..c83f5e34c --- /dev/null +++ b/lib/v2/who/maintainer.js @@ -0,0 +1,5 @@ +module.exports = { + '/news/:language?': ['nczitzk'], + '/news-room/:category?/:language?': ['LogicJake', 'nczitzk'], + '/speeches/:language?': ['nczitzk'], +}; diff --git a/lib/v2/who/news-room.js b/lib/v2/who/news-room.js new file mode 100644 index 000000000..c37c5ef10 --- /dev/null +++ b/lib/v2/who/news-room.js @@ -0,0 +1,76 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'feature-stories'; + const language = ctx.params.language ?? ''; + + const rootUrl = 'https://www.who.int'; + const currentUrl = `${rootUrl}/${language ? `${language}/` : ''}news-room/${category}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let list = $('.list-view--item a'); + + if (list.length === 0) { + const response = await got({ + method: 'get', + url: `${rootUrl}/api/hubs/${category.replace(/-/g, '')}?sf_culture=zh&$orderby=PublicationDateAndTime%20desc&$select=Title,PublicationDateAndTime,ItemDefaultUrl&$top=30`, + }); + + list = response.data.value.map((item) => ({ + title: item.Title, + link: `${currentUrl}/detail/${item.ItemDefaultUrl}`, + pubDate: parseDate(item.PublicationDateAndTime), + })); + } else { + list = list + .map((_, item) => { + item = $(item); + const link = item.attr('href'); + + return { + link: `${link.indexOf('http') === 0 ? '' : rootUrl}${item.attr('href')}`, + }; + }) + .get(); + } + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const matches = detailResponse.data.match(/"headline":"(.*)","description":"(.*)","datePublished":"(.*)","image"/); + + if (matches) { + item.title = matches[1]; + item.description = matches[2]; + item.pubDate = parseDate(matches[3]); + } else { + const content = cheerio.load(detailResponse.data); + + item.description = content('.sf-content-block').html(); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${$('meta[property="og:title"]').attr('content')} - WHO`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/routes/who/news.js b/lib/v2/who/news.js similarity index 96% rename from lib/routes/who/news.js rename to lib/v2/who/news.js index 1bc568986..0d1781106 100644 --- a/lib/routes/who/news.js +++ b/lib/v2/who/news.js @@ -2,7 +2,7 @@ const got = require('@/utils/got'); const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { - const language = ctx.params.language || 'en'; + const language = ctx.params.language ?? 'en'; const rootUrl = 'https://www.who.int'; const currentUrl = `${rootUrl}/${language === 'en' ? '' : `${language}/`}news`; diff --git a/lib/v2/who/radar.js b/lib/v2/who/radar.js new file mode 100644 index 000000000..7de1be696 --- /dev/null +++ b/lib/v2/who/radar.js @@ -0,0 +1,25 @@ +module.exports = { + 'who.int': { + _name: '世界卫生组织 WHO', + '.': [ + { + title: '新闻稿', + docs: 'https://docs.rsshub.app/government.html#shi-jie-wei-sheng-zu-zhi-who', + source: '/news', + target: '/who/news', + }, + { + title: '媒体中心', + docs: 'https://docs.rsshub.app/government.html#shi-jie-wei-sheng-zu-zhi-who', + source: '/news-room/:type', + target: '/who/news-room/:type', + }, + { + title: '总干事的讲话', + docs: 'https://docs.rsshub.app/government.html#shi-jie-wei-sheng-zu-zhi-who', + source: '/director-general/speeches', + target: '/who/speeches', + }, + ], + }, +}; diff --git a/lib/v2/who/router.js b/lib/v2/who/router.js new file mode 100644 index 000000000..5a620f7b6 --- /dev/null +++ b/lib/v2/who/router.js @@ -0,0 +1,5 @@ +module.exports = (router) => { + router.get('/news/:language?', require('./news')); + router.get('/news-room/:category?/:language?', require('./news-room')); + router.get('/speeches/:language?', require('./speeches')); +}; diff --git a/lib/routes/who/speeches.js b/lib/v2/who/speeches.js similarity index 100% rename from lib/routes/who/speeches.js rename to lib/v2/who/speeches.js