From dc8cf0f6b0f4d4ad80fbd90eef23bcf7134c5e10 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 15:40:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E9=A6=99=E6=B8=AF?= =?UTF-8?q?=E8=A8=8E=E8=AB=96=E5=8D=80=20(#8101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DIYgod --- docs/new-media.md | 6 ++++ lib/router.js | 3 ++ lib/routes/discuss/index.js | 71 +++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 lib/routes/discuss/index.js diff --git a/docs/new-media.md b/docs/new-media.md index adde987a62..7a481a9827 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2644,6 +2644,12 @@ column 为 third 时可选的 category: +## 香港討論區 + +### 版塊 + + + ## 香水时代 ### 首页 diff --git a/lib/router.js b/lib/router.js index 6a6ffec5de..e93233cf5c 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4210,6 +4210,9 @@ router.get('/right/forum/:id?', lazyloadRouteHandler('./routes/right/forum')); // 香港經濟日報 router.get('/hket/:category?', lazyloadRouteHandler('./routes/hket/index')); +// 香港討論區 +router.get('/discuss/:fid', lazyloadRouteHandler('./routes/discuss')); + // Uwants router.get('/uwants/:fid', lazyloadRouteHandler('./routes/uwants')); diff --git a/lib/routes/discuss/index.js b/lib/routes/discuss/index.js new file mode 100644 index 0000000000..fbbc8e2129 --- /dev/null +++ b/lib/routes/discuss/index.js @@ -0,0 +1,71 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const fid = ctx.params.fid; + + const rootUrl = 'https://www.discuss.com.hk'; + const currentUrl = `${rootUrl}/archiver/?fid-${fid}.html`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('li a') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 15) + .map((_, item) => { + item = $(item); + + return { + title: item.text(), + link: `${rootUrl}/${item.attr('href')}`, + }; + }) + .get(); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const data = detailResponse.data + .replace(/\[img\]https:\/\/www\.discuss\.com\.hk\/images\/common\/back\.gif\[\/img\]/g, '') + .replace(/\[url=(.*?)\]/gm, '') + .replace(/\[(\w+)=(.*?)\]/gm, '') + .replace(/\[\/(color|size)\]/gm, '') + .replace(/\[\/url\]/gm, '') + .replace(/\[(\w+)\]/gm, '<$1>') + .replace(/\[\/(\w+)\]/gm, ''); + + const content = cheerio.load(data); + + const cite = content('cite'); + item.author = cite.eq(0).text(); + const firstAuthorParent = cite.eq(0).parent(); + + item.description = content('.archiver_post').html(); + + cite.remove(); + + item.link = `${rootUrl}/viewthread.php?tid=${item.link.match(/tid-(\d+)\.html/)[1]}`; + item.pubDate = timezone(parseDate(firstAuthorParent.text().trim(), 'YYYY-M-D hh:mm A'), +8); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${$('h1 a').text().replace('查看完整版本 : ', '')} - 香港討論區`, + link: `${rootUrl}/forumdisplay.php?fid=${fid}`, + item: items, + }; +};