From 9c9a3cf6cabb94b15fdb7120b5aad5fa89dacf5a Mon Sep 17 00:00:00 2001 From: cssxsh <32539286+cssxsh@users.noreply.github.com> Date: Mon, 21 Oct 2019 12:10:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20MCBBS=20=E8=AE=BA=E5=9D=9B=20(#32?= =?UTF-8?q?94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 10 +++++++ lib/router.js | 4 +++ lib/routes/mcbbs/forum.js | 58 +++++++++++++++++++++++++++++++++++++++ lib/routes/mcbbs/post.js | 56 +++++++++++++++++++++++++++++++++++++ lib/routes/mcbbs/utils.js | 6 ++++ 5 files changed, 134 insertions(+) create mode 100644 lib/routes/mcbbs/forum.js create mode 100644 lib/routes/mcbbs/post.js create mode 100644 lib/routes/mcbbs/utils.js diff --git a/docs/bbs.md b/docs/bbs.md index 241dd0eb5..c7f22572e 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -14,6 +14,16 @@ pageClass: routes +## MCBBS + +### 版块 + + + +### 帖子 + + + ## NGA ### 分区帖子 diff --git a/lib/router.js b/lib/router.js index 75f25567c..b3aa25d66 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1861,6 +1861,10 @@ router.get('/hatena/anonymous_diary/archive', require('./routes/hatena/anonymous // kaggle router.get('/kaggle/discussion/:forumId/:sort?', require('./routes/kaggle/discussion')); +// mcbbs +router.get('/mcbbs/forum/:type', require('./routes/mcbbs/forum')); +router.get('/mcbbs/post/:tid/:authorid?', require('./routes/mcbbs/post')); + // Pocket router.get('/pocket/trending', require('./routes/pocket/trending')); diff --git a/lib/routes/mcbbs/forum.js b/lib/routes/mcbbs/forum.js new file mode 100644 index 000000000..71666e452 --- /dev/null +++ b/lib/routes/mcbbs/forum.js @@ -0,0 +1,58 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const type = ctx.params.type; + + const response = await got(utils.forumUrl, { + method: 'get', + params: { + mod: 'forumdisplay', + fid: type, + // 最新帖子 + filter: 'lastpost', + orderby: 'lastpost', + }, + baseUrl: utils.host, + }); + const data = response.data; + const $ = cheerio.load(data); + + const title = $('title').text(); + const list = $('#separatorline ~ tbody'); + const description = $('meta[name="description"]').attr('content'); + const item = list + .map((index, element) => { + const title = $('.s', element).text(); + const href = $('.icn > a', element).attr('href'); + const link = `${utils.host}/${href}`; + const postBy = $('.by', element); + const author = $('cite', postBy[0]); + const postDate = $('span[title]', postBy[0]); + const endAuthor = $('cite', postBy[1]); + const endDate = $('span[title]', postBy[1]); + const description = `${author.html()}发表于${postDate.parent().html()},最后由${endAuthor.html()}于${endDate.parent().html()}回复。`; + const pubDate = new Date(`${endDate.attr('title')} GMT+0800`).toUTCString(); + const category = $('.new > em', element).text(); + + const signle = { + title: title, + link: link, + author: author.text(), + description: description, + pubDate: pubDate, + category: category, + }; + return signle; + }) + .get(); + + ctx.state.data = { + title: title, + link: response.url, + description: description, + allowEmpty: true, + item: item, + }; +}; diff --git a/lib/routes/mcbbs/post.js b/lib/routes/mcbbs/post.js new file mode 100644 index 000000000..8a386bf3b --- /dev/null +++ b/lib/routes/mcbbs/post.js @@ -0,0 +1,56 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const utils = require('./utils'); + +module.exports = async (ctx) => { + const tid = ctx.params.tid; + const authorid = ctx.params.authorid; + const response = await got(utils.forumUrl, { + method: 'get', + params: { + mod: 'viewthread', + tid: tid, + authorid: authorid, + // 倒序查看帖子 + ordertype: 1, + }, + baseUrl: utils.host, + }); + const data = response.data; + const $ = cheerio.load(data); + const title = $('title').text(); + const description = $('meta[name="description"]').attr('content'); + // const description = $('div[id^=post_] > .t_f').html(); + const postPan = authorid ? $('#postlist > div[id^=post_]') : $('#postlist > div[id^=post_]').nextAll(); + const item = postPan + .map((index, element) => { + const link = utils.host + '/' + $('[id^=postnum]', element).attr('href'); + const xw1 = $('.authi > .xw1', element); + const author = xw1.text(); + const description = $('.t_f', element).html(); + const em = $('[id^=authorposton]', element); + const date = em.text(); + const span = $('span', em); + const postDate = span ? span.attr('title') : date; + const title = `${xw1.html()} ${em.html()}`; + const pubDate = new Date(`${postDate} GMT+0800`).toUTCString(); + + const signle = { + title: title, + link: link, + author: author, + description: description, + pubDate: pubDate, + }; + return signle; + }) + .get(); + + ctx.state.data = { + title: title, + link: response.url, + description: description, + allowEmpty: true, + item: item, + }; +}; diff --git a/lib/routes/mcbbs/utils.js b/lib/routes/mcbbs/utils.js new file mode 100644 index 000000000..ff1cb4e7e --- /dev/null +++ b/lib/routes/mcbbs/utils.js @@ -0,0 +1,6 @@ +const utils = { + host: 'https://www.mcbbs.net', + forumUrl: '/forum.php', +}; + +module.exports = utils;