From a8b2d68228bd19417d56d020071276be6451a8a2 Mon Sep 17 00:00:00 2001 From: Li Fangqiao <124950516+Ch1llNoodle@users.noreply.github.com> Date: Sat, 12 Apr 2025 21:42:01 +0800 Subject: [PATCH] fix(route): resolve issue #18517 by updating headers for Reuters API (#18828) --- lib/routes/reuters/common.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/routes/reuters/common.ts b/lib/routes/reuters/common.ts index 908ea65b2..22c9bab3b 100644 --- a/lib/routes/reuters/common.ts +++ b/lib/routes/reuters/common.ts @@ -6,6 +6,7 @@ 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'; @@ -91,6 +92,14 @@ 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/', + }; + try { const { title, description, rootUrl, response } = await (async () => { if (MUST_FETCH_BY_TOPICS.has(category)) { @@ -104,6 +113,7 @@ async function handler(ctx) { website: 'reuters', }), }, + headers: browserHeaders, }); return { @@ -130,6 +140,7 @@ async function handler(ctx) { : {}), }), }, + headers: browserHeaders, }); return { title: response.result.section.title, @@ -157,7 +168,9 @@ async function handler(ctx) { items.map((item) => ctx.req.query('fulltext') === 'true' ? cache.tryGet(item.link, async () => { - const detailResponse = await ofetch(item.link); + const detailResponse = await ofetch(item.link, { + headers: browserHeaders, + }); const content = load(detailResponse.data); if (detailResponse.url.startsWith('https://www.reuters.com/investigates/')) { @@ -220,7 +233,9 @@ async function handler(ctx) { // Fallback to arc outboundfeeds if API fails const arcUrl = topic ? `https://www.reuters.com/arc/outboundfeeds/v4/mobile/section${section_id}?outputType=json` : `https://www.reuters.com/arc/outboundfeeds/v4/mobile/section/${category}/?outputType=json`; - const arcResponse = await ofetch(arcUrl); + const arcResponse = await ofetch(arcUrl, { + headers: browserHeaders, + }); if (arcResponse.wireitems?.length) { const items = arcResponse.wireitems .map((item) => {