From 70e20bec2cb76bb8d3d10a7b06b077bbc3a2e056 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sun, 1 May 2022 06:34:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=9C=89=E7=9F=A5=E6=9C=89?= =?UTF-8?q?=E8=A1=8C=E6=9C=89=E7=9F=A5=E6=96=87=E7=AB=A0=20(#9668)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/finance.md | 2 +- lib/v2/youzhiyouxing/maintainer.js | 2 +- lib/v2/youzhiyouxing/materials.js | 52 ++++++++++++++++++------------ lib/v2/youzhiyouxing/radar.js | 12 +++---- lib/v2/youzhiyouxing/router.js | 2 +- 5 files changed, 40 insertions(+), 30 deletions(-) diff --git a/docs/finance.md b/docs/finance.md index f331db59b..ed6f286ee 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -422,7 +422,7 @@ pageClass: routes ### 有知文章 - + | 全部 | 知行小酒馆 | 知行黑板报 | 无人知晓 | 孟岩专栏 | 知行读书会 | 你好,同路人 | | :-: | :---: | :---: | :--: | :--: | :---: | :----: | diff --git a/lib/v2/youzhiyouxing/maintainer.js b/lib/v2/youzhiyouxing/maintainer.js index 5d519098d..90199b3d8 100644 --- a/lib/v2/youzhiyouxing/maintainer.js +++ b/lib/v2/youzhiyouxing/maintainer.js @@ -1,3 +1,3 @@ module.exports = { - '/materials/:column?': ['broven', 'Fatpandac'], + '/materials/:id?': ['broven', 'Fatpandac', 'nczitzk'], }; diff --git a/lib/v2/youzhiyouxing/materials.js b/lib/v2/youzhiyouxing/materials.js index ac1aec964..ca86dadc3 100644 --- a/lib/v2/youzhiyouxing/materials.js +++ b/lib/v2/youzhiyouxing/materials.js @@ -2,35 +2,46 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const { parseDate } = require('@/utils/parse-date'); -const rootUrl = 'https://youzhiyouxing.cn'; - module.exports = async (ctx) => { - const column = ctx.params.column ?? '0'; - const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 30; - const url = `${rootUrl}/materials?column=${column}&limit=${limit}`; + const id = ctx.params.id ?? ''; + + const rootUrl = 'https://youzhiyouxing.cn'; + const currentUrl = `${rootUrl}/materials?column_id=${id}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); - const response = await got(url); const $ = cheerio.load(response.data); - const title = $(`a[phx-value-column="${column}"]`).text() === '' ? '全部' : $(`a[phx-value-column="${column}"]`).text(); - const list = $('[phx-click=material]') - .map((_, item) => ({ - title: $(item).find('h3').text(), - link: `${rootUrl}/materials/${$(item).attr('phx-value-id')}`, - pubDate: parseDate($(item).find('div.xs-text.muted-text.tw-text-right').text(), ['YYYY年M月D日', 'M月D日']), - })) - .get(); - const items = await Promise.all( - list.map((item) => + let items = $('li[phx-click="material"]') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text(), + link: `${rootUrl}/materials/${item.attr('phx-value-id')}`, + pubDate: parseDate(item.find('.tw-text-right').text(), ['YYYY年M月D日', 'M月D日']), + }; + }); + + items = await Promise.all( + items.map((item) => ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got(item.link); + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + item.author = content('.tw-inline').text().replace('·', ''); item.description = content('#zx-rangy-root') .html() .replace(/()/g, '$1 data$2') .replace(/()/g, '$1 src$2'); - item.author = content('span.tw-inline').text().replace('·', ''); return item; }) @@ -38,9 +49,8 @@ module.exports = async (ctx) => { ); ctx.state.data = { - title: `有知有行 - ${title}`, - link: url, - description: `有知有行 - ${title}`, + title: `有知有行 - ${$(`a[phx-value-column_id="${id}"]`).text()}`, + link: currentUrl, item: items, }; }; diff --git a/lib/v2/youzhiyouxing/radar.js b/lib/v2/youzhiyouxing/radar.js index 112c13c92..67aad3ec2 100644 --- a/lib/v2/youzhiyouxing/radar.js +++ b/lib/v2/youzhiyouxing/radar.js @@ -13,7 +13,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '4') { + if (new URL(url).searchParams.get('column_id') === '4') { return '/youzhiyouxing/materials/4'; } }, @@ -23,7 +23,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '2') { + if (new URL(url).searchParams.get('column_id') === '2') { return '/youzhiyouxing/materials/2'; } }, @@ -33,7 +33,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '10') { + if (new URL(url).searchParams.get('column_id') === '10') { return '/youzhiyouxing/materials/10'; } }, @@ -43,7 +43,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '1') { + if (new URL(url).searchParams.get('column_id') === '1') { return '/youzhiyouxing/materials/1'; } }, @@ -53,7 +53,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '3') { + if (new URL(url).searchParams.get('column_id') === '3') { return '/youzhiyouxing/materials/3'; } }, @@ -63,7 +63,7 @@ module.exports = { docs: 'https://docs.rsshub.app/finance.html#you-you-wei-zhi-zhi-you-you-wei-xing-hang-xing-hang-heng-you-you-wei-zhi-zhi-wen-zhang-zhang', source: ['/materials'], target: (_params, url) => { - if (new URL(url).searchParams.get('column') === '11') { + if (new URL(url).searchParams.get('column_id') === '11') { return '/youzhiyouxing/materials/11'; } }, diff --git a/lib/v2/youzhiyouxing/router.js b/lib/v2/youzhiyouxing/router.js index 01d969746..7ab917168 100644 --- a/lib/v2/youzhiyouxing/router.js +++ b/lib/v2/youzhiyouxing/router.js @@ -1,3 +1,3 @@ module.exports = function (router) { - router.get('/materials/:column?', require('./materials')); + router.get('/materials/:id?', require('./materials')); };