From 3159abe9cff2a38529903f6aba2a4d22b119eabc Mon Sep 17 00:00:00 2001 From: miles Date: Tue, 13 Sep 2022 11:57:07 +0000 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20iThome=20=E5=8F=B0?= =?UTF-8?q?=E7=81=A3=E6=96=B0=E8=81=9E=20(#10771)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 11 +++++++++++ lib/v2/ithome/maintainer.js | 1 + lib/v2/ithome/radar.js | 11 +++++++++++ lib/v2/ithome/router.js | 1 + lib/v2/ithome/tw/feeds.js | 38 +++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 lib/v2/ithome/tw/feeds.js diff --git a/docs/new-media.md b/docs/new-media.md index a9ca613b5..7f149add7 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -695,6 +695,17 @@ Tag +## iThome 台灣 + +### Feeds + + + +| 新聞 | AI | Cloud | DevOps | 資安 | +| ---- | -------- | ----- | ------ | -------- | +| news | big-data | cloud | devops | security | + + ## KBS ### News diff --git a/lib/v2/ithome/maintainer.js b/lib/v2/ithome/maintainer.js index 55f9dff34..67dae9e9a 100644 --- a/lib/v2/ithome/maintainer.js +++ b/lib/v2/ithome/maintainer.js @@ -1,6 +1,7 @@ module.exports = { '/ranking/:type': ['immmortal', 'luyuhuang'], '/tag/:name': ['Fatpandac'], + '/tw/feeds/:category': ['miles170'], '/zt/:id': ['nczitzk'], '/:caty': ['luyuhuang'], }; diff --git a/lib/v2/ithome/radar.js b/lib/v2/ithome/radar.js index 8b7572fb9..c45eee24a 100644 --- a/lib/v2/ithome/radar.js +++ b/lib/v2/ithome/radar.js @@ -106,4 +106,15 @@ module.exports = { }, ], }, + 'ithome.com.tw': { + _name: 'iThome', + www: [ + { + title: 'Feeds', + docs: 'https://docs.rsshub.app/new-media.html#ithome-tai-wan', + source: ['/:category', '/:category/feeds'], + target: '/ithome/tw/feeds/:category', + }, + ], + }, }; diff --git a/lib/v2/ithome/router.js b/lib/v2/ithome/router.js index 87eed6fd1..e29785b58 100644 --- a/lib/v2/ithome/router.js +++ b/lib/v2/ithome/router.js @@ -1,6 +1,7 @@ module.exports = function (router) { router.get('/ranking/:type', require('./ranking')); router.get('/tag/:name', require('./tag')); + router.get('/tw/feeds/:category', require('./tw/feeds')); router.get('/zt/:id', require('./zt')); router.get('/:caty', require('./index')); }; diff --git a/lib/v2/ithome/tw/feeds.js b/lib/v2/ithome/tw/feeds.js new file mode 100644 index 000000000..68dfa92a1 --- /dev/null +++ b/lib/v2/ithome/tw/feeds.js @@ -0,0 +1,38 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +module.exports = async (ctx) => { + const baseUrl = 'https://www.ithome.com.tw'; + const currentUrl = `${baseUrl}/${ctx.params.category}/feeds`; + const response = await got(currentUrl); + const $ = cheerio.load(response.data); + const name = $('a.active-trail').text(); + const items = await Promise.all( + $('.title a') + .get() + .map((item) => { + const link = baseUrl + $(item).attr('href'); + return ctx.cache.tryGet(link, async () => { + const response = await got(link); + const $ = cheerio.load(response.data); + return { + title: $('.page-header').text(), + author: $('.author a').text(), + description: $('article').eq(0).html(), + pubDate: timezone(parseDate($('.created').text(), 'YYYY-MM-DD'), +8), + category: name, + link, + }; + }); + }) + ); + + ctx.state.data = { + title: `${name} | iThome`, + link: currentUrl, + description: 'iThome Online 是臺灣第一個網路原生報,提供IT產業即時新聞、企業IT產品報導與測試、技術專題、IT應用報導、IT書訊,以及面向豐富的名家專欄。', + item: items, + }; +};