From b552dad8d766887d85a1de710576b7316972bed5 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sun, 4 Feb 2024 05:42:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=96=B0=E5=8D=8E=E7=BD=91?= =?UTF-8?q?=E6=96=B0=E5=8D=8E=E7=A4=BE=E6=96=B0=E9=97=BB=20(#14390)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/v2/news/maintainer.js | 2 +- lib/v2/news/radar.js | 4 +- lib/v2/news/router.js | 3 +- lib/v2/news/templates/description.art | 21 +++++++ lib/v2/news/whxw.js | 68 ---------------------- lib/v2/news/xhsxw.js | 81 +++++++++++++++++++++++++++ website/docs/routes/new-media.mdx | 2 +- 7 files changed, 108 insertions(+), 73 deletions(-) create mode 100644 lib/v2/news/templates/description.art delete mode 100644 lib/v2/news/whxw.js create mode 100644 lib/v2/news/xhsxw.js diff --git a/lib/v2/news/maintainer.js b/lib/v2/news/maintainer.js index 4aa7f5af7..daf4d0b56 100644 --- a/lib/v2/news/maintainer.js +++ b/lib/v2/news/maintainer.js @@ -1,3 +1,3 @@ module.exports = { - '/whxw': ['nczitzk'], + '/xhsxw': ['nczitzk'], }; diff --git a/lib/v2/news/radar.js b/lib/v2/news/radar.js index cb756f4e8..33a97e2de 100644 --- a/lib/v2/news/radar.js +++ b/lib/v2/news/radar.js @@ -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', }, ], }, diff --git a/lib/v2/news/router.js b/lib/v2/news/router.js index 00ce0528e..212ac9817 100644 --- a/lib/v2/news/router.js +++ b/lib/v2/news/router.js @@ -1,3 +1,4 @@ module.exports = function (router) { - router.get('/whxw', require('./whxw')); + router.get('/xhsxw', require('./xhsxw')); + router.get('/whxw', require('./xhsxw')); }; diff --git a/lib/v2/news/templates/description.art b/lib/v2/news/templates/description.art new file mode 100644 index 000000000..249654e7e --- /dev/null +++ b/lib/v2/news/templates/description.art @@ -0,0 +1,21 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} + +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file diff --git a/lib/v2/news/whxw.js b/lib/v2/news/whxw.js deleted file mode 100644 index a2ca320a2..000000000 --- a/lib/v2/news/whxw.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/v2/news/xhsxw.js b/lib/v2/news/xhsxw.js new file mode 100644 index 000000000..cc41e7af3 --- /dev/null +++ b/lib/v2/news/xhsxw.js @@ -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, + }; +}; diff --git a/website/docs/routes/new-media.mdx b/website/docs/routes/new-media.mdx index 12e4f7b7d..d95d8d02a 100644 --- a/website/docs/routes/new-media.mdx +++ b/website/docs/routes/new-media.mdx @@ -4774,7 +4774,7 @@ ### 新华社新闻 {#xin-hua-wang-xin-hua-she-xin-wen} - + ## 新浪 {#xin-lang}