feat: add 台湾中央通讯社 (#5815)

Co-authored-by: Henry Wang <hi@henry.wang>
This commit is contained in:
Ethan Shen 2020-10-08 22:10:00 +08:00 committed by GitHub
parent 5606d4389f
commit 1c1de6c09d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -571,6 +571,18 @@ category 对应的关键词有
</Route>
## 台湾中央通讯社
### 分类
<Route author="nczitzk" example="/cna/aall" path="/cna/:id?" :paramsDesc="['分类 id见下表默认为 aall']">
| 即時 | 政治 | 國際 | 兩岸 | 產經 | 證券 | 科技 | 生活 | 社會 | 地方 | 文化 | 運動 | 娛樂 |
| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
| aall | aipl | aopl | acn | aie | asc | ait | ahel | asoc | aloc | acul | aspt | amov |
</Route>
## 网易新闻专栏
### 栏目

View File

@ -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'));

49
lib/routes/cna/index.js Normal file
View File

@ -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,
};
};