From e4166133c8c59998ad51e9144d6fee75d04c3d5e Mon Sep 17 00:00:00 2001 From: Logan Rockmore Date: Wed, 27 Sep 2023 10:12:39 -0400 Subject: [PATCH] fix(route): added some error checking to the GrubStreet RSS feed that fixes an error when there aren't any authors listed for an article. (#13417) * Added some error checking to the GrubStreet RSS feed that fixes an error when there aren't any authors listed for an article. * refactor: migrate to v2 --------- --- lib/router.js | 2 +- lib/routes/grubstreet/utils.js | 78 ------------------------ lib/{routes => v2}/grubstreet/index.js | 0 lib/v2/grubstreet/maintainer.js | 4 ++ lib/v2/grubstreet/radar.js | 13 ++++ lib/v2/grubstreet/router.js | 3 + lib/v2/grubstreet/utils.js | 84 ++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 79 deletions(-) delete mode 100644 lib/routes/grubstreet/utils.js rename lib/{routes => v2}/grubstreet/index.js (100%) create mode 100644 lib/v2/grubstreet/maintainer.js create mode 100644 lib/v2/grubstreet/radar.js create mode 100644 lib/v2/grubstreet/router.js create mode 100644 lib/v2/grubstreet/utils.js diff --git a/lib/router.js b/lib/router.js index bf397485b..2b5bf36a2 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1732,7 +1732,7 @@ router.get('/jijitang/article/:id', lazyloadRouteHandler('./routes/jijitang/arti router.get('/jijitang/publication', lazyloadRouteHandler('./routes/jijitang/publication')); // Grub Street -router.get('/grubstreet', lazyloadRouteHandler('./routes/grubstreet/index')); +// router.get('/grubstreet', lazyloadRouteHandler('./routes/grubstreet/index')); // 漫画堆 router.get('/manhuadui/manhua/:name/:serial?', lazyloadRouteHandler('./routes/manhuadui/manhua')); diff --git a/lib/routes/grubstreet/utils.js b/lib/routes/grubstreet/utils.js deleted file mode 100644 index ab038f481..000000000 --- a/lib/routes/grubstreet/utils.js +++ /dev/null @@ -1,78 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -async function load(link) { - const response = await got.get(link); - const $ = cheerio.load(response.data); - - const description = $('div.article-content'); - - // remove the content that we don't want to show - description.find('aside.related').remove(); - description.find('aside.article-details_heading-with-paragraph').remove(); - description.find('section.package-list').remove(); - description.find('div.source-links h2').remove(); - description.find('div.source-links svg').remove(); - description.find('div.mobile-secondary-area').remove(); - description.find('aside.newsletter-flex-text').remove(); - - return { - description: description.html(), - }; -} - -function ProcessFeed(list, caches) { - return Promise.all( - list.map(async (item) => { - const itemUrl = item.canonicalUrl; - - let bylineString = ''; - if (item.byline) { - const byline = item.byline[0]; - const bylineNames = byline.names.map((name) => name.text); - const bylineNamesString = bylineNames.join(', '); - - bylineString = 'by ' + bylineNamesString; - } - - const single = { - title: item.plaintextPrimaryHeadline, - link: itemUrl, - author: bylineString, - guid: itemUrl, - pubDate: item.date, - }; - - const other = await caches.tryGet(itemUrl, () => load(itemUrl)); - - return Promise.resolve(Object.assign({}, single, other)); - }) - ); -} - -const getData = async (ctx, url, title, description) => { - const response = await got({ - method: 'get', - url, - headers: { - Referer: url, - }, - }); - - const data = response.data; - - // limit the list to only 25 articles, to make sure that load times remain reasonable - const list = data.articles.slice(0, 25); - const result = await ProcessFeed(list, ctx.cache); - - return { - title, - link: url, - description, - item: result, - }; -}; - -module.exports = { - getData, -}; diff --git a/lib/routes/grubstreet/index.js b/lib/v2/grubstreet/index.js similarity index 100% rename from lib/routes/grubstreet/index.js rename to lib/v2/grubstreet/index.js diff --git a/lib/v2/grubstreet/maintainer.js b/lib/v2/grubstreet/maintainer.js new file mode 100644 index 000000000..c3c2c3b89 --- /dev/null +++ b/lib/v2/grubstreet/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '': ['loganrockmore'], + '/': ['loganrockmore'], +}; diff --git a/lib/v2/grubstreet/radar.js b/lib/v2/grubstreet/radar.js new file mode 100644 index 000000000..bae015786 --- /dev/null +++ b/lib/v2/grubstreet/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'grubstreet.com': { + _name: 'Grub Street', + '.': [ + { + title: 'Posts', + docs: 'https://docs.rsshub.app/routes/new-media#grub-street', + source: ['/'], + target: '/grubstreet', + }, + ], + }, +}; diff --git a/lib/v2/grubstreet/router.js b/lib/v2/grubstreet/router.js new file mode 100644 index 000000000..7e427447c --- /dev/null +++ b/lib/v2/grubstreet/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/', require('./index')); +}; diff --git a/lib/v2/grubstreet/utils.js b/lib/v2/grubstreet/utils.js new file mode 100644 index 000000000..7431c256c --- /dev/null +++ b/lib/v2/grubstreet/utils.js @@ -0,0 +1,84 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +async function load(link) { + const response = await got(link); + const $ = cheerio.load(response.data); + + const description = $('div.article-content'); + + // remove the content that we don't want to show + description.find('aside.related').remove(); + description.find('aside.article-details_heading-with-paragraph').remove(); + description.find('section.package-list').remove(); + description.find('div.source-links h2').remove(); + description.find('div.source-links svg').remove(); + description.find('div.mobile-secondary-area').remove(); + description.find('aside.newsletter-flex-text').remove(); + + return { + description: description.html(), + }; +} + +function ProcessFeed(list, caches) { + return Promise.all( + list.map((item) => + caches.tryGet(item.canonicalUrl, async () => { + const itemUrl = item.canonicalUrl.replace('http://', 'https://'); + + let bylineString; + if (item.byline) { + const byline = item.byline[0]; + + if (byline && byline.names) { + const bylineNames = byline.names.map((name) => name.text); + + if (bylineNames && bylineNames.length > 0) { + const bylineNamesString = bylineNames.join(', '); + + bylineString = 'by ' + bylineNamesString; + } + } + } + + const single = { + title: item.plaintextPrimaryHeadline, + link: itemUrl, + guid: itemUrl, + pubDate: item.date, + }; + + if (bylineString) { + single.author = bylineString; + } + + const { description } = await load(itemUrl); + single.description = description; + + return single; + }) + ) + ); +} + +const getData = async (ctx, url, title, description) => { + const response = await got(url); + + const data = response.data; + + // limit the list to only 25 articles, to make sure that load times remain reasonable + const list = data.articles.slice(0, ctx.query.limit ? parseInt(ctx.query.limit, 10) : 25); + const result = await ProcessFeed(list, ctx.cache); + + return { + title, + link: url, + description, + item: result, + }; +}; + +module.exports = { + getData, +};