fix(route/reuters): 修复reuters因为ua报错401 (#19554)
* 修复reuters因为ua报错401 * reuters使用配置中的ua * fix issue "Code scanning results / ESLint" --------- Co-authored-by: zhaorongsen <zhaorongsen2014@xiaochuankeji.cn>
This commit is contained in:
co-authored by
zhaorongsen
parent
1c4bd0a0c3
commit
cbbb643a51
@@ -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
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user