From a5d0eaeafa343485a97b91a14dd5ac69c57aec17 Mon Sep 17 00:00:00 2001 From: Felix Hsu Date: Sat, 3 Sep 2022 23:47:31 +0800 Subject: [PATCH] fix(route): producthunt rss failure #10688 (#10690) * Fix producthunt rss apolloState prop has been changed from putting the "Post" item first into the third one. * Fix the producthunt misleading '__typename' caused rss problem * refactor: simplify call --- lib/v2/producthunt/today.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/v2/producthunt/today.js b/lib/v2/producthunt/today.js index d96d759afe..0cf40ee0c3 100644 --- a/lib/v2/producthunt/today.js +++ b/lib/v2/producthunt/today.js @@ -5,22 +5,25 @@ const { art } = require('@/utils/render'); const path = require('path'); module.exports = async (ctx) => { - const response = await got.get('https://www.producthunt.com/'); + const response = await got('https://www.producthunt.com/'); const data = JSON.parse(cheerio.load(response.data)('#__NEXT_DATA__').html()); - const list = Object.values(data.props.apolloState).filter((o) => o.__typename === 'Post'); + const list = Object.values(data.props.apolloState) + .filter((o) => o.__typename === 'Post') + // only includes new post, not product + .filter((o) => o.hasOwnProperty('redirectToProduct') && o.redirectToProduct === null); const items = await Promise.all( list.map((item) => ctx.cache.tryGet(item.slug, async () => { - const detailresponse = await got.get(`https://www.producthunt.com/posts/${item.slug}`); + const detailresponse = await got(`https://www.producthunt.com/posts/${item.slug}`); const data = JSON.parse(cheerio.load(detailresponse.data)('#__NEXT_DATA__').html()); - const descData = Object.values(data.props.apolloState)[0]; + const descData = data.props.apolloState[`Post${item.id}`]; return { - title: `${descData.slug} - ${item.tagline}`, + title: `${item.slug} - ${item.tagline}`, description: descData.description + art(path.join(__dirname, 'templates/descImg.art'), {