From 5bd095f6b898379609ccc07dcdd570b32c66cfbe Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 23 Jan 2022 02:24:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E8=B5=B7=E7=82=B9?= =?UTF-8?q?=E5=9B=BE=20(#8726)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/reading.md | 22 +++++++++ lib/v2/qidiantu/index.js | 87 +++++++++++++++++++++++++++++++++++ lib/v2/qidiantu/maintainer.js | 4 ++ lib/v2/qidiantu/radar.js | 19 ++++++++ lib/v2/qidiantu/router.js | 3 ++ 5 files changed, 135 insertions(+) create mode 100644 lib/v2/qidiantu/index.js create mode 100644 lib/v2/qidiantu/maintainer.js create mode 100644 lib/v2/qidiantu/radar.js create mode 100644 lib/v2/qidiantu/router.js diff --git a/docs/reading.md b/docs/reading.md index 04c3288c7..32de59574 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -313,6 +313,28 @@ count 的取值范围为 1-12,为防止请求次数过多,推荐设置为 5 +## 起点图 + +### 首订 + + + +### 榜单 + + + +::: tip 提示 + +参数 **是否查看历史榜单** 设置为 `true` `yes` `t` `y` 等值后,RSS 会返回历史榜单。 + +如 [`/qidiantu/1/1/t`](https://rsshub.app/qidiantu/1/1/t) 将会返回 [起点首页 - 封推 的历史推荐记录](https://www.qidiantu.com/bang/1/1/) 的结果。 + +而 [`/qidiantu/1/1`](https://rsshub.app/qidiantu/1/1) 将会返回最新一期 [收藏增长 (起点首页 - 封推)](https://www.qidiantu.com/bang/1/1/2021-12-14) 的结果(编写文档时最新一期为 2021-12-14)。 + +::: + + + ## 青空文庫 ### 青空文庫新着リスト diff --git a/lib/v2/qidiantu/index.js b/lib/v2/qidiantu/index.js new file mode 100644 index 000000000..bce4d877b --- /dev/null +++ b/lib/v2/qidiantu/index.js @@ -0,0 +1,87 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'shouding'; + const type = ctx.params.type ?? (category === 'shouding' ? '' : '1'); + const isHistory = /t|y/i.test(ctx.params.is_history ?? 'false'); + + const today = new Date(); + today.setTime(new Date().getTime() - 24 * 60 * 60 * 1000); + const yesterday = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`; + + const rootUrl = 'https://www.qidiantu.com'; + const currentUrl = `${rootUrl}/${category === 'shouding' ? `${category}/` : `bang/${category}/${type}/${isHistory ? '' : yesterday}`}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + $('a[role="button"]').remove(); + + let items = $(isHistory ? 'ul li a' : 'tbody tr') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 25) + .toArray() + .map((item) => { + item = $(item); + + return isHistory + ? { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + } + : { + author: item.find('td').eq(3).text(), + title: item.find('td[value]').first().text(), + link: `${rootUrl}${item.find('td a').first().attr('href').replace(/\/d$/, '')}`, + category: item + .find(`td${category === 'shouding' ? '[value]' : ''}`) + .eq(1) + .text(), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(isHistory ? item.link : `${item.link}-${yesterday}`, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + content('#demo').next().remove(); + + if (isHistory) { + item.description = content('.table-responsive').html(); + item.pubDate = parseDate(item.link.split('/').pop()); + } else { + item.description = content('.media').html() + content('.panel-body').html(); + item.pubDate = timezone( + parseDate( + content('tbody tr') + .last() + .text() + .match(/刷新时间:(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/)[1] + ), + +8 + ); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${$('.panel-heading').text()} - 起点图`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/qidiantu/maintainer.js b/lib/v2/qidiantu/maintainer.js new file mode 100644 index 000000000..01cddd0c9 --- /dev/null +++ b/lib/v2/qidiantu/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/:category?/:type?/:is_history?': ['nczitzk'], + '/shouding': ['nczitzk'], +}; diff --git a/lib/v2/qidiantu/radar.js b/lib/v2/qidiantu/radar.js new file mode 100644 index 000000000..e3900a4b7 --- /dev/null +++ b/lib/v2/qidiantu/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'qidiantu.com': { + _name: '起点图', + '.': [ + { + title: '首订', + docs: 'https://docs.rsshub.app/reading.html#qi-dian-tu-shou-ding', + source: ['/shouding', '/'], + target: '/qidiantu/shouding', + }, + { + title: '榜单', + docs: 'https://docs.rsshub.app/reading.html#qi-dian-tu-bang-dan', + source: ['/bang/:category/:type', '/'], + target: '/qidiantu/:category?/:type?/:is_history?', + }, + ], + }, +}; diff --git a/lib/v2/qidiantu/router.js b/lib/v2/qidiantu/router.js new file mode 100644 index 000000000..701fe5239 --- /dev/null +++ b/lib/v2/qidiantu/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:category?/:type?/:is_history?', require('./index')); +};