fix(route): 新华网新华社新闻 (#14390)

This commit is contained in:
Ethan Shen 2024-02-04 05:42:43 +08:00 committed by GitHub
parent 64c0c47d44
commit b552dad8d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 108 additions and 73 deletions

View File

@ -1,3 +1,3 @@
module.exports = {
'/whxw': ['nczitzk'],
'/xhsxw': ['nczitzk'],
};

View File

@ -5,8 +5,8 @@ module.exports = {
{
title: '新华社新闻',
docs: 'https://docs.rsshub.app/routes/new-media#xin-hua-wang-xin-hua-she-xin-wen',
source: ['/'],
target: '/news/whxw',
source: ['/xhsxw.htm'],
target: '/news/xhsxw',
},
],
},

View File

@ -1,3 +1,4 @@
module.exports = function (router) {
router.get('/whxw', require('./whxw'));
router.get('/xhsxw', require('./xhsxw'));
router.get('/whxw', require('./xhsxw'));
};

View File

@ -0,0 +1,21 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}
{{ if intro }}
<blockquote>{{ intro }}</blockquote>
{{ /if }}
{{ if description }}
{{@ description }}
{{ /if }}

View File

@ -1,68 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const rootUrl = 'http://www.news.cn';
const currentUrl = `${rootUrl}/whxw.htm`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
let items = $('h3 a')
.toArray()
.slice(0, ctx.query.limit ? Number.parseInt(ctx.query.limit) : 100)
.map((item) => {
item = $(item);
return {
title: item.text(),
link: item.attr('href'),
};
});
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
let detailResponse;
try {
detailResponse = await got(item.link);
} catch {
item.status = 404;
}
if (item.status !== 404) {
const content = cheerio.load(detailResponse.data);
const date = content('.header-time.left').text() || content('.h-time').text() || content('.info').text();
const author =
content('.editor').text() ??
content('.p-jc')
.text()
.replaceAll(/[\n\r]/g, '')
.replace(' ', ':')
.trim();
item.author = author.split(':').pop().replace('】', '');
content('#articleEdit').remove();
item.description = content('#detail').html();
item.pubDate = timezone(parseDate(date, date.includes('/') ? 'YYYYMM/DD HH:mm:ss' : 'YYYY-MM-DD HH:mm:ss'), +8);
}
delete item.status;
return item;
})
)
);
ctx.state.data = {
title: $('title').text(),
link: currentUrl,
item: items,
};
};

81
lib/v2/news/xhsxw.js Normal file
View File

@ -0,0 +1,81 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const limit = ctx.query.limit ? Number.parseInt(ctx.query.limit, 10) : 100;
const rootUrl = 'http://www.news.cn';
const currentUrl = new URL('xhsxw.htm', rootUrl).href;
const { data: currentResponse } = await got(currentUrl);
const $ = cheerio.load(currentResponse);
const id = $('ul.wz-list')
.prop('data')
.replace(/datasource:/, '');
const apiUrl = new URL(`ds_${id}.json`, rootUrl).href;
const {
data: { datasource: response },
} = await got(apiUrl);
let items = response.slice(0, limit).map((item) => ({
title: item.title,
link: new URL(item.publishUrl, rootUrl).href,
description: art(path.join(__dirname, 'templates/description.art'), {
images:
item.shareImages?.map((i) => ({
src: i.imageUrl,
alt: item.title,
})) ?? undefined,
intro: item.summary,
}),
author: item.author,
category: item.keywords.split(/-|,/),
guid: `news-${item.contentId}`,
pubDate: timezone(parseDate(item.publishTime), +8),
}));
items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
try {
const { data: detailResponse } = await got(item.link);
const content = cheerio.load(detailResponse);
item.description += art(path.join(__dirname, 'templates/description.art'), {
description: content('#detailContent').html(),
});
} catch {
// no-empty
}
return item;
})
)
);
const title = $('title').text();
const image = new URL('20141223_xhsxw_logo_v1.png', rootUrl).href;
const icon = new URL('favicon.ico', rootUrl).href;
ctx.state.data = {
item: items,
title,
link: currentUrl,
description: title.split(/_/)[0],
language: 'zh',
image,
icon,
logo: icon,
author: title.split(/_/).pop(),
allowEmpty: true,
};
};

View File

@ -4774,7 +4774,7 @@
### 新华社新闻 {#xin-hua-wang-xin-hua-she-xin-wen}
<Route author="nczitzk" example="/news/whxw" path="/news/whxw" />
<Route author="nczitzk" example="/news/xhsxw" path="/news/xhsxw" radar="1" />
## 新浪 {#xin-lang}