Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/v2/news/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
'/whxw': ['nczitzk'],
'/xhsxw': ['nczitzk'],
};
4 changes: 2 additions & 2 deletions lib/v2/news/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
Expand Down
3 changes: 2 additions & 1 deletion lib/v2/news/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = function (router) {
router.get('/whxw', require('./whxw'));
router.get('/xhsxw', require('./xhsxw'));
router.get('/whxw', require('./xhsxw'));
};
21 changes: 21 additions & 0 deletions lib/v2/news/templates/description.art
Original file line number Diff line number Diff line change
@@ -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 }}
68 changes: 0 additions & 68 deletions lib/v2/news/whxw.js

This file was deleted.

81 changes: 81 additions & 0 deletions lib/v2/news/xhsxw.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
2 changes: 1 addition & 1 deletion website/docs/routes/new-media.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4768,7 +4768,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}

Expand Down
Loading