fix(route): 有知有行有知文章 (#9668)

This commit is contained in:
Ethan Shen 2022-05-01 06:34:00 +08:00 committed by GitHub
parent e5484a75bd
commit 70e20bec2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 30 deletions

View File

@ -422,7 +422,7 @@ pageClass: routes
### 有知文章
<Route author="broven Fatpandac" example="/youzhiyouxing/materials" path="/youzhiyouxing/materials/:column?" :paramsDesc="['分类,见下表,默认为全部']">
<Route author="broven Fatpandac nczitzk" example="/youzhiyouxing/materials" path="/youzhiyouxing/materials/:id?" :paramsDesc="['分类,见下表,默认为全部']">
| 全部 | 知行小酒馆 | 知行黑板报 | 无人知晓 | 孟岩专栏 | 知行读书会 | 你好,同路人 |
| :-: | :---: | :---: | :--: | :--: | :---: | :----: |

View File

@ -1,3 +1,3 @@
module.exports = {
'/materials/:column?': ['broven', 'Fatpandac'],
'/materials/:id?': ['broven', 'Fatpandac', 'nczitzk'],
};

View File

@ -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(/(<img.*?) src(=.*?>)/g, '$1 data$2')
.replace(/(<img.*?) data-src(=.*?>)/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,
};
};

View File

@ -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';
}
},

View File

@ -1,3 +1,3 @@
module.exports = function (router) {
router.get('/materials/:column?', require('./materials'));
router.get('/materials/:id?', require('./materials'));
};