From 944f46f995c5880da91c5d9bef369f5286d2feb5 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 17:10:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E9=A6=99=E6=B8=AF?= =?UTF-8?q?=E8=A1=9B=E7=94=9F=E9=98=B2=E8=AD=B7=E4=B8=AD=E5=BF=83=20(#8569?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/en/government.md | 20 +++++++ docs/government.md | 20 +++++++ lib/v2/hongkong/chp.js | 103 ++++++++++++++++++++++++++++++++++ lib/v2/hongkong/maintainer.js | 3 + lib/v2/hongkong/radar.js | 13 +++++ lib/v2/hongkong/router.js | 3 + 6 files changed, 162 insertions(+) create mode 100644 lib/v2/hongkong/chp.js create mode 100644 lib/v2/hongkong/maintainer.js create mode 100644 lib/v2/hongkong/radar.js create mode 100644 lib/v2/hongkong/router.js diff --git a/docs/en/government.md b/docs/en/government.md index 970ded0d9..a57c22bf3 100644 --- a/docs/en/government.md +++ b/docs/en/government.md @@ -35,6 +35,26 @@ Category +## Hong Kong Centre for Health Protection + +### Category + + + +Category + +| Important Topics | Press Releases | Response Level | Periodicals & Publications | Health Notice | +| ---------------- | ---------------- | -------------- | -------------------------- | ------------- | +| important_ft | press_data_index | ResponseLevel | publication | HealthAlert | + +Language + +| English | 中文简体 | 中文繁體 | +| ------- | -------- | -------- | +| en | zh_cn | zh_tw | + + + ## Ministry of Foreign Affairs of Japan ### Press conference diff --git a/docs/government.md b/docs/government.md index b457394c1..b4024bac1 100644 --- a/docs/government.md +++ b/docs/government.md @@ -264,6 +264,26 @@ pageClass: routes +## 香港卫生防护中心 + +### 分类 + + + +分类 + +| 重要资讯 | 新闻稿 | 应变级别 | 期刊及刊物 | 健康通告 | +| ------------ | ---------------- | ------------- | ----------- | ----------- | +| important_ft | press_data_index | ResponseLevel | publication | HealthAlert | + +语言 + +| English | 中文简体 | 中文繁體 | +| ------- | -------- | -------- | +| en | zh_cn | zh_tw | + + + ## 中国工业和信息化部 ### 政策解读 diff --git a/lib/v2/hongkong/chp.js b/lib/v2/hongkong/chp.js new file mode 100644 index 000000000..b6dfa2e6b --- /dev/null +++ b/lib/v2/hongkong/chp.js @@ -0,0 +1,103 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +const titles = { + title: { + en: 'Hong Kong Centre for Health Protection', + zh_cn: '香港卫生防护中心', + zh_tw: '香港衛生防護中心', + }, + important_ft: { + en: 'Important Topics', + zh_cn: '重要资讯', + zh_tw: '重要資訊', + }, + press_data_index: { + en: 'Press Releases', + zh_cn: '新闻稿', + zh_tw: '新聞稿', + }, + ResponseLevel: { + en: 'Response Level', + zh_cn: '应变级别', + zh_tw: '應變級別', + }, + publication: { + en: 'Periodicals & Publications', + zh_cn: '期刊及刊物', + zh_tw: '期刊及刊物', + }, + HealthAlert: { + en: 'Health Notice', + zh_cn: '健康通告', + zh_tw: '健康通告', + }, +}; + +module.exports = async (ctx) => { + const languageCodes = { + en: 'en', + zh_cn: 'sc', + zh_tw: 'tc', + }; + + const category = ctx.params.category ?? 'important_ft'; + const language = ctx.params.language ?? 'zh_tw'; + + const rootUrl = 'https://www.chp.gov.hk'; + const apiUrl = `${rootUrl}/js/${category}.js`; + const currentUrl = `${rootUrl}/${languageCodes[language]}${category === 'press_data_index' ? '/media/116' : ''}/index.html`; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + + const list = JSON.parse(response.data.match(/"data":(\[\{.*\}\])\}/)[1]).map((item) => { + let link = ''; + + if (item.UrlPath_en) { + link = item[`UrlPath_${language}`].indexOf('http') < 0 ? `${rootUrl}${item[`UrlPath_${language}`]}` : item[`UrlPath_${language}`]; + } else if (category === 'ResponseLevel' && item.FilePath_en) { + link = item[`FilePath_${language}`].indexOf('http') < 0 ? `${rootUrl}${item[`FilePath_${language}`]}` : item[`FilePath_${language}`]; + } else { + link = `${rootUrl}/${languageCodes[language]}/${category === 'publication' ? 'guideline' : 'features'}/${item.InfoBlockID}.html`; + } + + return { + link, + pubDate: parseDate(item.PublishDate), + description: item[`Content_${language}`] ?? '', + title: item[`Title_${language}`]?.replace(/<.*>/, '') ?? '', + }; + }); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + if ((category === 'important_ft' || category === 'press_data_index') && (item.link.indexOf('htm') > 0 || item.link.indexOf('/features/') > 0)) { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('#btmNav, script').remove(); + content('.contHeader, .title_display_date').remove(); + content('.printBtn, .bookmarkBtn, .qrBtn, .qr-content').remove(); + + item.description = content('#mainContent, #pressrelease').html(); + } + return item; + }) + ) + ); + + ctx.state.data = { + title: `${titles[category][language]} - ${titles.title[language]}`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/hongkong/maintainer.js b/lib/v2/hongkong/maintainer.js new file mode 100644 index 000000000..ef57b91b7 --- /dev/null +++ b/lib/v2/hongkong/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/chp/:category?/:language?': ['nczitzk'], +}; diff --git a/lib/v2/hongkong/radar.js b/lib/v2/hongkong/radar.js new file mode 100644 index 000000000..f3bd08611 --- /dev/null +++ b/lib/v2/hongkong/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'chp.gov.hk': { + _name: '香港衛生防護中心', + '.': [ + { + title: '分类', + docs: 'https://docs.rsshub.app/government.html#xiang-gang-wei-sheng-fang-hu-zhong-xin-fen-lei', + source: ['/'], + target: '/hongkong/chp/:category?/:language?', + }, + ], + }, +}; diff --git a/lib/v2/hongkong/router.js b/lib/v2/hongkong/router.js new file mode 100644 index 000000000..e48bb3601 --- /dev/null +++ b/lib/v2/hongkong/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/chp/:category?/:language?', require('./chp')); +};