From a8058f292167a03f3d064376d5741fcc0c671ff1 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 4 Jul 2023 00:45:26 -1200 Subject: [PATCH] fix(route): mingpao keep redirecting (#12753) --- lib/v2/mingpao/index.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/v2/mingpao/index.js b/lib/v2/mingpao/index.js index 7b82c3a79..043dafbb1 100644 --- a/lib/v2/mingpao/index.js +++ b/lib/v2/mingpao/index.js @@ -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; })