From c3ffb3cc637c5f863606305be7870d78f3ea2f1a Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Wed, 25 Oct 2023 01:17:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BA=BA=E6=B0=91=E7=BD=91?= =?UTF-8?q?=E4=BA=BA=E6=B0=91=E6=97=B6=E8=AF=84=20(#13630)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/v2/people/index.js | 39 +++++++++++++++------------------------ lib/v2/people/router.js | 3 +-- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/lib/v2/people/index.js b/lib/v2/people/index.js index 931b3aacf..b0af27d1a 100644 --- a/lib/v2/people/index.js +++ b/lib/v2/people/index.js @@ -6,33 +6,34 @@ const { parseDate } = require('@/utils/parse-date'); const { isValidHost } = require('@/utils/valid-host'); module.exports = async (ctx) => { - const site = ctx.params[0] ?? 'www'; - let category = ctx.params[1] ?? (site === 'www' ? '59476' : ''); + const { site = 'www' } = ctx.params; + let { category = site === 'www' ? '59476' : '' } = ctx.params; category = site === 'cpc' && category === '24h' ? '87228' : category; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; + if (!isValidHost(site)) { throw Error('Invalid site'); } const rootUrl = `http://${site}.people.com.cn`; - const currentUrl = `${rootUrl}/GB/${category}/index.html`; + const currentUrl = new URL(`GB/${category}`, rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, + const { data: response } = await got(currentUrl, { responseType: 'buffer', }); - const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + const $ = cheerio.load(iconv.decode(response, 'gbk')); $('em').remove(); $('.bshare-more, .page_n, .page').remove(); - $('a img').each(function () { - $(this).parent().remove(); + $('a img, h3 img').each((_, e) => { + $(e).parent().remove(); }); - let items = $('.p6, div.p2j_list, div.headingNews, div.ej_list_box, .fl') + let items = $('.p6, div.p2j_list, div.headingNews, div.ej_list_box, .fl, .leftItem') .find('a') + .slice(0, limit) .toArray() .map((item) => { item = $(item); @@ -41,7 +42,7 @@ module.exports = async (ctx) => { return { title: item.text(), - link: link.indexOf('http') === 0 ? link : `${rootUrl}${link.replace(/^\.\./, '')}`, + link: link.indexOf('http') === 0 ? link : new URL(link.replace(/^\.\./, ''), rootUrl).href, }; }); @@ -49,27 +50,17 @@ module.exports = async (ctx) => { items.map((item) => ctx.cache.tryGet(item.link, async () => { try { - const detailResponse = await got({ - method: 'get', - url: item.link, + const { data: detailResponse } = await got(item.link, { responseType: 'buffer', }); - const data = iconv.decode(detailResponse.data, 'gbk'); + const data = iconv.decode(detailResponse, 'gbk'); const content = cheerio.load(data); content('.paper_num, #rwb_tjyd').remove(); item.description = content('.rm_txt_con, .show_text').html(); - item.pubDate = timezone( - parseDate( - data - .match(/(\d{4}年\d{2}月\d{2}日\d{2}:\d{2})/)[1] - .replace(/年|月/g, '-') - .replace(/日/, ' ') - ), - +8 - ); + item.pubDate = timezone(parseDate(data.match(/(\d{4}年\d{2}月\d{2}日\d{2}:\d{2})/)[1], 'YYYY年MM月DD日 HH:mm'), +8); } catch (err) { item.description = err; } diff --git a/lib/v2/people/router.js b/lib/v2/people/router.js index b689de923..a6e40929f 100644 --- a/lib/v2/people/router.js +++ b/lib/v2/people/router.js @@ -1,6 +1,5 @@ module.exports = function (router) { router.get('/liuyan/:id/:state?', require('./liuyan')); router.get('/xjpjh/:keyword?/:year?', require('./xjpjh')); - router.get(/([\w\d]+)\/([\w\d/]+)?/, require('./index')); - router.get('/:0?', require('./index')); + router.get('/:site?/:category*', require('./')); };