From 269e8bcdc97f455060d28a0bdb657c208d94f464 Mon Sep 17 00:00:00 2001 From: Shelken <33972006+shelken@users.noreply.github.com> Date: Mon, 8 Aug 2022 00:19:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=BA=8C=E6=AC=A1?= =?UTF-8?q?=E5=85=83=E8=99=AB=E6=B4=9E=20(#10406)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 二次元虫洞 * refactor(radar): 完善二次元虫洞radar * Apply suggestions from code review 规范代码 Co-authored-by: Tony * style: auto format * fix(route): 增加时间解析 Co-authored-by: Tony * fix(route): 引入时间解析方法 Co-authored-by: shelken Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- docs/bbs.md | 20 +++++++++++++ lib/v2/2cycd/index.js | 57 ++++++++++++++++++++++++++++++++++++++ lib/v2/2cycd/maintainer.js | 3 ++ lib/v2/2cycd/radar.js | 25 +++++++++++++++++ lib/v2/2cycd/router.js | 3 ++ 5 files changed, 108 insertions(+) create mode 100644 lib/v2/2cycd/index.js create mode 100644 lib/v2/2cycd/maintainer.js create mode 100644 lib/v2/2cycd/radar.js create mode 100644 lib/v2/2cycd/router.js diff --git a/docs/bbs.md b/docs/bbs.md index 5b4cbaa15..39a318700 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -434,6 +434,26 @@ pageClass: routes +## 二次元虫洞 + +### 板块 + + + +板块(更多板块请自行 [查看](http://www.2cycd.com)) + +| 音乐下载(默认) | 动漫下载 | 游戏下载 | +| -------- | ---- | ---- | +| 43 | 53 | 42 | + +排序 + +| 发布时间排序(默认) | 回复/查看 | 查看 | +| ---------- | ------- | ----- | +| dateline | replies | views | + + + ## 光谷社区 ### 子论坛 diff --git a/lib/v2/2cycd/index.js b/lib/v2/2cycd/index.js new file mode 100644 index 000000000..95f7fce36 --- /dev/null +++ b/lib/v2/2cycd/index.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); +const iconv = require('iconv-lite'); + +// http://www.2cycd.com/forum.php?mod=forumdisplay&fid=43&orderby=dateline + +module.exports = async (ctx) => { + const fid = ctx.params.fid ?? '43'; + const sort = ctx.params.sort ?? 'dateline'; + + const rootUrl = 'http://www.2cycd.com/forum.php?mod=forumdisplay'; + const currentUrl = `${rootUrl}&fid=${fid}&orderby=${sort}`; + + const response = await got(currentUrl, { + responseType: 'buffer', + }); + + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + + const list = $('tbody[id^="normalthread_"]') + .map((_, item) => { + item = $(item); + const xst = item.find('a.s.xst'); + const author = item.find('td.by cite a').eq(0).text(); + return { + title: xst.text(), + link: xst.attr('href'), + author, + }; + }) + .get(); + // console.log(list); + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got(item.link, { + responseType: 'buffer', + }); + + const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk')); + const first_post = content('td[id^="postmessage_"]').first(); + const dateobj = content('em[id^="authorposton"]').first(); + item.description = first_post.html(); + item.pubDate = timezone(parseDate(dateobj.find('span').attr('title'), 'YYYY-M-D HH:mm:ss'), +8); + + return item; + }) + ) + ); + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/2cycd/maintainer.js b/lib/v2/2cycd/maintainer.js new file mode 100644 index 000000000..9c62e0b1c --- /dev/null +++ b/lib/v2/2cycd/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:fid/:sort?': ['shelken'], +}; diff --git a/lib/v2/2cycd/radar.js b/lib/v2/2cycd/radar.js new file mode 100644 index 000000000..e6d237ff8 --- /dev/null +++ b/lib/v2/2cycd/radar.js @@ -0,0 +1,25 @@ +module.exports = { + '2cycd.com': { + _name: '二次元虫洞', + '.': [ + { + title: '板块', + docs: 'https://docs.rsshub.app/bbs.html#er-ci-yuan-chong-dong', + source: '/:path', + target: (params, url) => { + let pid, sort; + const static_matched = params.path.match(/forum-(\d+)-\d+.html/); + if (static_matched) { + pid = static_matched[1]; + } else if (params.path === 'forum.php') { + pid = new URL(url).searchParams.get('fid'); + sort = new URL(url).searchParams.get('orderby'); + } else { + return false; + } + return `/2cycd/${pid}/${sort ? sort : 'dateline'}`; + }, + }, + ], + }, +}; diff --git a/lib/v2/2cycd/router.js b/lib/v2/2cycd/router.js new file mode 100644 index 000000000..c9a4b57af --- /dev/null +++ b/lib/v2/2cycd/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/:fid/:sort?', require('./index')); +};