From 0022ca1d2e07cbf604ee57baefb060293a6c96aa Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Tue, 22 Feb 2022 20:30:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20DT=E8=B4=A2=E7=BB=8F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=B4=9E=E5=AF=9F=20(#7672)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add DT财经数据洞察 * fix typo * refactor: migrate to v2 Co-authored-by: TonyRL --- docs/finance.md | 10 ++++++ lib/router.js | 5 +-- lib/{routes => v2}/dtcj/datahero.js | 5 +-- lib/v2/dtcj/datainsight.js | 53 +++++++++++++++++++++++++++++ lib/v2/dtcj/maintainer.js | 4 +++ lib/v2/dtcj/radar.js | 25 ++++++++++++++ lib/v2/dtcj/router.js | 4 +++ 7 files changed, 102 insertions(+), 4 deletions(-) rename lib/{routes => v2}/dtcj/datahero.js (90%) create mode 100644 lib/v2/dtcj/datainsight.js create mode 100644 lib/v2/dtcj/maintainer.js create mode 100644 lib/v2/dtcj/radar.js create mode 100644 lib/v2/dtcj/router.js diff --git a/docs/finance.md b/docs/finance.md index 0b3b171cd6..423e7cd1ef 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -12,6 +12,16 @@ pageClass: routes ## DT 财经 +### 数据洞察 + + + +| 城数 | NEXT 情报局 | 专业精选 | +| -- | -------- | ---- | +| 3 | 1 | 4 | + + + ### 数据侠专栏 diff --git a/lib/router.js b/lib/router.js index d0d95b6023..ef293356e5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3891,8 +3891,9 @@ router.get('/jlbtc/kyc/:category?', lazyloadRouteHandler('./routes/universities/ router.get('/jlbtc/jwc/:id?', lazyloadRouteHandler('./routes/universities/jlbtc/jwc')); router.get('/jlbtc/:category?', lazyloadRouteHandler('./routes/universities/jlbtc/index')); -// DT 财经 -router.get('/dtcj/datahero/:category?', lazyloadRouteHandler('./routes/dtcj/datahero')); +// DT 财经 migrated to v2 +// router.get('/dtcj/datahero/:category?', lazyloadRouteHandler('./routes/dtcj/datahero')); +// router.get('/dtcj/datainsight/:id?', lazyloadRouteHandler('./routes/dtcj/datainsight')); // 劍心.回憶 router.get('/kenshin/:category?/:type?', lazyloadRouteHandler('./routes/kenshin/index')); diff --git a/lib/routes/dtcj/datahero.js b/lib/v2/dtcj/datahero.js similarity index 90% rename from lib/routes/dtcj/datahero.js rename to lib/v2/dtcj/datahero.js index dc5b6057dd..b1ca837a4c 100644 --- a/lib/routes/dtcj/datahero.js +++ b/lib/v2/dtcj/datahero.js @@ -1,5 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); const titles = { 5: '侠创', @@ -9,7 +10,7 @@ const titles = { }; module.exports = async (ctx) => { - const category = ctx.params.category || ''; + const category = ctx.params.category ?? ''; const rootUrl = 'https://dtcj.com'; const currentUrl = `${rootUrl}/api/v1/data_hero_informations?per=15&page=1&topic_id=${category}`; @@ -22,7 +23,7 @@ module.exports = async (ctx) => { title: item.title, author: item.author, link: `https://dtcj.com/topic/${item.id}`, - pubDate: new Date(item.date).toUTCString(), + pubDate: parseDate(item.date), })); const items = await Promise.all( diff --git a/lib/v2/dtcj/datainsight.js b/lib/v2/dtcj/datainsight.js new file mode 100644 index 0000000000..5824508589 --- /dev/null +++ b/lib/v2/dtcj/datainsight.js @@ -0,0 +1,53 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const id = ctx.params.id ?? ''; + + const rootUrl = 'https://dtcj.com'; + const currentUrl = `${rootUrl}/${id ? `insighttopic/${id}` : 'datainsight'}`; + + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('.info-2_P1UM a') + .slice(0, 10) + .map((_, item) => { + item = $(item); + + return { + title: item.text(), + link: `${rootUrl}${item.attr('href')}`, + }; + }) + .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('.content-3mNFyi').html(); + item.pubDate = parseDate(detailResponse.data.match(/"date":"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}\+\d{2}:\d{2})","thumbnail_url":/)[1]); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/dtcj/maintainer.js b/lib/v2/dtcj/maintainer.js new file mode 100644 index 0000000000..4b9ccc28fd --- /dev/null +++ b/lib/v2/dtcj/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/datahero/:category?': ['nczitzk'], + '/datainsight/:id?': ['nczitzk'], +}; diff --git a/lib/v2/dtcj/radar.js b/lib/v2/dtcj/radar.js new file mode 100644 index 0000000000..5df944e98c --- /dev/null +++ b/lib/v2/dtcj/radar.js @@ -0,0 +1,25 @@ +module.exports = { + 'dtcj.com': { + _name: 'DT 财经', + '.': [ + { + title: '数据侠专栏', + docs: 'https://docs.rsshub.app/finance.html#dt-cai-jing', + source: ['/datahero/topic'], + target: (_params, url) => `/dtcj/datahero/${new URL(url).searchParams.get('topic_id')}`, + }, + { + title: '数据洞察', + docs: 'https://docs.rsshub.app/finance.html#dt-cai-jing', + source: ['/dtcj/datainsight'], + target: '/dtcj/datainsight', + }, + { + title: '数据洞察', + docs: 'https://docs.rsshub.app/finance.html#dt-cai-jing', + source: ['/insighttopic/:id'], + target: '/dtcj/datainsight/:id', + }, + ], + }, +}; diff --git a/lib/v2/dtcj/router.js b/lib/v2/dtcj/router.js new file mode 100644 index 0000000000..8c56982301 --- /dev/null +++ b/lib/v2/dtcj/router.js @@ -0,0 +1,4 @@ +module.exports = (router) => { + router.get('/datahero/:category?', require('./datahero')); + router.get('/datainsight/:id?', require('./datainsight')); +};