From cbbb643a517b4897c3aeed1494ad4f6199af6080 Mon Sep 17 00:00:00 2001 From: Millium Orion <664834952@qq.com> Date: Tue, 8 Jul 2025 04:10:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(route/reuters):=20=E4=BF=AE=E5=A4=8Dreuters?= =?UTF-8?q?=E5=9B=A0=E4=B8=BAua=E6=8A=A5=E9=94=99401=20(#19554)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复reuters因为ua报错401 * reuters使用配置中的ua * fix issue "Code scanning results / ESLint" --------- Co-authored-by: zhaorongsen --- lib/routes/reuters/common.ts | 89 +++++++++++++++++------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/lib/routes/reuters/common.ts b/lib/routes/reuters/common.ts index 218cd0065..e674e24bd 100644 --- a/lib/routes/reuters/common.ts +++ b/lib/routes/reuters/common.ts @@ -4,7 +4,6 @@ import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; -import randUserAgent from '@/utils/rand-user-agent'; import { art } from '@/utils/render'; import path from 'node:path'; @@ -90,9 +89,7 @@ async function handler(ctx) { const section_id = `/${category}/${topic ? `${topic}/` : ''}`; - const ua = randUserAgent({ browser: 'chrome', os: 'windows', device: 'desktop' }); const browserHeaders = { - 'User-Agent': ua, Accept: 'application/json, text/plain, */*', 'Accept-Language': 'en-US,en;q=0.9', Referer: 'https://www.reuters.com/', @@ -131,10 +128,10 @@ async function handler(ctx) { website: 'reuters', ...(useSophi ? { - fetch_type: 'sophi', - sophi_page: '*', - sophi_widget: 'topic', - } + fetch_type: 'sophi', + sophi_page: '*', + sophi_widget: 'topic', + } : {}), }), }, @@ -166,55 +163,55 @@ async function handler(ctx) { items.map((item) => ctx.req.query('fulltext') === 'true' ? cache.tryGet(item.link, async () => { - const detailResponse = await ofetch(item.link, { - headers: browserHeaders, - }); - const content = load(detailResponse.data); + const detailResponse = await ofetch(item.link, { + headers: browserHeaders, + }); + const content = load(detailResponse.data); - if (detailResponse.url.startsWith('https://www.reuters.com/investigates/')) { - const ldJson = JSON.parse(content('script[type="application/ld+json"]').text()); - content('.special-report-article-container .container, #slide-dek, #slide-end, .share-in-article-container').remove(); + if (detailResponse.url.startsWith('https://www.reuters.com/investigates/')) { + const ldJson = JSON.parse(content('script[type="application/ld+json"]').text()); + content('.special-report-article-container .container, #slide-dek, #slide-end, .share-in-article-container').remove(); - item.title = ldJson.headline; - item.pubDate = parseDate(ldJson.dateCreated); - item.author = ldJson.creator; - item.category = ldJson.keywords; - item.description = content('.special-report-article-container').html(); + item.title = ldJson.headline; + item.pubDate = parseDate(ldJson.dateCreated); + item.author = ldJson.creator; + item.category = ldJson.keywords; + item.description = content('.special-report-article-container').html(); - return item; - } + return item; + } - const matches = content('script#fusion-metadata') - .text() - .match(/Fusion.globalContent=({[\S\s]*?});/); + const matches = content('script#fusion-metadata') + .text() + .match(/Fusion.globalContent=({[\S\s]*?});/); - if (matches) { - const data = JSON.parse(matches[1]); + if (matches) { + const data = JSON.parse(matches[1]); - item.title = data.result.title || item.title; - item.description = art(path.join(__dirname, 'templates/description.art'), { - result: data.result, - }); - item.pubDate = parseDate(data.result.display_time); - item.author = data.result.authors.map((author) => author.name).join(', '); - item.category = data.result.taxonomy.keywords; + item.title = data.result.title || item.title; + item.description = art(path.join(__dirname, 'templates/description.art'), { + result: data.result, + }); + item.pubDate = parseDate(data.result.display_time); + item.author = data.result.authors.map((author) => author.name).join(', '); + item.category = data.result.taxonomy.keywords; - return item; - } + return item; + } - content('.title').remove(); - content('.article-metadata').remove(); + content('.title').remove(); + content('.article-metadata').remove(); - item.title = content('meta[property="og:title"]').attr('content'); - item.pubDate = parseDate(detailResponse.data.match(/"datePublished":"(.*?)","dateModified/)[1]); - item.author = detailResponse.data - .match(/{"@type":"Person","name":"(.*?)"}/g) - .map((p) => p.match(/"name":"(.*?)"/)[1]) - .join(', '); - item.description = content('article').html(); + item.title = content('meta[property="og:title"]').attr('content'); + item.pubDate = parseDate(detailResponse.data.match(/"datePublished":"(.*?)","dateModified/)[1]); + item.author = detailResponse.data + .match(/{"@type":"Person","name":"(.*?)"}/g) + .map((p) => p.match(/"name":"(.*?)"/)[1]) + .join(', '); + item.description = content('article').html(); - return item; - }) + return item; + }) : item ) );