From 1c1de6c09d53f578bf336533827c4e2c4774c05b Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Thu, 8 Oct 2020 22:10:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E5=8F=B0=E6=B9=BE=E4=B8=AD?= =?UTF-8?q?=E5=A4=AE=E9=80=9A=E8=AE=AF=E7=A4=BE=20(#5815)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Henry Wang --- docs/traditional-media.md | 12 ++++++++++ lib/router.js | 3 +++ lib/routes/cna/index.js | 49 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/routes/cna/index.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 88971d963..c7f924ea3 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -571,6 +571,18 @@ category 对应的关键词有 +## 台湾中央通讯社 + +### 分类 + + + +| 即時 | 政治 | 國際 | 兩岸 | 產經 | 證券 | 科技 | 生活 | 社會 | 地方 | 文化 | 運動 | 娛樂 | +| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | +| aall | aipl | aopl | acn | aie | asc | ait | ahel | asoc | aloc | acul | aspt | amov | + + + ## 网易新闻专栏 ### 栏目 diff --git a/lib/router.js b/lib/router.js index 5eed4c74b..53165c732 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3257,6 +3257,9 @@ router.get('/appsales/:caty?/:time?', require('./routes/appsales/index')); // CGTN router.get('/cgtn/top', require('./routes/cgtn/top')); +// 台湾中央通讯社 +router.get('/cna/:id?', require('./routes/cna/index')); + // 华为心声社区 router.get('/huawei/xinsheng/:caty?/:order?/:keyword?', require('./routes/huawei/xinsheng/index')); diff --git a/lib/routes/cna/index.js b/lib/routes/cna/index.js new file mode 100644 index 000000000..52f974371 --- /dev/null +++ b/lib/routes/cna/index.js @@ -0,0 +1,49 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + ctx.params.id = ctx.params.id || 'aall'; + + const rootUrl = `https://www.cna.com.tw/list/${ctx.params.id}.aspx`; + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + const list = $('#jsMainList li a div h2') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: item.parents('a').attr('href'), + pubDate: new Date(item.next().text() + ' GMT+8').toUTCString(), + }; + }) + .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); + const topImage = content('.fullPic').html(); + + item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: rootUrl, + item: items, + }; +};