From 1af5ddc3a29cd0343aa265d13d36aa4246aa38b1 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 24 Aug 2023 01:38:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=99=9A=E7=82=B9=E6=8A=A5?= =?UTF-8?q?=E9=81=93=20(#13092)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 晚点报道 * fix: add programa * fix typo * update lib/v2/latepost/index.js --------- --- lib/v2/latepost/index.js | 112 ++++++++++++++++++++++++++++---------- lib/v2/latepost/radar.js | 7 ++- lib/v2/latepost/router.js | 2 +- 3 files changed, 90 insertions(+), 31 deletions(-) diff --git a/lib/v2/latepost/index.js b/lib/v2/latepost/index.js index 492c4bf6e..4329f04be 100644 --- a/lib/v2/latepost/index.js +++ b/lib/v2/latepost/index.js @@ -3,60 +3,114 @@ const cheerio = require('cheerio'); const timezone = require('@/utils/timezone'); const { parseDate, parseRelativeDate } = require('@/utils/parse-date'); -const titles = { - '': '最新报道', - 1: '晚点独家', - 2: '人物访谈', - 3: '晚点早知道', - 4: '长报道', -}; +/** + * Convert an array into a dictionary object. + * The keys of the dictionary object are the `id` properties of the elements in the array, + * and the values are the remaining properties of each element. + * @param {Array} arr - The array to be converted. + * @returns {Object} - The converted dictionary object. + */ +const arrayToDictionary = (arr) => + arr.reduce( + (dictionary, { id, ...rest }) => ({ + ...dictionary, + [id]: { + ...rest, + }, + }), + {} + ); module.exports = async (ctx) => { - const proma = ctx.params.proma ?? ''; + const { proma } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 5; + + const title = '晚点'; + const defaultTitle = '最新报道'; + const exclusiveCategory = '晚点独家'; const rootUrl = 'https://www.latepost.com'; - const currentUrl = `${rootUrl}/${proma ? 'site/index' : 'news/get-news-data'}`; + const currentUrl = new URL(proma ? `news/index?proma=${proma}` : '', rootUrl).href; - const response = await got({ - method: 'post', - url: currentUrl, - page: 1, - programa: proma, + const apiColumnUrl = new URL('site/get-column', rootUrl).href; + const apiCommentUrl = new URL('news/get-comment', rootUrl).href; + const apiUrl = new URL(proma ? 'site/index' : 'news/get-news-data', rootUrl).href; + + const { data: columnResponse } = await got(apiColumnUrl); + const columns = arrayToDictionary(columnResponse?.data ?? []); + + const { data: response } = await got.post(apiUrl, { + json: { + page: 1, + limit, + programa: parseInt(proma, 10), + }, }); - let items = response.data.data.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25).map((item) => ({ + let items = response.data.slice(0, limit).map((item) => ({ title: item.title, - link: `${rootUrl}${item.detail_url}`, - category: item.label.map((l) => l.label), + link: new URL(item.detail_url, rootUrl).href, + category: [item.is_dj ? exclusiveCategory : undefined, item.programa ? columns[item.programa].title : undefined, ...item.label.map((c) => c.label)], + guid: item.id, + pubDate: parseDate(item.release_time, ['MM月DD日', 'YYYY年MM月DD日']), })); items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, + const { data: detailResponse } = await got(item.link); + + const { data: commentResponse } = await got.post(apiCommentUrl, { + news_id: item.guid, + page: 1, + limit: Infinity, + sort: 1, + delete_num: 0, }); - const content = cheerio.load(detailResponse.data); + const content = cheerio.load(detailResponse); - content('br').each(function () { - content(this).parent().remove(); - }); + item.title = item.title ?? content('div.article-header-title').text(); + item.description = content('#select-main') + .html() + .replace(/


<\/p>/g, ''); + item.author = content('div.article-header-author div.author-link a.label').first().text(); + item.category = item.category.filter((c) => c); + item.guid = `latepost-${item.guid}`; - const pubDate = content('.article-header-date').text(); - item.pubDate = timezone(/月|日/.test(pubDate) ? parseDate((/年/.test(pubDate) ? pubDate : `${new Date().getFullYear()}-${pubDate}`).replace(/年|月/g, '-').replace(/日/g, '')) : parseRelativeDate(pubDate), +8); + const pubDate = content('div.article-header-date').text(); - item.description = content('#select-main').html(); + if (pubDate) { + if (/\d+月\d+日/.test(pubDate)) { + item.pubDate = parseDate(pubDate, ['MM月DD日 HH:mm', 'YYYY年MM月DD日 HH:mm']); + } else { + item.pubDate = parseRelativeDate(pubDate); + } + } + + item.pubDate = timezone(item.pubDate, +8); + item.comments = commentResponse.data?.length() ?? 0; return item; }) ) ); + const icon = new URL('favicon.ico', rootUrl).href; + + const { data: currentResponse } = await got(currentUrl); + + const $ = cheerio.load(currentResponse); + ctx.state.data = { - title: `${titles[proma]} - 晚点`, - link: currentUrl, item: items, + title: `${title} - ${proma ? columns[proma].title : defaultTitle}`, + link: currentUrl, + description: $('div.logo-txt').first().text(), + language: 'zh-cn', + image: new URL($('div.logo-txt img').prop('src'), rootUrl).href, + icon, + logo: icon, + author: title, }; }; diff --git a/lib/v2/latepost/radar.js b/lib/v2/latepost/radar.js index 024874786..6634bb61b 100644 --- a/lib/v2/latepost/radar.js +++ b/lib/v2/latepost/radar.js @@ -6,7 +6,12 @@ module.exports = { title: '报道', docs: 'https://docs.rsshub.app/routes/new-media#wan-dian-latepost-bao-dao', source: '/', - target: (params, url) => `/latepost/${new URL(url).searchParams.get('proma')}`, + target: (params, url) => { + url = new URL(url); + const proma = url.searchParams.get('proma'); + + return `/latepost${proma ? `/${proma}` : ''}`; + }, }, ], }, diff --git a/lib/v2/latepost/router.js b/lib/v2/latepost/router.js index f0db9cf0e..9146b4202 100644 --- a/lib/v2/latepost/router.js +++ b/lib/v2/latepost/router.js @@ -1,3 +1,3 @@ module.exports = function (router) { - router.get('/:proma?', require('./index')); + router.get('/:proma?', require('./')); };