From e5ea410e7d2543047f46be99440b379d8902f24d Mon Sep 17 00:00:00 2001 From: WooMai <34209782+WooMai@users.noreply.github.com> Date: Wed, 17 Mar 2021 05:41:36 +0800 Subject: [PATCH] feat(route): Add Liyuan Forums Support (#7131) Co-authored-by: NeverBehave --- docs/bbs.md | 18 +++++++++ lib/router.js | 6 +++ lib/routes/liyuan-forums/threads.js | 61 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 lib/routes/liyuan-forums/threads.js diff --git a/docs/bbs.md b/docs/bbs.md index de133d637..5ddbe4fc1 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -360,6 +360,24 @@ pageClass: routes | 最新主题 | latest | | 精华主题 | digest | +## 梨园 + +### 主题帖(全站) + + + +### 主题帖(板块) + + + +### 主题帖(专题) + + + +### 主题帖(用户) + + + ## 龙空 ### 分区 diff --git a/lib/router.js b/lib/router.js index 71cf57701..21403d7e1 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4042,6 +4042,12 @@ router.get('/wainao-reads/all-articles', require('./routes/wainao/index')); // react router.get('/react/react-native-weekly', require('./routes/react/react-native-weekly')); +// 梨园 +router.get('/liyuan-forums/threads', require('./routes/liyuan-forums/threads')); +router.get('/liyuan-forums/threads/forum/:forum_id', require('./routes/liyuan-forums/threads')); +router.get('/liyuan-forums/threads/topic/:topic_id', require('./routes/liyuan-forums/threads')); +router.get('/liyuan-forums/threads/user/:user_id', require('./routes/liyuan-forums/threads')); + // 集思录 router.get('/jisilu/reply/:user', require('./routes/jisilu/reply')); router.get('/jisilu/topic/:user', require('./routes/jisilu/topic')); diff --git a/lib/routes/liyuan-forums/threads.js b/lib/routes/liyuan-forums/threads.js new file mode 100644 index 000000000..c4e4f9347 --- /dev/null +++ b/lib/routes/liyuan-forums/threads.js @@ -0,0 +1,61 @@ +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const forum_id = ctx.params.forum_id || null; + const topic_id = ctx.params.topic_id || null; + const user_id = ctx.params.user_id || null; + + const query = ['initial_post=1']; + + let link = 'https://forums.liyuans.com/recent'; + + if (forum_id) { + query.push(`forum_id=${encodeURIComponent(forum_id)}`); + link = `https://forums.liyuans.com/forum/${forum_id}`; + } + + if (topic_id) { + query.push(`topic_id=${encodeURIComponent(topic_id)}`); + link = `https://forums.liyuans.com/topic/${topic_id}`; + } + + if (user_id) { + query.push(`user_id=${encodeURIComponent(user_id)}`); + link = `https://forums.liyuans.com/user/${user_id}`; + } + + let qstr = ''; + if (query.length > 0) { + qstr = '?' + query.join('&'); + } + + const response = await got({ + method: 'get', + url: `https://api.forums.liyuans.com/threads${qstr}`, + }); + + const data = response.data.data.results; + + ctx.state.data = { + title: '梨园', + link: link, + description: '最新帖子 - 梨园', + allowEmpty: true, + item: data.map((item) => { + let category = [item.forum.name]; + if (item.topic) { + category = category.push(item.topic.name); + } + + return { + title: item.title, + author: item.user.nickname, + category: category, + description: `@${item.user.username}: ${item.initial_post.thumb}`, + pubDate: new Date(item.create_time * 1000).toUTCString(), + guid: `Thread_${item.id}`, + link: `https://forums.liyuans.com/thread/${item.id}`, + }; + }), + }; +};