fix(route): mingpao keep redirecting (#12753)

This commit is contained in:
Tony 2023-07-04 00:45:26 -12:00 committed by GitHub
parent c3d3374b5e
commit a8058f2921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 6 deletions

View File

@ -4,6 +4,7 @@ const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
const logger = require('@/utils/logger');
const renderFanBox = (media) =>
art(path.join(__dirname, 'templates/fancybox.art'), {
@ -26,11 +27,20 @@ module.exports = async (ctx) => {
const items = await Promise.all(
feed.items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got(item.link, {
headers: {
Referer: 'https://news.mingpao.com/',
},
});
let response;
try {
response = await got(item.link, {
headers: {
Referer: 'https://news.mingpao.com/',
},
});
} catch (e) {
if (e instanceof got.MaxRedirectsError) {
logger.error(`MaxRedirectsError when requesting ${decodeURIComponent(item.link)}`);
return item;
}
throw e;
}
const $ = cheerio.load(response.data);
const fancyboxImg = $('a.fancybox').length ? $('a.fancybox') : $('a.fancybox-buttons');
@ -71,7 +81,7 @@ module.exports = async (ctx) => {
item.description = renderDesc(fancybox, $('.txt4').html() ?? $('.article_content.line_1_5em').html());
item.pubDate = parseDate(item.pubDate);
item.guid = item.link.indexOf('?') > 0 ? item.link : item.link.substring(0, item.link.lastIndexOf('/'));
item.guid = item.link.includes('?') ? item.link : item.link.substring(0, item.link.lastIndexOf('/'));
return item;
})