From 46d1052e8aaa120c5dd4ea7798dcc67a4d64a180 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=87=AF=E9=98=B3?= <10895490+haokaiyang@users.noreply.github.com> Date: Sun, 8 Mar 2020 23:14:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20LearnKu=20=E8=AE=BA=E5=9D=9B=20(#?= =?UTF-8?q?4197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/bbs.md | 10 +++++++ lib/router.js | 3 ++ lib/routes/learnku/topic.js | 57 +++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/routes/learnku/topic.js diff --git a/docs/bbs.md b/docs/bbs.md index e67b2d6e8..4110be331 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -40,6 +40,16 @@ pageClass: routes +## LearnKu + +### 社区 + + + +| 招聘 | 翻译 | 问答 | 链接 | +| ---- | ------------ | ---- | ----- | +| jobs | translations | qa | links | + ## MCBBS ### 版块 diff --git a/lib/router.js b/lib/router.js index e78588945..c956f1dea 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2346,4 +2346,7 @@ router.get('/guat/news/:type?', require('./routes/guat/news')); // 国家留学网 router.get('/csc/notice/:type?', require('./routes/csc/notice')); +// LearnKu +router.get('/learnku/:community/:category?', require('./routes/learnku/topic')); + module.exports = router; diff --git a/lib/routes/learnku/topic.js b/lib/routes/learnku/topic.js new file mode 100644 index 000000000..d3309c05f --- /dev/null +++ b/lib/routes/learnku/topic.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const community = ctx.params.community; + const category = ctx.params.category || ''; + + let url = `https://learnku.com/${community}`; + if (category !== '') { + url = `https://learnku.com/${community}/c/${category}`; + } + + const response = await got({ + method: 'get', + url: url, + }); + + const data = response.data; + const $ = cheerio.load(data); + const list = $('.simple-topic').get(); + const title = $('.sidebar .community-details .header span').text(); + $('.sidebar .community-details .main div div a').remove(); + const description = $('.sidebar .community-details .main div div').text(); + const categoryTitle = new Map([ + ['translations', { name: '翻译' }], + ['jobs', { name: '招聘' }], + ['qa', { name: '问答' }], + ['links', { name: '链接' }], + ['', { name: '最新' }], + ]); + + ctx.state.data = { + title: `LearnKu - ${title} - ${categoryTitle.get(category).name}`, + link: url, + description: description, + item: list + .map((item) => { + const $ = cheerio.load(item); + const categoryName = $('.category-name') + .text() + .trim(); + if (['置顶', '广告'].includes(categoryName)) { + return ''; + } + $('.topic-title i').remove(); + return { + title: $('.topic-title') + .text() + .trim(), + category: categoryName, + link: $('.topic-title-wrap').attr('href'), + pubDate: new Date($('.timeago').attr('title')).toUTCString(), + }; + }) + .filter((item) => item !== ''), + }; +};