From bd6effd2e2e8c0986b331083f443b0d5fe50c9be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=AE=E3=83=A3=E3=83=A9?= Date: Mon, 28 Dec 2020 09:08:57 +0000 Subject: [PATCH] fix --- lib/routes/mediadigest/category.js | 34 +++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/routes/mediadigest/category.js b/lib/routes/mediadigest/category.js index d52214530..b42a27f64 100644 --- a/lib/routes/mediadigest/category.js +++ b/lib/routes/mediadigest/category.js @@ -1,7 +1,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -function getArticle(line) { +function getArticle(ctx, line, tasks) { const article = ctx.cache.tryGet(line, async () => { const a_r = await got.get(`https://app3.rthk.hk/mediadigest/${line}`); const $ = cheerio.load(a_r.data); @@ -11,7 +11,7 @@ function getArticle(line) { const author_list = $('div.story-author'); const authors = author_list.map((_index, author) => $(author).text()); const author = authors.map((_index, author) => `${author}
`); - const s_author = author.toArray().join(""); + const s_author = author.toArray().join(''); const author_block = `

${s_author}

`; // date const date = $('div.story-calendar').text(); @@ -37,27 +37,37 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); const list = $('div.category-line'); - list.map((_index, line) => $(line).find('a').first().attr('href')) - .each((_index, line) => { - getArticle(line); - }); + list.map((_index, line) => $(line).find('a').first().attr('href')).each((_index, line) => { + getArticle(ctx, line, tasks); + }); } else { - let cids = [1, 2]; for (let i = 4; i < 28; ++i) {cids.push(i);}; - let urls = cids.map((cid) => `https://app3.rthk.hk/mediadigest/category.php?cid=${cid}`); + const cids = [1, 2]; + for (let i = 4; i < 28; ++i) { + cids.push(i); + } + const urls = cids.map((cid) => `https://app3.rthk.hk/mediadigest/category.php?cid=${cid}`); const list_allt = await Promise.all( urls.map(async (url) => { const response = await got.get(url); const $ = cheerio.load(response.data); - const list = $('div.category-line').map((_index, line) => $(line).find('a').first().attr('href')); + const list = $('div.category-line').map((_index, line) => $(line).find('a').first().attr('href')); return Promise.resolve(list.toArray()); }) ); const list_all = [...new Set(list_allt.flat())]; - list_all.forEach((line) => { - getArticle(line); + const list_recent_20 = list_all + .sort((a, b) => { + const aid_a = a.match(/aid=(\d+)/)[1]; + const aid_b = b.match(/aid=(\d+)/)[1]; + // reverse + return aid_b - aid_a; + }) + .slice(0, 20); + list_recent_20.forEach((line) => { + getArticle(ctx, line, tasks); }); - }; + } const rss = await Promise.all(tasks);