From e0fb0510474e3cb8cf1d052ad0ff0517e31cb64d Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 5 Nov 2022 01:10:46 +0900 Subject: [PATCH] fix(route): odaily (#11220) --- lib/v2/odaily/post.js | 35 +++++++++++++++-------------------- lib/v2/odaily/utils.js | 2 +- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/v2/odaily/post.js b/lib/v2/odaily/post.js index 073e74e04..6155181ec 100644 --- a/lib/v2/odaily/post.js +++ b/lib/v2/odaily/post.js @@ -20,15 +20,11 @@ module.exports = async (ctx) => { const currentUrl = `${rootUrl}/api/pp/api/app-front/feed-stream?feed_id=${id}&b_id=&per_page=${ctx.query.limit ?? 25}`; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const response = await got(currentUrl); let items = response.data.data.items .map((item) => ({ title: item.title, - author: item.user.name, type: item.entity_type, description: `

${item.summary}

`, link: `${rootUrl}/${item.entity_type}/${item.entity_id}`, @@ -37,26 +33,25 @@ module.exports = async (ctx) => { .filter((item) => item.type !== 'newsflash'); items = await Promise.all( - items.map( - async (item) => - await ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got(item.link); - const content = cheerio.load(detailResponse.data.match(item.type === 'post' ? /"content":"(.*)","extraction_tags":/ : /"description":"(.*?)","cover":/)[1]); + const ssr = JSON.parse(`{${detailResponse.data.match(/window\.__INITIAL_STATE__ = {(.*)}/)[1]}}`); - content('img').each(function () { - content(this).attr('src', content(this).attr('src').replace(/\\"/g, '')); - }); + const content = cheerio.load(ssr.post.detail.content, null, false); + content('img').each((_, img) => { + img.attribs.src = img.attribs.src.split('?x-oss-process')[0]; + img.attribs.src = img.attribs.src.split('!heading')[0]; + }); - item.description = content.html(); + item.description = content.html(); + item.author = ssr.post.user.name; - delete item.type; + delete item.type; - return item; - }) + return item; + }) ) ); diff --git a/lib/v2/odaily/utils.js b/lib/v2/odaily/utils.js index b8914be30..3ff06174a 100644 --- a/lib/v2/odaily/utils.js +++ b/lib/v2/odaily/utils.js @@ -1,3 +1,3 @@ module.exports = { - rootUrl: 'https://www.0daily.com', + rootUrl: 'https://www.odaily.news', };