From 4e65978b0c3118de9cf552152c23f1e05ea130e4 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Fri, 29 Apr 2022 08:12:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20C114=E9=80=9A=E4=BF=A1?= =?UTF-8?q?=E7=BD=91=E6=BB=9A=E5=8A=A8=E6=96=B0=E9=97=BB=20(#9651)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat(route): add C114通信网滚动新闻 --- docs/new-media.md | 6 +++++ lib/v2/c114/maintainer.js | 3 +++ lib/v2/c114/radar.js | 13 +++++++++ lib/v2/c114/roll.js | 57 +++++++++++++++++++++++++++++++++++++++ lib/v2/c114/router.js | 3 +++ 5 files changed, 82 insertions(+) create mode 100644 lib/v2/c114/maintainer.js create mode 100644 lib/v2/c114/radar.js create mode 100644 lib/v2/c114/roll.js create mode 100644 lib/v2/c114/router.js diff --git a/docs/new-media.md b/docs/new-media.md index a43d2fdbeb..748db81780 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -162,6 +162,12 @@ pageClass: routes +## C114 通信网 + +### 滚动新闻 + + + ## CBNData ### 看点 diff --git a/lib/v2/c114/maintainer.js b/lib/v2/c114/maintainer.js new file mode 100644 index 0000000000..9c2cbaafd7 --- /dev/null +++ b/lib/v2/c114/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/roll': ['nczitzk'], +}; diff --git a/lib/v2/c114/radar.js b/lib/v2/c114/radar.js new file mode 100644 index 0000000000..97a3e58d5f --- /dev/null +++ b/lib/v2/c114/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'c114.com.cn': { + _name: 'C114通信网', + '.': [ + { + title: '滚动新闻', + docs: 'https://docs.rsshub.app/new-media.html#c114-tong-xin-wang-gun-dong-xin-wen', + source: ['/news/roll.asp', '/'], + target: '/c114/roll', + }, + ], + }, +}; diff --git a/lib/v2/c114/roll.js b/lib/v2/c114/roll.js new file mode 100644 index 0000000000..2f68743a57 --- /dev/null +++ b/lib/v2/c114/roll.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const timezone = require('@/utils/timezone'); +const { parseDate } = require('@/utils/parse-date'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const rootUrl = 'https://www.c114.com.cn'; + const currentUrl = `${rootUrl}/news/roll.asp`; + + const response = await got({ + method: 'get', + url: currentUrl, + responseType: 'buffer', + }); + + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + + let items = $('.new_list_c h6 a') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 50) + .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, + responseType: 'buffer', + }); + + const content = cheerio.load(iconv.decode(detailResponse.data, 'gbk')); + + item.description = content('.text').html(); + item.author = content('.author').first().text().replace('C114通信网  ', ''); + item.pubDate = timezone(parseDate(content('.r_time').text()), +8); + item.category = content('meta[name="keywords"]').attr('content').split(','); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/c114/router.js b/lib/v2/c114/router.js new file mode 100644 index 0000000000..44f7186f02 --- /dev/null +++ b/lib/v2/c114/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/roll', require('./roll')); +};