From daf95a4709d0e09b60ff25f00a3eb06d44c36e7b Mon Sep 17 00:00:00 2001 From: linbuxiao <56839018+linbuxiao@users.noreply.github.com> Date: Sat, 27 Nov 2021 15:54:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(router):=20timednews=20=E6=97=B6=E5=88=BB?= =?UTF-8?q?=E6=96=B0=E9=97=BB=20(#8279)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tony --- docs/new-media.md | 15 +++++ lib/v2/timednews/maintainer.js | 3 + lib/v2/timednews/news.js | 106 +++++++++++++++++++++++++++++++++ lib/v2/timednews/radar.js | 57 ++++++++++++++++++ lib/v2/timednews/router.js | 3 + 5 files changed, 184 insertions(+) create mode 100644 lib/v2/timednews/maintainer.js create mode 100644 lib/v2/timednews/news.js create mode 100644 lib/v2/timednews/radar.js create mode 100644 lib/v2/timednews/router.js diff --git a/docs/new-media.md b/docs/new-media.md index 764ce03d12a499..cce548e6c9d224 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2985,3 +2985,18 @@ QueryString: | | zh-hk | zh-tw | + +## 时刻新闻 + +### 新闻 + + + +子分类 + +| 全部 | 时政 | 财经 | 科技 | 社会 | 体娱 | 国际 | 美国 | 中国 | 欧洲 | 评论 | +|-----|----------------|---------|------------|-------|--------|---------------|-----|-----|--------|----------| +| all | currentAffairs | finance | technology | social| sports | international | usa | cn | europe | comments | + + + diff --git a/lib/v2/timednews/maintainer.js b/lib/v2/timednews/maintainer.js new file mode 100644 index 00000000000000..ab76f5cf91ba00 --- /dev/null +++ b/lib/v2/timednews/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/:type?': ['linbuxiao'], +}; diff --git a/lib/v2/timednews/news.js b/lib/v2/timednews/news.js new file mode 100644 index 00000000000000..f751ebb25bdc71 --- /dev/null +++ b/lib/v2/timednews/news.js @@ -0,0 +1,106 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +const BASE = 'https://www.timednews.com/topic'; + +const PATH_LIST = { + all: { + name: '全部', + path: 'cat/1.html', + }, + currentAffairs: { + name: '时政', + path: 'subcat/1.html', + }, + finance: { + name: '财经', + path: 'subcat/2.html', + }, + technology: { + name: '科技', + path: 'subcat/3.html', + }, + social: { + name: '社会', + path: 'subcat/4.html', + }, + sports: { + name: '体娱', + path: 'subcat/5.html', + }, + international: { + name: '国际', + path: 'subcat/6.html', + }, + usa: { + name: '美国', + path: 'subcat/7.html', + }, + cn: { + name: '中国', + path: 'subcat/8.html', + }, + europe: { + name: '欧洲', + path: 'subcat/9.html', + }, + comments: { + name: '评论', + path: 'subcat/14.html', + }, +}; + +module.exports = async (ctx) => { + const type = ctx.params.type ?? 'all'; + const url = `${BASE}/${PATH_LIST[type].path}`; + const res = await got({ + method: 'get', + url, + }); + + const $ = cheerio.load(res.data); + + const list = $('#content li') + .map((i, e) => { + const c = cheerio.load(e); + return { + title: c('a').text().trim(), + link: c('a').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 c = cheerio.load(detailResponse.data, { decodeEntities: false }); + c('.event .twitter').remove(); + item.pubDate = parseDate(c('.datetime #publishdate').text(), 'YYYY-MM-DD'); + item.author = c('.datetime #author').text(); + item.description = c('.event').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: '时刻新闻', + link: url, + description: `时刻新闻 ${PATH_LIST[type].name}`, + item: items, + }; + + ctx.state.json = { + title: '时刻新闻', + link: url, + description: `时刻新闻 ${PATH_LIST[type].name}`, + item: items, + }; +}; diff --git a/lib/v2/timednews/radar.js b/lib/v2/timednews/radar.js new file mode 100644 index 00000000000000..0f5ed5ed8fdd6b --- /dev/null +++ b/lib/v2/timednews/radar.js @@ -0,0 +1,57 @@ +module.exports = { + 'timednews.com': { + _name: '时刻新闻', + '.': [ + { + title: '新闻', + docs: 'https://docs.rsshub.app/new-media.html#shi-ke-xin-wen', + source: ['/topic/:type/:id'], + target: ({ type, id }) => { + let name = ''; + if (type === 'cat') { + if (id === '1') { + name = 'all'; + } + } else if (type === 'subcat') { + switch (id) { + case '1': + name = 'currentAffairs'; + break; + case '2': + name = 'finance'; + break; + case '3': + name = 'technology'; + break; + case '4': + name = 'social'; + break; + case '5': + name = 'sports'; + break; + case '6': + name = 'international'; + break; + case '7': + name = 'usa'; + break; + case '8': + name = 'cn'; + break; + case '9': + name = 'europe'; + break; + case '14': + name = 'comments'; + break; + default: + break; + } + } + + return `/timednews/news/${name}`; + }, + }, + ], + }, +}; diff --git a/lib/v2/timednews/router.js b/lib/v2/timednews/router.js new file mode 100644 index 00000000000000..c36fd4f1c82e1f --- /dev/null +++ b/lib/v2/timednews/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news/:type?', require('./news')); +};