From b9d59de81af450e4118996159dcd083451ae830f Mon Sep 17 00:00:00 2001 From: Enoch Ma Date: Wed, 23 Nov 2022 17:42:36 +0100 Subject: [PATCH] fix(route): twreporter (category) (#11243) * Update topics.js * update doc(cn) for nautilus * update doc(en) for nautilus * refactor: use cache.tryGet * refactor: migrate to v2 * Update category.js * Revert "refactor:" This reverts commit 963d065833e5877d5200c3b07502b9ecc09eb9e6. * Revert "refactor: use cache.tryGet" This reverts commit 879bc58b8c917a144ac3670d3f2bc294e955bb60. * Revert "update doc(en) for nautilus" This reverts commit 41ce3f8f5637f5b8aaa211d8f169fb996b7834f4. * Revert "update doc(cn) for nautilus" This reverts commit 74458c78b5ac5df2aeb219d202c6e7a75d172e30. * Revert "Update topics.js" This reverts commit 988c443264189707d75645541c1be8ff01131f03. --- lib/v2/twreporter/category.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/v2/twreporter/category.js b/lib/v2/twreporter/category.js index 0947f5422..2616c26d6 100644 --- a/lib/v2/twreporter/category.js +++ b/lib/v2/twreporter/category.js @@ -8,16 +8,25 @@ module.exports = async (ctx) => { const url = baseURL + `/categories/${ctx.params.cid}`; const res = await got(url); const $ = cheerio.load(res.data); - const list = $('.lnKPLr').get(); - const category = $('.kCfkTU').text(); + const regexp = /^window\.__REDUX_STATE__=(.*);$/gm; + const raw = $('script[charset="UTF-8"]').html().replace(regexp, '$1'); + const list = JSON.parse(raw); + const ids = list.entities.posts.allIds; + + const links = []; + const titles = []; + const category = list.entities.posts.byId[ids[0]].categories[0].name; + + for (const id of ids) { + links.push(baseURL + '/a/' + list.entities.posts.byId[id].slug); + titles.push(list.entities.posts.byId[id].title); + } const out = await Promise.all( - list.map((item) => { - const $ = cheerio.load(item); - const address = baseURL + $('a').attr('href'); - const title = $('.list-item__Title-sc-1dx5lew-5').text(); - return ctx.cache.tryGet(address, async () => { - const single = await fetch(address); + links.map((item, index) => { + const title = titles[index]; + return ctx.cache.tryGet(item, async () => { + const single = await fetch(item); single.title = title; return single; });