From baaa141dcf10e3b2f95d3a9ac7ba61aaea6ba54d Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 10 Dec 2020 06:27:26 +0800 Subject: [PATCH] feat: add the united states trade representative press release (#6384) --- docs/en/government.md | 14 ++++++++ docs/government.md | 14 ++++++++ lib/router.js | 3 ++ lib/routes/us/ustr/press-releases.js | 51 ++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 lib/routes/us/ustr/press-releases.js diff --git a/docs/en/government.md b/docs/en/government.md index f576213c0..6572abbe6 100644 --- a/docs/en/government.md +++ b/docs/en/government.md @@ -21,6 +21,20 @@ pageClass: routes +## The United States Trade Representative + +### Press Release + + + +::: tip Tip + +Fill in the English expression for the month in the Month field, eg `December` for the 12th Month。 + +::: + + + ## United Nations ### Security Council Vetoed a Resolution diff --git a/docs/government.md b/docs/government.md index 48a7ede88..81abef700 100644 --- a/docs/government.md +++ b/docs/government.md @@ -83,6 +83,20 @@ pageClass: routes +## 美国贸易代表办公室 + +### 新闻稿 + + + +::: tip 提示 + +月份处填写该月的英语表达,如 12 月 应填入 `December`。 + +::: + + + ## 美国中央情报局 ### 年度信息自由法报告 diff --git a/lib/router.js b/lib/router.js index 2596e85f0..b23383f69 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3670,4 +3670,7 @@ router.get('/proletar/:type?/:id?', require('./routes/proletar/index')); // QTTabBar router.get('/qttabbar/change-log', require('./routes/qttabbar/change-log')); +// 美国贸易代表办公室 +router.get('/ustr/press-releases/:year?/:month?', require('./routes/us/ustr/press-releases')); + module.exports = router; diff --git a/lib/routes/us/ustr/press-releases.js b/lib/routes/us/ustr/press-releases.js new file mode 100644 index 000000000..0f07d89d2 --- /dev/null +++ b/lib/routes/us/ustr/press-releases.js @@ -0,0 +1,51 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const year = ctx.params.year || new Date().getFullYear().toString(); + const month = ctx.params.month || ''; + + const rootUrl = 'https://ustr.gov'; + const currentUrl = `${rootUrl}/about-us/policy-offices/press-office/press-releases/${year}/${month}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('ul.listing li a') + .slice(0, 15) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: `${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 content = cheerio.load(detailResponse.data); + + item.description = content('.field--name-body').html(); + item.pubDate = new Date(content('.title-underline').next().text()).toUTCString(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};