Skip to content

Commit

Permalink
feat(route): add zuzhirenshi 中国组织人事报 (DIYgod#12077)
Browse files Browse the repository at this point in the history
* add zuzhirenshi

* complete radar

* change title format

* Apply suggestion from code review
  • Loading branch information
5upernova-heng authored Mar 11, 2023
1 parent 6fd8bdf commit 9dd7f0b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -2570,3 +2570,15 @@ category 对应的关键词有
`https://www.rfa.org/cantonese/news/htm` 对应 `/rfa/cantonese/news/htm`

</Route>

## 组织人事报

### 电子报

<Route author="5upernove-heng" example="/zuzhirenshi" path="/zuzhirenshi/:id?" :paramsDesc="['报纸版号,默认为全部']" radar="1">

| 第一版 要闻 | 第二版 要闻 | 第三版 人才 | 第四版 人社工作 | 第五版 基层党建 | 第六版 理论评论 | 第七版 史事通鉴 | 第八版 关注 |
| ----------- | ----------- | ----------- | --------------- | --------------- | --------------- | --------------- | ----------- |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

</Route>
87 changes: 87 additions & 0 deletions lib/v2/zuzhirenshi/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');

function parseJavascript(jsCode) {
// Parse Release Date, SubLanMuList, AreaInfoList
const ReleaseDateRegex = /var ReleaseDate\s*=\s*"([^"]+)";/;
const ReleaseDateMatch = jsCode.match(ReleaseDateRegex);
const ReleaseDate = ReleaseDateMatch ? ReleaseDateMatch[1] : null;

const SubLanMuListRegex = /var SubLanMuList\s*=\s*(\[.+?\]);/;
const SubLanMuListMatch = jsCode.match(SubLanMuListRegex);
const SubLanMuList = SubLanMuListMatch ? JSON.parse(SubLanMuListMatch[1]) : null;

const InfoListRegex = /var InfoList\s*=\s*(\[.+?\]);/;
const InfoListMatch = jsCode.match(InfoListRegex);
const InfoList = InfoListMatch ? JSON.parse(InfoListMatch[1]) : null;

return {
ReleaseDate,
SubLanMuList,
InfoList,
};
}

async function getArticle(link) {
const response = await got(link);
const $ = cheerio.load(response.data);
const title = $('div.listhottitle3').text().trim();
const pubDate = timezone(parseDate($('.innertop').clone().children().remove().end().text().trim(), 'YYYY/MM/DD'), +8);
const description = $('.innercontent').html();
return {
title,
link,
pubDate,
description,
};
}

async function getPageInfo(url) {
const response = await got(url);
const $ = cheerio.load(response.data);
const jsCode = $('script').text().split('$(function')[0];
return parseJavascript(jsCode);
}

async function getPageContents(releaseDate, id, ctx) {
const baseUrl = `http://www.zuzhirenshi.com/dianzibao`;
const link = `${baseUrl}/${releaseDate}/${id}/index.htm`;
const info = await getPageInfo(link);
const items = await Promise.all(
info.InfoList.map((article) => {
const link = `${baseUrl}/${releaseDate}/${id}/${article.infoid}.htm`;
const item = ctx.cache.tryGet(link, async () => {
const item = await getArticle(link);
return item;
});
return item;
})
);
return items;
}

module.exports = async (ctx) => {
const url = 'http://www.zuzhirenshi.com/dianzibao/index.htm';
const info = await getPageInfo(url);
const releaseDate = info.ReleaseDate;
const id = ctx.params.id || 0;
let items = [];
if (id === 0) {
const promises = [];
for (let i = 1; i <= info.SubLanMuList.length; i++) {
promises.push(getPageContents(releaseDate, i, ctx));
}
const results = await Promise.all(promises);
items = results.reduce((a, b) => a.concat(b), []);
} else {
items = await getPageContents(releaseDate, id, ctx);
}
const titleSuffix = id ? `${String(info.SubLanMuList[id - 1].lmmc)}` : '全部';
ctx.state.data = {
title: `组织人事报 - ${titleSuffix} - ${releaseDate}`,
link: 'https://www.zuzhirenshi.com/dianzibao/index.htm',
item: items,
};
};
3 changes: 3 additions & 0 deletions lib/v2/zuzhirenshi/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/:id?': ['5upernova-heng'],
};
13 changes: 13 additions & 0 deletions lib/v2/zuzhirenshi/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
'zuzhirenshi.com': {
_name: '组织人事报',
'.': [
{
title: '日报',
docs: 'https://docs.rsshub.app/traditional-media.html#zu-zhi-ren-shi-bao',
source: ['/dianzibao/*'],
target: '/zuzhirenshi',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/zuzhirenshi/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:id?', require('.'));
};

0 comments on commit 9dd7f0b

Please sign in to comment.