diff --git a/docs/bbs.md b/docs/bbs.md index deb4ae59e0..847425b728 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -4,6 +4,12 @@ pageClass: routes # 论坛 +## Chiphell + +### 子版块 + + + ## Dcard ### 首頁帖子 diff --git a/lib/router.js b/lib/router.js index 50c19b3de2..4d6b8d9567 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2348,4 +2348,7 @@ router.get('/csc/notice/:type?', require('./routes/csc/notice')); // LearnKu router.get('/learnku/:community/:category?', require('./routes/learnku/topic')); +// Chiphell +router.get('/chiphell/forum/:forumId?', require('./routes/chiphell/forum')); + module.exports = router; diff --git a/lib/routes/chiphell/forum.js b/lib/routes/chiphell/forum.js new file mode 100644 index 0000000000..d51ca2eccf --- /dev/null +++ b/lib/routes/chiphell/forum.js @@ -0,0 +1,82 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const parser = require('@/utils/rss-parser'); + +module.exports = async (ctx) => { + const forumId = ctx.params.forumId; + + const feed = await parser.parseURL(`https://www.chiphell.com/forum.php?mod=rss&fid=${forumId}`); + const items = await Promise.all( + feed.items.map(async (item) => { + const cache = await ctx.cache.get(item.link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got({ + method: 'get', + url: item.link, + }); + + const html = response.data; + const $ = cheerio.load(html); + const description = $('div[class="pcb"]').first(); + const imgNode = $(description).find('img'); + + // replace placeholer image url + imgNode.each((index, element) => { + if ( + $(element).attr('src') && + $(element) + .attr('src') + .indexOf('none.gif') !== -1 + ) { + $(element).attr('src', $(element).attr('zoomfile')); + } + + // remove image size limit + $(element).attr('width', null); + $(element).attr('height', null); + }); + + // remove image tips + $(description) + .find('div[class*="aimg_tip"]') + .remove(); + $(description) + .find('div[class*="tip"]') + .remove(); + $(description) + .find('p[class="mbn"]') + .remove(); + + // remove rate infomation + $(description) + .find('dl[class="rate"]') + .remove(); + $(description) + .find('h3[class*="psth"]') + .remove(); + + const single = { + title: item.title, + description: description.html(), + pubDate: item.pubDate, + link: item.link, + author: item.author, + }; + ctx.cache.set(item.link, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + let title = feed.title.split('-'); + title = `${title[title.length - 1]} - Chiphell`; + + ctx.state.data = { + title: title, + link: feed.link, + description: feed.description, + item: items, + }; +};