From ccc7f432e935bdee53f628f9b32cfc933e1d538a Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 16:01:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20IT=E4=B9=8B=E5=AE=B6?= =?UTF-8?q?=E4=B8=93=E9=A2=98=20(#8312)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 8 ++++++ lib/v2/ithome/maintainer.js | 3 +++ lib/v2/ithome/radar.js | 13 +++++++++ lib/v2/ithome/router.js | 3 +++ lib/v2/ithome/zt.js | 54 +++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 lib/v2/ithome/maintainer.js create mode 100644 lib/v2/ithome/radar.js create mode 100644 lib/v2/ithome/router.js create mode 100644 lib/v2/ithome/zt.js diff --git a/docs/new-media.md b/docs/new-media.md index 6aa2415b70..f41bb6c236 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -520,6 +520,14 @@ Tag +### 专题 + + + +所有专题请见[此处](https://www.ithome.com/zt) + + + ## IT 桔子 ### 投融资事件 diff --git a/lib/v2/ithome/maintainer.js b/lib/v2/ithome/maintainer.js new file mode 100644 index 0000000000..49b35abc83 --- /dev/null +++ b/lib/v2/ithome/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/zt/:id': ['nczitzk'], +}; diff --git a/lib/v2/ithome/radar.js b/lib/v2/ithome/radar.js new file mode 100644 index 0000000000..f999dd4cc5 --- /dev/null +++ b/lib/v2/ithome/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'ithome.com': { + _name: 'IT之家', + '.': [ + { + title: '专题', + docs: 'https://docs.rsshub.app/new-media.html#it-zhi-jia-zhuan-ti', + source: ['/zt/:id'], + target: '/ithome/zt/:id', + }, + ], + }, +}; diff --git a/lib/v2/ithome/router.js b/lib/v2/ithome/router.js new file mode 100644 index 0000000000..18214a15bd --- /dev/null +++ b/lib/v2/ithome/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/zt/:id', require('./zt')); +}; diff --git a/lib/v2/ithome/zt.js b/lib/v2/ithome/zt.js new file mode 100644 index 0000000000..0d1d5a5f9a --- /dev/null +++ b/lib/v2/ithome/zt.js @@ -0,0 +1,54 @@ +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 id = ctx.params.id; + + const rootUrl = 'https://www.ithome.com'; + const currentUrl = `${rootUrl}/zt/${id}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.newsbody a') + .map((_, item) => { + item = $(item); + + return { + title: item.text(), + link: item.attr('href'), + }; + }) + .get(); + + const items = await Promise.all( + list.map((item) => + ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = cheerio.load(detailResponse.data); + + item.description = content('.post_content').html(); + item.author = content('#author_baidu').text().replace('作者:', ''); + item.pubDate = timezone(parseDate(content('#pubtime_baidu').text()), +8); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${$('title').text()} - IT之家`, + link: currentUrl, + item: items, + }; +};