Skip to content

Commit

Permalink
fix(route): 学习时报分版面 (DIYgod#12069)
Browse files Browse the repository at this point in the history
* fix(route): 学习时报分版面

* fix: use HTTPS

---------
  • Loading branch information
nczitzk authored Mar 9, 2023
1 parent 0cafed6 commit fe96ab5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
14 changes: 2 additions & 12 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -1362,19 +1362,9 @@ category 对应的关键词有

### 学习时报

<Route author="nczitzk" example="/cntheory/paper" path="/cntheory/paper/:id?" :paramsDesc="['编号,见下表,默认为全部']">
<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 |
如订阅 **第 A1 版:国内大局**,路由为 [`/cntheory/paper/国内大局`](https://rsshub.app/cntheory/paper/国内大局)

</Route>

Expand Down
54 changes: 25 additions & 29 deletions lib/v2/cntheory/paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require('path');
module.exports = async (ctx) => {
const id = ctx.params.id;

const rootUrl = 'http://paper.cntheory.com';
const rootUrl = 'https://paper.cntheory.com';

let response = await got({
method: 'get',
Expand All @@ -16,43 +16,38 @@ module.exports = async (ctx) => {

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]}`,
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 = $('table')
.last()
.find('a')
.toArray()
.map((a) => `${link}/${$(a).attr('href')}`);
let items = [];

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,
});
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);
const page = cheerio.load(pageResponse.data);

items.push(
...page('table')
.last()
.find('a')
.toArray()
.map((a) => `${link}/${$(a).attr('href')}`)
);
})
);
}
items.push(
...page('table')
.last()
.find('a')
.toArray()
.map((a) => `${link}/${$(a).attr('href')}`)
);
})
);

items = await Promise.all(
items.map((item) =>
Expand Down Expand Up @@ -86,8 +81,9 @@ module.exports = async (ctx) => {
);

ctx.state.data = {
title: `学习时报${id ? ` - ${$('.l_t').first().text()}` : ''}`,
title: `学习时报${id ? ` - ${id}` : ''}`,
link: rootUrl,
item: items,
allowEmpty: true,
};
};

0 comments on commit fe96ab5

Please sign in to comment.