From c26b25b57174d11b6245ea9db9968d3f013398be Mon Sep 17 00:00:00 2001 From: Kay W Date: Sat, 22 Jan 2022 20:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dlearnku=20=20descript=20?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=8D=E6=AD=A3=E7=A1=AE=E5=92=8C=E4=B8=8D?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=86=85=E5=AE=B9=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#8818)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kai Wei --- lib/router.js | 3 -- lib/routes/learnku/topic.js | 53 ---------------------- lib/v2/learnku/maintainer.js | 3 ++ lib/v2/learnku/radar.js | 13 ++++++ lib/v2/learnku/router.js | 3 ++ lib/v2/learnku/templates/topic.art | 14 ++++++ lib/v2/learnku/topic.js | 72 ++++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 56 deletions(-) delete mode 100644 lib/routes/learnku/topic.js create mode 100644 lib/v2/learnku/maintainer.js create mode 100644 lib/v2/learnku/radar.js create mode 100644 lib/v2/learnku/router.js create mode 100644 lib/v2/learnku/templates/topic.art create mode 100644 lib/v2/learnku/topic.js diff --git a/lib/router.js b/lib/router.js index daa24dde37..b5676b91dd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2681,9 +2681,6 @@ router.get('/krankenkassen', lazyloadRouteHandler('./routes/krankenkassen')); // 桂林航天工业学院 router.get('/guat/news/:type?', lazyloadRouteHandler('./routes/guat/news')); -// LearnKu -router.get('/learnku/:community/:category?', lazyloadRouteHandler('./routes/learnku/topic')); - // NEEA router.get('/neea/:type', lazyloadRouteHandler('./routes/neea')); diff --git a/lib/routes/learnku/topic.js b/lib/routes/learnku/topic.js deleted file mode 100644 index cf3a75d0d7..0000000000 --- a/lib/routes/learnku/topic.js +++ /dev/null @@ -1,53 +0,0 @@ -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, - }); - - 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, - 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 !== ''), - }; -}; diff --git a/lib/v2/learnku/maintainer.js b/lib/v2/learnku/maintainer.js new file mode 100644 index 0000000000..ae7fe1ee57 --- /dev/null +++ b/lib/v2/learnku/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:community/:category?': ['kayw-geek'], +}; diff --git a/lib/v2/learnku/radar.js b/lib/v2/learnku/radar.js new file mode 100644 index 0000000000..a2965ad4fa --- /dev/null +++ b/lib/v2/learnku/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'learnku.com': { + _name: 'Learn Ku 社区', + '.': [ + { + title: '分区', + docs: 'https://docs.rsshub.app/bbs.html#learnku', + source: ['/:community'], + target: '/learnku/:community', + }, + ], + }, +}; diff --git a/lib/v2/learnku/router.js b/lib/v2/learnku/router.js new file mode 100644 index 0000000000..1c4ed102bd --- /dev/null +++ b/lib/v2/learnku/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:community/:category?', require('./topic.js')); +}; diff --git a/lib/v2/learnku/templates/topic.art b/lib/v2/learnku/templates/topic.art new file mode 100644 index 0000000000..0dd3dce470 --- /dev/null +++ b/lib/v2/learnku/templates/topic.art @@ -0,0 +1,14 @@ +
+

🦕正文

+
+
+ {{@ article }} +
+
+{{if comment }} +
+

👨‍💻评论

+
+ {{@ comment }} +
+{{/if}} diff --git a/lib/v2/learnku/topic.js b/lib/v2/learnku/topic.js new file mode 100644 index 0000000000..d410060011 --- /dev/null +++ b/lib/v2/learnku/topic.js @@ -0,0 +1,72 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { art } = require('@/utils/render'); +const path = require('path'); +const { parseDate } = require('@/utils/parse-date'); + +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, + }); + + const data = response.data; + const $ = cheerio.load(data); + const list = $('.simple-topic').get(); + const item = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const categoryName = $('.category-name span').text().trim(); + if (['置顶', '广告'].includes(categoryName)) { + return ''; + } + $('.topic-title i').remove(); + const itemLink = $('.topic-title-wrap').attr('href'); + + const title = $('.topic-title').text().trim(); + const content = await ctx.cache.tryGet(itemLink, async () => { + const result = await got.get(itemLink); + + return cheerio.load(result.data); + }); + const article = content('.article-content .content-body').html(); + const comment = content('#all-comments').html(); + + return { + title, + description: art(path.join(__dirname, 'templates/topic.art'), { + article, + comment, + }), + category: categoryName, + link: itemLink, + pubDate: parseDate($('.timeago').attr('title'), 'YYYY/MM/DD'), + }; + }) + ); + + 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, + item: item.filter((item) => item !== ''), + }; +};