From 94e4f0734f9155be5135f2eaa58b7590f1f5a6ac Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Fri, 21 Jul 2023 00:11:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E7=88=B1=E6=80=9D?= =?UTF-8?q?=E6=83=B3=E4=B8=93=E9=A2=98=20(#12845)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reading.md | 12 +++++++++ lib/v2/aisixiang/column.js | 35 ++++++++++++++++---------- lib/v2/aisixiang/maintainer.js | 1 + lib/v2/aisixiang/radar.js | 6 +++++ lib/v2/aisixiang/router.js | 1 + lib/v2/aisixiang/thinktank.js | 26 ++++++++++---------- lib/v2/aisixiang/toplist.js | 36 +++++++++++++++------------ lib/v2/aisixiang/utils.js | 26 ++++++++++++-------- lib/v2/aisixiang/zhuanti.js | 45 ++++++++++++++++++++++++++++++++++ 9 files changed, 137 insertions(+), 51 deletions(-) create mode 100644 lib/v2/aisixiang/zhuanti.js diff --git a/docs/reading.md b/docs/reading.md index e69390e4b..b69faa44e 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -182,6 +182,18 @@ For instance, when doing search at and you get url +### 专题 + + + +::: tip 提示 + +更多专题请见 [关键词](http://www.aisixiang.com/zhuanti/) + +::: + + + ### 排行 diff --git a/lib/v2/aisixiang/column.js b/lib/v2/aisixiang/column.js index 863ce1ccd..1ea10139b 100644 --- a/lib/v2/aisixiang/column.js +++ b/lib/v2/aisixiang/column.js @@ -1,36 +1,45 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { rootUrl, ProcessFeed } = require('./utils'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const { rootUrl, ossUrl, ProcessFeed } = require('./utils'); module.exports = async (ctx) => { - const id = ctx.params.id; - const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 30; + const { id } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; - const currentUrl = `${rootUrl}/data/search?column=${id}`; + const currentUrl = new URL(`/data/search?column=${id}`, rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const { data: response } = await got(currentUrl); - const $ = cheerio.load(response.data); + const $ = cheerio.load(response); const title = $('div.article-title a').first().text().replace(/\[|\]/g, ''); - const items = $('div.search_list a[title]') + const items = $('div.article-title') + .slice(0, limit) .toArray() .map((item) => { item = $(item); + const a = item.find('a[title]'); + return { - title: item.text().split(':').pop(), - link: new URL(item.attr('href'), rootUrl).href, + title: a.text(), + link: new URL(a.prop('href'), rootUrl).href, + author: a.text().split(':')[0], + pubDate: timezone(parseDate(item.find('span').text()), +8), }; }); ctx.state.data = { + item: await ProcessFeed(limit, ctx.cache.tryGet, items), title: `爱思想 - ${title}`, link: currentUrl, - item: await ProcessFeed(limit, ctx.cache.tryGet, items), + description: $('div.tips').text(), + language: 'zh-cn', + image: new URL('images/logo.jpg', ossUrl).href, + subtitle: title, }; }; diff --git a/lib/v2/aisixiang/maintainer.js b/lib/v2/aisixiang/maintainer.js index 1b6175522..3004961ce 100644 --- a/lib/v2/aisixiang/maintainer.js +++ b/lib/v2/aisixiang/maintainer.js @@ -3,4 +3,5 @@ module.exports = { '/ranking/:id?/:period?': ['HenryQW', 'nczitzk'], '/toplist/:id?/:period?': ['nczitzk'], '/thinktank/:id/:type?': ['hoilc', 'nczitzk'], + '/zhuanti/:id': ['nczitzk'], }; diff --git a/lib/v2/aisixiang/radar.js b/lib/v2/aisixiang/radar.js index 0e1b9306f..b83b6d3bf 100644 --- a/lib/v2/aisixiang/radar.js +++ b/lib/v2/aisixiang/radar.js @@ -8,6 +8,12 @@ module.exports = { source: ['/data/search', '/'], target: (params, url) => `/aisixiang/column/${new URL(url).searchParams.get('column')}`, }, + { + title: '专题', + docs: 'https://docs.rsshub.app/reading.html#ai-si-xiang', + source: ['/zhuanti', '/'], + target: (params, url) => `/aisixiang/zhuanti/${new URL(url).href.match(/\/zhuanti\/(.*?)\.html/)[1]}`, + }, { title: '排行', docs: 'https://docs.rsshub.app/reading.html#ai-si-xiang', diff --git a/lib/v2/aisixiang/router.js b/lib/v2/aisixiang/router.js index 8cbf64048..66559ee2b 100644 --- a/lib/v2/aisixiang/router.js +++ b/lib/v2/aisixiang/router.js @@ -3,4 +3,5 @@ module.exports = function (router) { router.get('/ranking/:id?/:period?', require('./toplist')); router.get('/toplist/:id?/:period?', require('./toplist')); router.get('/thinktank/:id/:type?', require('./thinktank')); + router.get('/zhuanti/:id', require('./zhuanti')); }; diff --git a/lib/v2/aisixiang/thinktank.js b/lib/v2/aisixiang/thinktank.js index 38732a250..5bd5869a7 100644 --- a/lib/v2/aisixiang/thinktank.js +++ b/lib/v2/aisixiang/thinktank.js @@ -1,20 +1,17 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { rootUrl, ProcessFeed } = require('./utils'); + +const { rootUrl, ossUrl, ProcessFeed } = require('./utils'); module.exports = async (ctx) => { - const id = ctx.params.id; - const type = ctx.params.type ?? ''; - const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 30; + const { id, type = '' } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; - const currentUrl = `${rootUrl}/thinktank/${id}.html`; + const currentUrl = new URL(`thinktank/${id}.html`, rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const { data: response } = await got(currentUrl); - const $ = cheerio.load(response.data); + const $ = cheerio.load(response); const title = `${$('h2').first().text().trim()}${type}`; @@ -31,19 +28,22 @@ module.exports = async (ctx) => { items = [...items, ...$(l).parent().find('ul li a').toArray()]; } - items = items.map((item) => { + items = items.slice(0, limit).map((item) => { item = $(item); return { title: item.text().split(':').pop(), - link: new URL(item.attr('href'), rootUrl).href, + link: new URL(item.prop('href'), rootUrl).href, }; }); ctx.state.data = { + item: await ProcessFeed(limit, ctx.cache.tryGet, items), title: `爱思想 - ${title}`, link: currentUrl, - item: await ProcessFeed(limit, ctx.cache.tryGet, items), description: $('div.thinktank-author-description-box p').text(), + language: 'zh-cn', + image: new URL('images/logo_thinktank.jpg', ossUrl).href, + subtitle: title, }; }; diff --git a/lib/v2/aisixiang/toplist.js b/lib/v2/aisixiang/toplist.js index 2d855e702..caaf12d0c 100644 --- a/lib/v2/aisixiang/toplist.js +++ b/lib/v2/aisixiang/toplist.js @@ -1,37 +1,43 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const { rootUrl, ProcessFeed } = require('./utils'); +const { parseDate } = require('@/utils/parse-date'); + +const { rootUrl, ossUrl, ProcessFeed } = require('./utils'); module.exports = async (ctx) => { - const id = ctx.params.id ?? '1'; - const period = ctx.params.period ?? '1'; - const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 30; + const { id = '1', period = '1' } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; - const currentUrl = `${rootUrl}/toplist${id ? `?id=${id}${id === '1' ? `&period=${period}` : ''}` : ''}`; + const currentUrl = new URL(`toplist${id ? `?id=${id}${id === '1' ? `&period=${period}` : ''}` : ''}`, rootUrl).href; - const response = await got({ - method: 'get', - url: currentUrl, - }); + const { data: response } = await got(currentUrl); - const $ = cheerio.load(response.data); + const $ = cheerio.load(response); - const title = `${$('.hl').text() || ''}${$('title').text().split('_')[0]}`; + const title = `${$('a.hl').text() || ''}${$('title').text().split('_')[0]}`; - const items = $('div.tips a') + const items = $('div.tops_list') + .slice(0, limit) .toArray() .map((item) => { item = $(item); + const a = item.find('div.tips a'); + return { - title: item.text().split(':').pop(), - link: new URL(item.attr('href'), rootUrl).href, + title: a.text(), + link: new URL(a.prop('href'), rootUrl).href, + author: item.find('div.name').text(), + pubDate: parseDate(item.find('div.times').text()), }; }); ctx.state.data = { + item: await ProcessFeed(limit, ctx.cache.tryGet, items), title: `爱思想 - ${title}`, link: currentUrl, - item: await ProcessFeed(limit, ctx.cache.tryGet, items), + language: 'zh-cn', + image: new URL('images/logo_toplist.jpg', ossUrl).href, + subtitle: title, }; }; diff --git a/lib/v2/aisixiang/utils.js b/lib/v2/aisixiang/utils.js index 48fca95c7..d52b458eb 100644 --- a/lib/v2/aisixiang/utils.js +++ b/lib/v2/aisixiang/utils.js @@ -3,28 +3,33 @@ const cheerio = require('cheerio'); const timezone = require('@/utils/timezone'); const { parseDate } = require('@/utils/parse-date'); -const rootUrl = 'http://www.aisixiang.com'; +const ossUrl = 'https://oss.aisixiang.com'; +const rootUrl = 'https://www.aisixiang.com'; const ProcessFeed = (limit, tryGet, items) => Promise.all( items.slice(0, limit).map((item) => tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + const { data: detailResponse } = await got(item.link); - const content = cheerio.load(detailResponse.data); + const content = cheerio.load(detailResponse); - item.author = content('strong').first().text(); - item.description = content('.article-content').html(); - item.pubDate = timezone(parseDate(content('.info').text().split('时间:').pop()), +8); + const commentMatches = content('h3.comment-header') + .text() + .match(/评论(\d+)/); + + item.title = content('h3').first().text().split(':').pop(); + item.description = content('div.article-content').html(); + item.author = content('div.about strong').first().text(); item.category = content('u') .first() .parent() .find('u') .toArray() - .map((a) => content(a).text()); + .map((c) => content(c).text()); + item.pubDate = timezone(parseDate(content('div.info').text().split('时间:').pop()), +8); + item.upvotes = content('span.like-num').text() ? parseInt(content('span.like-num').text(), 10) : 0; + item.comments = commentMatches ? parseInt(commentMatches[1], 10) : 0; return item; }) @@ -33,5 +38,6 @@ const ProcessFeed = (limit, tryGet, items) => module.exports = { rootUrl, + ossUrl, ProcessFeed, }; diff --git a/lib/v2/aisixiang/zhuanti.js b/lib/v2/aisixiang/zhuanti.js new file mode 100644 index 000000000..6c90eb406 --- /dev/null +++ b/lib/v2/aisixiang/zhuanti.js @@ -0,0 +1,45 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +const { rootUrl, ossUrl, ProcessFeed } = require('./utils'); + +module.exports = async (ctx) => { + const { id } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; + + const currentUrl = new URL(`zhuanti/${id}.html`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = cheerio.load(response); + + const title = $('div.tips h2').first().text(); + + const items = $('div.article-title') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const a = item.find('a'); + + return { + title: a.text(), + link: new URL(a.prop('href'), rootUrl).href, + author: a.text().split(':')[0], + pubDate: timezone(parseDate(item.find('span').text()), +8), + }; + }); + + ctx.state.data = { + item: await ProcessFeed(limit, ctx.cache.tryGet, items), + title: `爱思想 - ${title}`, + link: currentUrl, + description: $('div.tips p').text(), + language: 'zh-cn', + image: new URL('images/logo_zhuanti.jpg', ossUrl).href, + subtitle: title, + }; +};