From 46d8d6a7dd254ee46e0fbda16d218d55a77eff8f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 10 Oct 2020 21:30:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E5=85=AB=E9=98=95=E5=B9=BF?= =?UTF-8?q?=E8=A7=92=E6=96=B0=E9=97=BB=20(#5846)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Henry Wang --- docs/new-media.md | 12 +++++ lib/router.js | 3 ++ lib/routes/popyard/index.js | 95 +++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 lib/routes/popyard/index.js diff --git a/docs/new-media.md b/docs/new-media.md index 28629a097..b681d773a 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -483,6 +483,18 @@ Supported sub-sites: +## 八阕 + +### 广角新闻 + + + +| 全景 | 中国 | 国际 | 科教 | 军事 | 体育 | 娱乐 | 艺术 | 文史 | 观点 | 生活 | 产经 | 其它 | +| ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | +| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | + + + ## 白鲸出海 ### 首页最新帖子 diff --git a/lib/router.js b/lib/router.js index 0938c6e31..60bbfe4b4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3332,6 +3332,9 @@ router.get('/npc/:caty', require('./routes/npc/index')); // 高科技行业门户 router.get('/ofweek/news', require('./routes/ofweek/news')); +// 八阕 +router.get('/popyard/:caty?', require('./routes/popyard/index')); + // 原神 router.get('/yuanshen/:location?/:category?', require('./routes/yuanshen/index')); diff --git a/lib/routes/popyard/index.js b/lib/routes/popyard/index.js new file mode 100644 index 000000000..22e623096 --- /dev/null +++ b/lib/routes/popyard/index.js @@ -0,0 +1,95 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + ctx.params.caty = ctx.params.caty || '0'; + + const rootUrl = 'https://www.popyard.com'; + const currentUrl = `${rootUrl}/cgi-mod/threads.cgi?cate=${ctx.params.caty}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(response.data); + + const list = $('li a') + .slice(0, 15) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: `${rootUrl}/cgi-mod${item.attr('href').replace('.', '')}`, + author: item.next().text().split('[')[0], + pubDate: new Date( + item + .next() + .text() + .match(/\[(.*)\]/)[1] + 'GMT+8' + ).toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + headers: { + Referer: currentUrl, + }, + }); + const content = cheerio.load(detailResponse.data); + + const description = content('#sponser').parent(); + + const pages = description.parents('p').eq(0).next('p').find('font a'); + + const subPages = [], + pageLinks = []; + + pages.each(function () { + pageLinks.push(`${rootUrl}/cgi-mod${content(this).attr('href').replace('.', '')}`); + }); + + if (pages.length > 0) { + pageLinks.forEach( + async (link) => + await ctx.cache.tryGet(link, async () => { + const pageResponse = await got({ + method: 'get', + url: link, + headers: { + Referer: currentUrl, + }, + }); + const subContent = cheerio.load(pageResponse.data); + + const subDescription = subContent('#sponser').parent(); + + subContent('#sponser').prev('table').remove(); + subContent('#sponser').remove(); + + subPages.push(subDescription.html()); + }) + ); + } + + content('#sponser').prev('table').remove(); + content('#sponser').remove(); + + item.description = description.html() + subPages.join(''); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};