From a1a62813e2cd71d75caa36ad8202718cb344906f Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 15 Mar 2022 23:10:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E5=B7=A5=E4=BA=BA=E5=87=BA=E7=89=88=E7=A4=BE=E6=96=B0=E9=97=BB?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=20(#7303)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 中国工人出版社新闻中心 * refactor: migrate to v2 * fix: parseDate typo in router.js Co-authored-by: TonyRL --- docs/new-media.md | 12 ++++++++ lib/v2/wp-china/maintainer.js | 3 ++ lib/v2/wp-china/news.js | 57 +++++++++++++++++++++++++++++++++++ lib/v2/wp-china/radar.js | 13 ++++++++ lib/v2/wp-china/router.js | 3 ++ 5 files changed, 88 insertions(+) create mode 100644 lib/v2/wp-china/maintainer.js create mode 100644 lib/v2/wp-china/news.js create mode 100644 lib/v2/wp-china/radar.js create mode 100644 lib/v2/wp-china/router.js diff --git a/docs/new-media.md b/docs/new-media.md index 4e9c9434d..9e19a69cd 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -3784,6 +3784,18 @@ column 为 third 时可选的 category: +## 中国工人出版社 + +### 新闻中心 + + + +| 最新资讯 | 专题报道 | +| ------ | ------- | +| latest | stories | + + + ## 中国机械工程学会 ### 学会新闻 diff --git a/lib/v2/wp-china/maintainer.js b/lib/v2/wp-china/maintainer.js new file mode 100644 index 000000000..d4350891f --- /dev/null +++ b/lib/v2/wp-china/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/:category?': ['nczitzk'], +}; diff --git a/lib/v2/wp-china/news.js b/lib/v2/wp-china/news.js new file mode 100644 index 000000000..8ad96177c --- /dev/null +++ b/lib/v2/wp-china/news.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +const config = { + latest: 'list-35b3413e4c03411d8f3726162570804f', + stories: 'list-7224ef82da1b498eb24046a6c56ff566', +}; + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'latest'; + + const rootUrl = 'http://www.wp-china.com'; + const currentUrl = `${rootUrl}/f/${config[category]}.html`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.new_list') + .slice(0, 10) + .map((_, item) => { + item = $(item); + return { + title: item.children('.fb').text(), + link: `${rootUrl}${item.parent().attr('href')}`, + pubDate: timezone(parseDate(item.children('.grey').text()), +8), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('.wb100').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text().replace(' - Powered By gobestsoft', ''), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/wp-china/radar.js b/lib/v2/wp-china/radar.js new file mode 100644 index 000000000..55e46037f --- /dev/null +++ b/lib/v2/wp-china/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'wp-china.com': { + _name: '中国工人出版社', + '.': [ + { + title: '新闻中心', + docs: 'https://docs.rsshub.app/new-media.html#zhong-guo-gong-ren-chu-ban-she', + source: ['/'], + target: '/wp-china/news', + }, + ], + }, +}; diff --git a/lib/v2/wp-china/router.js b/lib/v2/wp-china/router.js new file mode 100644 index 000000000..1b7e39450 --- /dev/null +++ b/lib/v2/wp-china/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/news/:category?', require('./news')); +};