From 78165d77b21c281ce5c4f8ed75ed6f470c048fca Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 4 Jun 2022 12:08:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=AD=A3=E7=89=88?= =?UTF-8?q?=E4=B8=AD=E5=9B=BD=20(#9890)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/shopping.md | 12 ++++++++ lib/v2/getitfree/index.js | 54 ++++++++++++++++++++++++++++++++++ lib/v2/getitfree/maintainer.js | 3 ++ lib/v2/getitfree/radar.js | 13 ++++++++ lib/v2/getitfree/router.js | 3 ++ 5 files changed, 85 insertions(+) create mode 100644 lib/v2/getitfree/index.js create mode 100644 lib/v2/getitfree/maintainer.js create mode 100644 lib/v2/getitfree/radar.js create mode 100644 lib/v2/getitfree/router.js diff --git a/docs/shopping.md b/docs/shopping.md index 4e4a20b70..ec5a5b314 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -431,3 +431,15 @@ For instance, in + +## 正版中国 + +### 分类 + + + +| 所有类别 | Android | iOS | Mac | PC | UWP | 公告 | 永久免费 | 限时免费 | 限时折扣 | +| ---- | ------- | --- | --- | -- | --- | ------------ | ---- | -------- | -------- | +| | android | ios | mac | pc | uwp | notification | free | giveaway | discount | + + diff --git a/lib/v2/getitfree/index.js b/lib/v2/getitfree/index.js new file mode 100644 index 000000000..9d9bd971e --- /dev/null +++ b/lib/v2/getitfree/index.js @@ -0,0 +1,54 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? ''; + + const rootUrl = 'https://www.getitfree.cn'; + const currentUrl = `${rootUrl}${category ? `/category/${category}` : ''}/page/1`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('a.post-title') + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.text(), + link: item.attr('href'), + }; + }); + + 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); + + content('h3, footer').remove(); + + item.description = content('.post-content-text').html(); + item.pubDate = parseDate(content('time.entry-date').first().attr('datetime')); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/getitfree/maintainer.js b/lib/v2/getitfree/maintainer.js new file mode 100644 index 000000000..81eb78d73 --- /dev/null +++ b/lib/v2/getitfree/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:category?': ['nczitzk'], +}; diff --git a/lib/v2/getitfree/radar.js b/lib/v2/getitfree/radar.js new file mode 100644 index 000000000..8989af49f --- /dev/null +++ b/lib/v2/getitfree/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'getitfree.cn': { + _name: '正版中国', + '.': [ + { + title: '分类', + docs: 'https://docs.rsshub.app/shopping.html#zheng-ban-zhong-guo-fen-lei', + source: ['/category/:category', '/'], + target: '/getitfree/:category?', + }, + ], + }, +}; diff --git a/lib/v2/getitfree/router.js b/lib/v2/getitfree/router.js new file mode 100644 index 000000000..cf781366f --- /dev/null +++ b/lib/v2/getitfree/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:category?', require('./index')); +};