feat: 添加文汇报 (#3438)

This commit is contained in:
hoilc
2019-11-19 17:54:31 +08:00
committed by DIYgod
parent 6d022e0236
commit b02dcfe7b7
3 changed files with 86 additions and 0 deletions

View File

@@ -308,6 +308,12 @@ category 对应的关键词有
<Route author="Polynomia" example="/guardian/china" path="/guardian/china"/>
## 文汇报
### 分类
<Route author="hoilc" example="/whb/bihui" path="/whb/:category" :paramsDesc="['文汇报分类名, 可在该分类的 URL 中找到(即 http://www.whb.cn/zhuzhan/:category/index.html)']" />
## 香港 01
### 热门

View File

@@ -1935,6 +1935,9 @@ router.get('/gov/cnca/hydt', require('./routes/gov/cnca/hydt'));
router.get('/gov/cnca/zxtz', require('./routes/gov/cnca/zxtz'));
// 文汇报
router.get('/whb/:category', require('./routes/whb/zhuzhan'));
// 三界异次元
router.get('/3ycy/home', require('./routes/3ycy/home.js'));

77
lib/routes/whb/zhuzhan.js Normal file
View File

@@ -0,0 +1,77 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const category = ctx.params.category;
const base = 'http://www.whb.cn';
const url = `${base}/zhuzhan/${category}/index.html`;
const list_response = await got.get(url);
const $ = cheerio.load(list_response.data);
const category_name = $('.title_jingpinhui > a')
.first()
.text();
const list = $('.info_jingpinhui').toArray();
const parseContent = (htmlString) => {
const $ = cheerio.load(htmlString);
const date_and_author = $('.content_other').text();
const matched_date = /(\d+)年(\d+)月(\d+)日 (\d+):(\d+):(\d+)/.exec(date_and_author);
const date = new Date(parseInt(matched_date[1]), parseInt(matched_date[2]) - 1, parseInt(matched_date[3]), parseInt(matched_date[4]), parseInt(matched_date[5]), parseInt(matched_date[6]));
const author = date_and_author.split(':').slice(-1)[0];
const content = $('.content_info');
return {
author: author,
description: content.html(),
pubDate: date,
};
};
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('.title > a');
const path = title.attr('href');
const link = `${base}${path}`;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const rssitem = {
title: title.text().trim(),
link: link,
};
try {
const response = await got.get(link);
const result = parseContent(response.data);
if (!result) {
return Promise.resolve('');
}
rssitem.author = result.author;
rssitem.description = result.description;
rssitem.pubDate = result.pubDate;
} catch (err) {
return Promise.resolve('');
}
ctx.cache.set(link, JSON.stringify(rssitem));
return Promise.resolve(rssitem);
})
);
ctx.state.data = {
title: `文汇报 - ${category_name}`,
link: url,
item: out.filter((item) => item !== ''),
};
};