From a428c4c5cdf85d2a1b8afbf1fa839312dabfbb55 Mon Sep 17 00:00:00 2001 From: Mengxin Cao <50902043+mcao2@users.noreply.github.com> Date: Sun, 1 May 2022 06:27:20 -0700 Subject: [PATCH] fix(route): broken link embassy/us (#9673) * Fix broken link embassy/us * refactor: migrate to v2 * fix: embassy/us selector --- lib/router.js | 4 +- lib/routes/embassy/index.js | 64 ----- lib/v2/embassy/index.js | 60 +++++ lib/v2/embassy/maintainer.js | 3 + lib/v2/embassy/radar.js | 258 ++++++++++++++++++++ lib/v2/embassy/router.js | 3 + lib/{routes => v2}/embassy/supportedList.js | 10 +- 7 files changed, 331 insertions(+), 71 deletions(-) delete mode 100644 lib/routes/embassy/index.js create mode 100644 lib/v2/embassy/index.js create mode 100644 lib/v2/embassy/maintainer.js create mode 100644 lib/v2/embassy/radar.js create mode 100644 lib/v2/embassy/router.js rename lib/{routes => v2}/embassy/supportedList.js (98%) diff --git a/lib/router.js b/lib/router.js index 2c7ab3227..4ea4d0cce 100644 --- a/lib/router.js +++ b/lib/router.js @@ -836,8 +836,8 @@ router.get('/iplay/home', lazyloadRouteHandler('./routes/iplay/home')); // xclient.info router.get('/xclient/app/:name', lazyloadRouteHandler('./routes/xclient/app')); -// 中国驻外使领事馆 -router.get('/embassy/:country/:city?', lazyloadRouteHandler('./routes/embassy/index')); +// 中国驻外使领事馆 migrated to v2 +// router.get('/embassy/:country/:city?', lazyloadRouteHandler('./routes/embassy/index')); // 澎湃新闻 // router.get('/thepaper/featured', lazyloadRouteHandler('./routes/thepaper/featured')); diff --git a/lib/routes/embassy/index.js b/lib/routes/embassy/index.js deleted file mode 100644 index bf92499ee..000000000 --- a/lib/routes/embassy/index.js +++ /dev/null @@ -1,64 +0,0 @@ -const got = require('@/utils/got'); -const url = require('url'); -const cheerio = require('cheerio'); - -const supportedList = require('./supportedList'); - -module.exports = async (ctx) => { - const country = ctx.params.country; - const city = ctx.params.city || undefined; - - let config = supportedList[country.toLowerCase()]; - let desc; - - if (city === undefined) { - desc = `中国驻${config.countryCN}大使馆 -- 重要通知`; - } else { - config = config.consulates[city.toLowerCase()]; - desc = `中国驻${config.cityCN}领事馆 -- 重要通知`; - } - - const link = config.link; - const hostname = url.parse(link).hostname; - - const res = await got.get(link); - const $ = cheerio.load(res.data); - - const list = []; - - $(config.list) - .slice(0, 10) - .each((i, e) => { - const temp = url.resolve(link, $(e).attr('href')); - if (url.parse(temp).hostname === hostname) { - list.push(temp); - } - }); - - const out = await Promise.all( - list.map(async (link) => { - const cache = await ctx.cache.get(link); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } - - const response = await got.get(link); - const $ = cheerio.load(response.data); - const single = { - title: $(config.title).text(), - link, - description: $(config.description).html(), - pubDate: new Date($(config.pubDate).text().replace('(', '').replace(')', '')).toUTCString(), - }; - ctx.cache.set(link, JSON.stringify(single)); - return Promise.resolve(single); - }) - ); - - ctx.state.data = { - title: desc, - description: desc, - link, - item: out, - }; -}; diff --git a/lib/v2/embassy/index.js b/lib/v2/embassy/index.js new file mode 100644 index 000000000..7c88dce39 --- /dev/null +++ b/lib/v2/embassy/index.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +const supportedList = require('./supportedList'); + +module.exports = async (ctx) => { + const country = ctx.params.country; + const city = ctx.params.city ?? undefined; + + let config = supportedList[country.toLowerCase()]; + let desc; + + if (city === undefined) { + desc = `中国驻${config.countryCN}大使馆 -- 重要通知`; + } else { + config = config.consulates[city.toLowerCase()]; + desc = `中国驻${config.cityCN}领事馆 -- 重要通知`; + } + + const link = config.link; + const hostname = new URL(link).hostname; + + const res = await got(link); + const $ = cheerio.load(res.data); + + const list = []; + + $(config.list) + .slice(0, 10) + .each((i, e) => { + const temp = new URL($(e).attr('href'), link); + if (temp.hostname === hostname) { + list.push(temp); + } + }); + + const out = await Promise.all( + list.map((link) => + ctx.cache.tryGet(link.href, async () => { + const response = await got(link); + const $ = cheerio.load(response.data); + const single = { + title: $(config.title).text(), + link, + description: $(config.description).html(), + pubDate: parseDate($(config.pubDate).text().replace('(', '').replace(')', '')), + }; + return single; + }) + ) + ); + + ctx.state.data = { + title: desc, + description: desc, + link, + item: out, + }; +}; diff --git a/lib/v2/embassy/maintainer.js b/lib/v2/embassy/maintainer.js new file mode 100644 index 000000000..b1c30efbf --- /dev/null +++ b/lib/v2/embassy/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:country/:city?': ['HenryQW'], +}; diff --git a/lib/v2/embassy/radar.js b/lib/v2/embassy/radar.js new file mode 100644 index 000000000..12a8a367e --- /dev/null +++ b/lib/v2/embassy/radar.js @@ -0,0 +1,258 @@ +module.exports = { + 'china-embassy.org': { + _name: '中国驻外使领馆', + ca: [ + { + title: '重要通知 - 加拿大大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/ca', + }, + ], + jp: [ + { + title: '通知通告 - 日本大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfws/LSB', '/'], + target: '/embassy/jp', + }, + ], + kr: [ + { + title: '重要通知 - 韩国大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/lsqz/ls_tz', '/'], + target: '/embassy/kr', + }, + ], + my: [ + { + title: '重要通知 - 马来西亚大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/my', + }, + ], + sg: [ + { + title: '重要通知 - 新加坡大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfw/zytzs', '/'], + target: '/embassy/sg', + }, + ], + }, + 'china-embassy.gov.cn': { + _name: '中国驻外使领馆', + us: [ + { + title: '重要通知 - 美国大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/zytz', '/'], + target: '/embassy/us', + }, + ], + }, + 'chinese-embassy.org.uk': { + _name: '中国驻外使领馆', + www: [ + { + title: '领事协助 - 英国大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfw/lsxz', '/'], + target: '/embassy/us', + }, + ], + }, + 'chineseembassy.org': { + _name: '中国驻外使领馆', + de: [ + { + title: '近期通知 - 德国大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfw/jqtz', '/'], + target: '/embassy/de', + }, + ], + fr: [ + { + title: '重要通知 - 法国大使馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zgzfg/zgsg/lsb', '/'], + target: '/embassy/fr', + }, + ], + }, + 'china-consulate.org': { + _name: '中国驻外使领馆', + marseille: [ + { + title: '领事服务最新公告 - 马赛总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfwgg', '/'], + target: '/embassy/fr/marseille', + }, + ], + strasbourg: [ + { + title: '重要通知 - 斯特拉斯堡总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfw', '/'], + target: '/embassy/fr/strasbourg', + }, + ], + lyon: [ + { + title: '通知、通告 - 里昂总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tztg', '/'], + target: '/embassy/fr/lyon', + }, + ], + nagasaki: [ + { + title: '通知公告 - 长崎总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tzgg', '/'], + target: '/embassy/jp/nagasaki', + }, + ], + osaka: [ + { + title: '通知公告 - 大阪总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tzgg', '/'], + target: '/embassy/jp/osaka', + }, + ], + fukuoka: [ + { + title: '通知公告 - 福冈总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tzgg', '/'], + target: '/embassy/jp/fukuoka', + }, + ], + sapporo: [ + { + title: '通知公告 - 札幌总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tzgg', '/'], + target: '/embassy/jp/sapporo', + }, + ], + niigata: [ + { + title: '通知通告 - 新潟总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsbh/tztg', '/'], + target: '/embassy/jp/niigata', + }, + ], + busan: [ + { + title: '通知公告 - 釜山总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lsfw/tzgg101', '/'], + target: '/embassy/kr/busan', + }, + ], + gwangju: [ + { + title: '公告通知 - 光州总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/lbxx/ggtz', '/'], + target: '/embassy/kr/gwangju', + }, + ], + edinburgh: [ + { + title: '重要通知 - 爱丁堡总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/uk/edinburgh', + }, + ], + newyork: [ + { + title: '重要通知 - 纽约总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/fwzc/zxtz', '/'], + target: '/embassy/us/newyork', + }, + ], + }, + 'chinaconsulatechicago.org': { + _name: '中国驻外使领馆', + www: [ + { + title: '领馆重要通知 - 芝加哥总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/us/chicago', + }, + ], + }, + 'chinaconsulatesf.org': { + _name: '中国驻外使领馆', + www: [ + { + title: '重要通知 - 旧金山总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/us/sanfrancisco', + }, + ], + }, + 'chineseconsulate.org': { + _name: '中国驻外使领馆', + montreal: [ + { + title: '重要通知 - 蒙特利尔总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/zytz', '/'], + target: '/embassy/ca/montreal', + }, + ], + munich: [ + { + title: '近期通知 - 慕尼黑总领馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/jqtz', '/'], + target: '/embassy/de/munich', + }, + ], + nagoya: [ + { + title: '通知公告 - 名古屋总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: '', + target: '/embassy/jp/nagoya', + }, + ], + jeju: [ + { + title: '公告栏 - 济州总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/ggl', '/'], + target: '/embassy/kr/jeju', + }, + ], + belfast: [ + { + title: '通知通告 - 贝尔法斯特总领馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tztg', '/'], + target: '/embassy/uk/belfast', + }, + ], + manchester: [ + { + title: '通知公告 - 曼彻斯特总领事馆', + docs: 'https://docs.rsshub.app/government.html#zhong-guo-zhu-wai-shi-ling-guan', + source: ['/chn/tzgg', '/'], + target: '/embassy/uk/manchester', + }, + ], + }, +}; diff --git a/lib/v2/embassy/router.js b/lib/v2/embassy/router.js new file mode 100644 index 000000000..23a683682 --- /dev/null +++ b/lib/v2/embassy/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/:country/:city?', require('./index')); +}; diff --git a/lib/routes/embassy/supportedList.js b/lib/v2/embassy/supportedList.js similarity index 98% rename from lib/routes/embassy/supportedList.js rename to lib/v2/embassy/supportedList.js index 3bb0d88aa..979eb581d 100644 --- a/lib/routes/embassy/supportedList.js +++ b/lib/v2/embassy/supportedList.js @@ -222,11 +222,11 @@ module.exports = { us: { country: 'us', countryCN: '美国', - link: 'http://www.china-embassy.org/chn/lszj/zytz/', - list: 'div#docMore a', - title: '#News_Body_Title', - description: '#News_Body_Txt_A', - pubDate: '#News_Body_Time > div:nth-child(1) > span', + link: 'http://us.china-embassy.gov.cn/zytz/', + list: '.list-content ul li a', + title: 'div.title', + description: 'div.trs_editor_view', + pubDate: 'div.date', consulates: { chicago: { cityCN: '芝加哥',