fix(route): 人民网人民时评 (#13630)

This commit is contained in:
Ethan Shen 2023-10-25 01:17:10 +08:00 committed by GitHub
parent cd5c87fee6
commit c3ffb3cc63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 26 deletions

View File

@ -6,33 +6,34 @@ const { parseDate } = require('@/utils/parse-date');
const { isValidHost } = require('@/utils/valid-host');
module.exports = async (ctx) => {
const site = ctx.params[0] ?? 'www';
let category = ctx.params[1] ?? (site === 'www' ? '59476' : '');
const { site = 'www' } = ctx.params;
let { category = site === 'www' ? '59476' : '' } = ctx.params;
category = site === 'cpc' && category === '24h' ? '87228' : category;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30;
if (!isValidHost(site)) {
throw Error('Invalid site');
}
const rootUrl = `http://${site}.people.com.cn`;
const currentUrl = `${rootUrl}/GB/${category}/index.html`;
const currentUrl = new URL(`GB/${category}`, rootUrl).href;
const response = await got({
method: 'get',
url: currentUrl,
const { data: response } = await got(currentUrl, {
responseType: 'buffer',
});
const $ = cheerio.load(iconv.decode(response.data, 'gbk'));
const $ = cheerio.load(iconv.decode(response, 'gbk'));
$('em').remove();
$('.bshare-more, .page_n, .page').remove();
$('a img').each(function () {
$(this).parent().remove();
$('a img, h3 img').each((_, e) => {
$(e).parent().remove();
});
let items = $('.p6, div.p2j_list, div.headingNews, div.ej_list_box, .fl')
let items = $('.p6, div.p2j_list, div.headingNews, div.ej_list_box, .fl, .leftItem')
.find('a')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);
@ -41,7 +42,7 @@ module.exports = async (ctx) => {
return {
title: item.text(),
link: link.indexOf('http') === 0 ? link : `${rootUrl}${link.replace(/^\.\./, '')}`,
link: link.indexOf('http') === 0 ? link : new URL(link.replace(/^\.\./, ''), rootUrl).href,
};
});
@ -49,27 +50,17 @@ module.exports = async (ctx) => {
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
try {
const detailResponse = await got({
method: 'get',
url: item.link,
const { data: detailResponse } = await got(item.link, {
responseType: 'buffer',
});
const data = iconv.decode(detailResponse.data, 'gbk');
const data = iconv.decode(detailResponse, 'gbk');
const content = cheerio.load(data);
content('.paper_num, #rwb_tjyd').remove();
item.description = content('.rm_txt_con, .show_text').html();
item.pubDate = timezone(
parseDate(
data
.match(/(\d{4}年\d{2}月\d{2}日\d{2}:\d{2})/)[1]
.replace(/年|月/g, '-')
.replace(/日/, ' ')
),
+8
);
item.pubDate = timezone(parseDate(data.match(/(\d{4}年\d{2}月\d{2}日\d{2}:\d{2})/)[1], 'YYYY年MM月DD日 HH:mm'), +8);
} catch (err) {
item.description = err;
}

View File

@ -1,6 +1,5 @@
module.exports = function (router) {
router.get('/liuyan/:id/:state?', require('./liuyan'));
router.get('/xjpjh/:keyword?/:year?', require('./xjpjh'));
router.get(/([\w\d]+)\/([\w\d/]+)?/, require('./index'));
router.get('/:0?', require('./index'));
router.get('/:site?/:category*', require('./'));
};