feat(route): add 理论网学习时报 (#9813)

This commit is contained in:
Ethan Shen 2022-05-25 20:15:45 +08:00 committed by GitHub
parent 7002b7ebd1
commit 04f7329b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 0 deletions

View File

@ -1095,6 +1095,26 @@ category 对应的关键词有
<Route author="wushijishan nczitzk" example="/kaopunews/:language?" path="/kaopunews" :paramsDesc="['语言,可选 zh-hans 即简体中文,或 zh-hant 即繁体中文']"/>
## 理论网
### 学习时报
<Route author="nczitzk" example="/cntheory/paper" path="/cntheory/paper/:id?" :paramsDesc="['编号,见下表,默认为全部']">
| 版 | 编号 |
| ----------- | -- |
| 全部 | |
| 第 A1 版:国内大局 | A1 |
| 第 A2 版:市场经济 | A2 |
| 第 A3 版:民主法治 | A3 |
| 第 A4 版:读书治学 | A4 |
| 第 A5 版:特别策划 | A5 |
| 第 A6 版:科技前沿 | A6 |
| 第 A7 版:社会治理 | A7 |
| 第 A8 版:学员天地 | A8 |
</Route>
## 连线 Wired
非订阅用户每月有阅读全文次数限制。

View File

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

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

@ -0,0 +1,93 @@
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 = 'http://paper.cntheory.com';
let response = await got({
method: 'get',
url: rootUrl,
});
response = await got({
method: 'get',
url: `${rootUrl}/${id ? response.data.match(/URL=(.*)"/)[1].replace(/xxsb_\w+\.htm$/, `xxsb_${id}.htm`) : 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 = $('table')
.last()
.find('a')
.toArray()
.map((a) => `${link}/${$(a).attr('href')}`);
if (!id) {
await Promise.all(
$('#pageLink')
.toArray()
.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()
.replace(/display:none;/g, ''),
description: content('founder-content').html(),
}),
};
})
)
);
ctx.state.data = {
title: `学习时报${id ? ` - ${$('.l_t').first().text()}` : ''}`,
link: rootUrl,
item: items,
};
};

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/traditional-media.html#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 }}