From d717d190bce3239ff37d0282d3df975e44af5466 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Wed, 6 Sep 2023 01:34:38 +0800 Subject: [PATCH] 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 --------- --- lib/v2/cna/index.js | 55 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/lib/v2/cna/index.js b/lib/v2/cna/index.js index d33fb28ef0..0e26bf1c27 100644 --- a/lib/v2/cna/index.js +++ b/lib/v2/cna/index.js @@ -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, }; };