From a5a3afa4bd8e745c2577beeedb25a5cdbabe4490 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Tue, 11 Apr 2023 05:30:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20Go=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E4=B8=AD=E6=96=87=E7=BD=91=E6=9D=BF=E5=9D=97=20(#12274)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/programming.md | 8 +++- lib/v2/studygolang/go.js | 5 +++ lib/v2/studygolang/jobs.js | 36 ++-------------- lib/v2/studygolang/maintainer.js | 5 ++- lib/v2/studygolang/radar.js | 12 ++++-- lib/v2/studygolang/router.js | 1 + lib/v2/studygolang/utils.js | 74 ++++++++++++++++++++++++++++++++ lib/v2/studygolang/weekly.js | 28 ++---------- 8 files changed, 104 insertions(+), 65 deletions(-) create mode 100644 lib/v2/studygolang/go.js create mode 100644 lib/v2/studygolang/utils.js diff --git a/docs/programming.md b/docs/programming.md index 3ca569334..bd6d8140d 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -350,13 +350,17 @@ GitHub 官方也提供了一些 RSS: ## Go 语言中文网 +### 板块 + + + ### 周刊 - + ### 招聘 - + ## GoCN diff --git a/lib/v2/studygolang/go.js b/lib/v2/studygolang/go.js new file mode 100644 index 000000000..090aba0a3 --- /dev/null +++ b/lib/v2/studygolang/go.js @@ -0,0 +1,5 @@ +const { FetchGoItems } = require('./utils'); + +module.exports = async (ctx) => { + ctx.state.data = await FetchGoItems(ctx); +}; diff --git a/lib/v2/studygolang/jobs.js b/lib/v2/studygolang/jobs.js index 5702b8fe3..3e163f94c 100644 --- a/lib/v2/studygolang/jobs.js +++ b/lib/v2/studygolang/jobs.js @@ -1,37 +1,7 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseDate } = require('@/utils/parse-date'); -const timezone = require('@/utils/timezone'); -const base_url = 'https://studygolang.com'; +const { FetchGoItems } = require('./utils'); module.exports = async (ctx) => { - const jobs_url = 'https://studygolang.com/go/jobs'; + ctx.params.id = 'jobs'; - const response = await got({ - url: jobs_url, - headers: { - Referer: base_url, - }, - }); - - const $ = cheerio.load(response.data); - const list = $('.topics .topic').get(); - - ctx.state.data = { - title: 'Go语言中文网 | 招聘', - link: jobs_url, - description: `获取Go语言中文网的最新招聘`, - item: await Promise.all(list.map((item) => getFeedItem(item))), - }; + ctx.state.data = await FetchGoItems(ctx); }; - -function getFeedItem(item) { - const $ = cheerio.load(item); - const title = $('.title a'); - - return { - title: title.attr('title'), - link: `${base_url}${title.attr('href')}`, - pubDate: timezone(parseDate($('.meta .timeago').attr('title'), 'YYYY-MM-DD hh:mm:ss'), +8), - }; -} diff --git a/lib/v2/studygolang/maintainer.js b/lib/v2/studygolang/maintainer.js index d1af550b7..5ccc33026 100644 --- a/lib/v2/studygolang/maintainer.js +++ b/lib/v2/studygolang/maintainer.js @@ -1,4 +1,5 @@ module.exports = { - '/jobs': ['CcccFz'], - '/weekly': ['Weilet'], + '/go/:id?': ['nczitzk'], + '/jobs': ['CcccFz', 'nczitzk'], + '/weekly': ['CWeilet', 'nczitzk'], }; diff --git a/lib/v2/studygolang/radar.js b/lib/v2/studygolang/radar.js index d2cadb4e8..49f39b1ea 100644 --- a/lib/v2/studygolang/radar.js +++ b/lib/v2/studygolang/radar.js @@ -2,17 +2,23 @@ module.exports = { 'studygolang.com': { _name: 'Go 语言中文网', '.': [ + { + title: '板块', + docs: 'https://docs.rsshub.app/programming.html#go-yu-yan-zhong-wen-wang', + source: ['/go/:id', '/'], + target: '/studygolang/go/:id?', + }, { title: '招聘', docs: 'https://docs.rsshub.app/programming.html#go-yu-yan-zhong-wen-wang', - source: ['/'], - target: '/studygolang/jobs', + source: ['/go/jobs', '/'], + target: '/studygolang/go/jobs', }, { title: '周刊', docs: 'https://docs.rsshub.app/programming.html#go-yu-yan-zhong-wen-wang', source: ['/go/weekly', '/'], - target: '/studygolang/weekly', + target: '/studygolang/go/weekly', }, ], }, diff --git a/lib/v2/studygolang/router.js b/lib/v2/studygolang/router.js index 587b748e4..888534881 100644 --- a/lib/v2/studygolang/router.js +++ b/lib/v2/studygolang/router.js @@ -1,4 +1,5 @@ module.exports = function (router) { + router.get('/go/:id?', require('./go')); router.get('/jobs', require('./jobs')); router.get('/weekly', require('./weekly')); }; diff --git a/lib/v2/studygolang/utils.js b/lib/v2/studygolang/utils.js new file mode 100644 index 000000000..798d82b1c --- /dev/null +++ b/lib/v2/studygolang/utils.js @@ -0,0 +1,74 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); +const md = require('markdown-it')({ + html: true, + linkify: true, +}); + +module.exports = { + FetchGoItems: async (ctx) => { + const id = ctx.params.id ?? 'weekly'; + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50; + + const rootUrl = 'https://studygolang.com'; + const currentUrl = `${rootUrl}/go/${id}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.right-info .title') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const a = item.find('a'); + + return { + title: a.text(), + link: `${rootUrl}${a.attr('href')}`, + author: item.next().find('.author').text(), + category: item + .next() + .find('.node') + .toArray() + .map((n) => $(n).text()), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.pubDate = timezone(parseDate(content('.timeago').first().attr('title')), +8); + + try { + item.description = md.render(content('.content').html()); + } catch (e) { + // no-empty + } + + return item; + }) + ) + ); + + return { + title: `Go语言中文网 - ${$('.title h2').text()}`, + link: currentUrl, + item: items, + }; + }, +}; diff --git a/lib/v2/studygolang/weekly.js b/lib/v2/studygolang/weekly.js index a1cdcadce..6a3ceb06b 100644 --- a/lib/v2/studygolang/weekly.js +++ b/lib/v2/studygolang/weekly.js @@ -1,29 +1,7 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); +const { FetchGoItems } = require('./utils'); module.exports = async (ctx) => { - const url = 'https://studygolang.com/go/weekly'; - const response = await got({ - method: 'get', - url, - }); - const $ = cheerio.load(response.data); - const resultItem = $('div.topic div.title') - .map((_index, elem) => { - elem = $(elem); - const $link = elem.find('a'); - const title = $link.text(); - const link = $link.attr('href'); - return { - title, - link, - }; - }) - .get(); + ctx.params.id = 'weekly'; - ctx.state.data = { - title: 'Go语言爱好者周刊', - link: url, - item: resultItem, - }; + ctx.state.data = await FetchGoItems(ctx); };