From 948f53651648a1aab38942e82fc16b3bef2cfdeb Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 15 Dec 2020 19:25:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20MarginNote=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E5=88=86=E4=BA=AB=E7=A4=BE=E5=8C=BA=E6=A0=87=E7=AD=BE=20(#6419?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/study.md | 11 +++++++++ lib/router.js | 3 +++ lib/routes/marginnote/tag.js | 43 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lib/routes/marginnote/tag.js diff --git a/docs/study.md b/docs/study.md index f79d1b8ef..042669c95 100644 --- a/docs/study.md +++ b/docs/study.md @@ -81,6 +81,17 @@ pageClass: routes +## MarginNote + +### 标签 + + + +| 经验分享 | 论坛精华 | 待跟进反馈 | 优秀建议 | 精选回答 | 官方签名 | 自动更新 | 3674 以上版本支持 | 368 以上版本支持 | 未经验证的安全风险 | 笔记本分享 | 关键反馈 | 精选话题讨论 | 灵感盒 | 引用 | +| -------- | -------- | ---------- | -------- | -------- | -------- | -------- | ----------------- | ---------------- | ------------------ | ---------- | -------- | ------------ | ------ | ---- | + + + ## Mind42 ### 分类 diff --git a/lib/router.js b/lib/router.js index 7926d8411..4207fe98b 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3698,6 +3698,9 @@ router.get('/treasury/press-releases/:category?/:title?', require('./routes/us/t // Bandisoft router.get('/bandisoft/:id?/:lang?', require('./routes/bandisoft/index')); +// MarginNote +router.get('/marginnote/tag/:id?', require('./routes/marginnote/tag')); + // ASML router.get('/asml/press-releases', require('./routes/asml/press-releases')); diff --git a/lib/routes/marginnote/tag.js b/lib/routes/marginnote/tag.js new file mode 100644 index 000000000..0a89f5d39 --- /dev/null +++ b/lib/routes/marginnote/tag.js @@ -0,0 +1,43 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id || '经验分享'; + + const rootUrl = 'https://bbs.marginnote.cn'; + const currentUrl = `${rootUrl}/tag/${id}/l/latest.json`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const list = response.data.topic_list.topics.slice(0, 10).map((item) => ({ + title: item.title, + link: `${rootUrl}/t/topic/${item.id}`, + pubDate: new Date(item.last_posted_at).toUTCString(), + })); + + 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, + }); + const content = cheerio.load(detailResponse.data.replace('', '')); + + item.author = content('.creator').eq(0).text(); + item.description = content('.post').eq(0).html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${response.data.topic_list.tags[0].name} - MarginNote 中文论坛`, + link: currentUrl, + item: items, + }; +};