fix: recover cntheory (#14300)

* fix: recover cntheory

* fix: replaceAll
This commit is contained in:
Tony 2024-01-22 04:52:00 +00:00 committed by GitHub
parent 0a9296075e
commit b50d1d7725
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,3 @@
module.exports = {
'/paper/:id?': ['nczitzk'],
};

87
lib/v2/cntheory/paper.js Normal file
View File

@ -0,0 +1,87 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const id = ctx.params.id;
const rootUrl = 'https://paper.cntheory.com';
let response = await got({
method: 'get',
url: rootUrl,
});
response = await got({
method: 'get',
url: `${rootUrl}/${response.data.match(/URL=(.*)"/)[1]}`,
});
const $ = cheerio.load(response.data);
const matches = response.data.match(/images\/(\d{4}-\d{2}\/\d{2})\/\w+\/\w+_brief/);
const link = `${rootUrl}/html/${matches[1]}`;
let items = [];
await Promise.all(
$('#pageLink')
.toArray()
.filter((p) => (id ? $(p).text().split('').pop() === id : true))
.map((p) => `${link}/${$(p).attr('href').replace(/\.\//, '')}`)
.map(async (p) => {
const pageResponse = await got({
method: 'get',
url: p,
});
const page = cheerio.load(pageResponse.data);
items.push(
...page('table')
.last()
.find('a')
.toArray()
.map((a) => `${link}/${$(a).attr('href')}`)
);
})
);
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item, async () => {
const detailResponse = await got({
method: 'get',
url: item,
});
const content = cheerio.load(detailResponse.data);
return {
link: item,
title: content('h1').text(),
pubDate: parseDate(matches[1], 'YYYY-MM/DD'),
enclosure_url: `${rootUrl}${
content('.ban_t a')
.first()
.attr('href')
.match(/(\/images.*)/)[1]
}`,
description: art(path.join(__dirname, 'templates/description.art'), {
resource: content('#reslist').html().replaceAll('display:none;', ''),
description: content('founder-content').html(),
}),
};
})
)
);
ctx.state.data = {
title: `学习时报${id ? ` - ${id}` : ''}`,
link: rootUrl,
item: items,
allowEmpty: true,
};
};

13
lib/v2/cntheory/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'cntheory.com': {
_name: '理论网',
paper: [
{
title: '学习时报',
docs: 'https://docs.rsshub.app/routes/traditional-media#li-lun-wang-xue-xi-shi-bao',
source: ['/'],
target: (params, url) => `/cntheory/paper/${new URL(url).toString().match(/-(\w+)\.htm/)[1]}`,
},
],
},
};

View File

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

View File

@ -0,0 +1,2 @@
{{@ resource }}
{{@ description }}

View File

@ -1489,6 +1489,14 @@ This route adds the missing photo and Link element. (Offical RSS doesn't have Li
| hakka | political | medical | local | international |
</Route>
## 理论网 {#li-lun-wang}
### 学习时报 {#li-lun-wang-xue-xi-shi-bao}
<Route author="nczitzk" example="/cntheory/paper" path="/cntheory/paper/:id?" paramsDesc={['板块,默认为全部']}>
如订阅 **第 A1 版:国内大局**,路由为 [`/cntheory/paper/国内大局`](https://rsshub.app/cntheory/paper/国内大局)。
</Route>
## 联合早报 {#lian-he-zao-bao}
:::warning