feat(route): add category to cna & other improvements (#13216)

* feat(route): add category to cna.

* Include `carouseList` articles.

* Update index.js

* fix: use web api

---------
This commit is contained in:
Andvari
2023-09-06 01:34:38 +08:00
committed by GitHub
parent 31b56187c0
commit d717d190bc
+31 -24
View File
@@ -5,31 +5,30 @@ const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const id = ctx.params.id || 'aall';
const isTopic = /^\d+$/.test(id);
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 20;
let rootUrl;
if (/^\d+$/.test(id)) {
rootUrl = `https://www.cna.com.tw/topic/newstopic/${id}.aspx`;
} else {
rootUrl = `https://www.cna.com.tw/list/${id}.aspx`;
}
const response = await got({
method: 'get',
url: rootUrl,
const { data: response } = await got({
method: 'post',
url: `https://www.cna.com.tw/cna2018api/api/${isTopic ? 'WTopic' : 'WNewsList'}`,
json: {
action: '0',
category: isTopic ? 'newstopic' : id,
tno: isTopic ? id : undefined,
pagesize: limit,
pageidx: 1,
},
});
const $ = cheerio.load(response.data);
const list = $('.mainList li a div h2')
.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10)
.toArray()
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.parents('a').attr('href'),
pubDate: timezone(parseDate(item.next().text()), +8),
};
});
const {
ResultData: { MetaData: metadata },
ResultData: resultData,
} = response;
const list = (isTopic ? resultData.Topic.NewsItems : resultData.Items).slice(0, limit).map((item) => ({
title: item.HeadLine,
link: item.PageUrl,
pubDate: timezone(parseDate(item.CreateTime), +8),
}));
const items = await Promise.all(
list.map((item) =>
@@ -42,6 +41,12 @@ module.exports = async (ctx) => {
const topImage = content('.fullPic').html();
item.description = (topImage === null ? '' : topImage) + content('.paragraph').eq(0).html();
item.category = [
...content("meta[property='article:tag']")
.get()
.map((e) => e.attribs.content),
content('.active > a').text(),
];
return item;
})
@@ -49,8 +54,10 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: $('title').text(),
link: rootUrl,
title: metadata.Title,
description: metadata.Description,
link: metadata.CanonicalUrl,
image: metadata.Image,
item: items,
};
};