From adcf6eaccd0cdb1b6485aac4aaa3bd57c5d05e03 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 17 Jul 2021 17:08:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20CBNData=E6=B6=88=E8=B4=B9?= =?UTF-8?q?=E7=AB=99=E7=9C=8B=E7=82=B9=20(#7810)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: NeverBehave --- docs/new-media.md | 12 +++++++ lib/router.js | 3 ++ lib/routes/cbndata/information.js | 53 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 lib/routes/cbndata/information.js diff --git a/docs/new-media.md b/docs/new-media.md index e72d61b4049520..b7e34b040b6f8c 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -114,6 +114,18 @@ pageClass: routes +## CBNData + +### 看点 + + + +| 看点 | 餐饮零售 | 美妆个护 | 服饰鞋包 | 家电数码 | 宠物 | 营销 | +| ---- | -------- | -------- | -------- | -------- | ---- | ---- | +| | 2560 | 1 | 2559 | 59 | 2419 | 2484 | + + + ## cfan ### 新闻 diff --git a/lib/router.js b/lib/router.js index fb31bba26dde35..89152ddf299954 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4130,6 +4130,9 @@ router.get('/caus/:category?', require('./routes/caus')); // 摩点 router.get('/modian/zhongchou/:category?/:sort?/:status?', require('./routes/modian/zhongchou')); +// CBNData +router.get('/cbndata/information/:category?', require('./routes/cbndata/information')); + // TANC 艺术新闻 router.get('/tanchinese/:category?', require('./routes/tanchinese')); diff --git a/lib/routes/cbndata/information.js b/lib/routes/cbndata/information.js new file mode 100644 index 00000000000000..db3bf21d09e0b5 --- /dev/null +++ b/lib/routes/cbndata/information.js @@ -0,0 +1,53 @@ +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const category = ctx.params.category || ''; + + const rootUrl = 'https://www.cbndata.com'; + const apiUrl = `${rootUrl}/api/v3/informations?page=1&per_page=10${category ? `&tag_id=${category}` : ''}`; + + const response = await got({ + method: 'get', + url: apiUrl, + }); + + const list = response.data.data.map((item) => ({ + title: item.id, + link: `${rootUrl}/api/v3/informations/show?id=${item.id}`, + pubDate: Date.parse(item.date), + })); + + 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, + }); + + item.link = `${rootUrl}/information/${item.title}`; + + item.title = detailResponse.data.data.title; + item.author = detailResponse.data.data.author; + item.description = detailResponse.data.data.content; + + return item; + }) + ) + ); + + let title = '看点'; + + for (const tag of response.data.home_tags) { + if (tag.id.toString() === category) { + title = tag.name; + break; + } + } + ctx.state.data = { + title: `${title} - CBNData消费站`, + link: `${rootUrl}/information${category ? `?tag_id=${category}` : ''}`, + item: items, + }; +};