From 70dcecc69f811e746f93b768954029dcf1c221ec Mon Sep 17 00:00:00 2001 From: Zhengyuan Wei Date: Tue, 9 Jun 2020 11:23:13 +0800 Subject: [PATCH] feat: add futunn lighlights (#4945) --- docs/finance.md | 6 ++++ lib/router.js | 3 ++ lib/routes/futunn/highlights.js | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/routes/futunn/highlights.js diff --git a/docs/finance.md b/docs/finance.md index f13903730..2fc47705c 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -36,6 +36,12 @@ pageClass: routes +## 富途牛牛 + +### 要闻 + + + ## 淘股吧股票论坛 ### 论坛总版 diff --git a/lib/router.js b/lib/router.js index 52b3efdf3..a7595eab5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2838,4 +2838,7 @@ router.get('/gov/chinatax/latest', require('./routes/gov/chinatax/latest')); // 荔枝FM router.get('/lizhi/user/:id', require('./routes/lizhi/user')); +// 富途牛牛 +router.get('/futunn/highlights', require('./routes/futunn/highlights')); + module.exports = router; diff --git a/lib/routes/futunn/highlights.js b/lib/routes/futunn/highlights.js new file mode 100644 index 000000000..e33205ecb --- /dev/null +++ b/lib/routes/futunn/highlights.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const date_util = require('@/utils/date'); + +module.exports = async (ctx) => { + const link = `https://news.futunn.com/main`; + + const response = await got.get(link); + const $ = cheerio.load(response.data); + // extract json from 'window._params = {...};' + const params = $('script:not([src])').html().trim().slice(17, -1); + + const newslist = JSON.parse(params).preList; + + const out = await Promise.all( + newslist.map(async (info) => { + const title = info.title; + const itemUrl = info.url; + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got({ + url: itemUrl, + method: 'get', + headers: { + Referer: link, + }, + }); + + const $ = cheerio.load(response.data); + + const description = $('div.newsDetailBox div.main div.inner').html() || '原文已被删除'; + + const date = $('div.timeBar').text(); + + const single = { + title: title, + link: itemUrl, + description: description, + pubDate: date_util(date, 8), + }; + ctx.cache.set(itemUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `富途牛牛要闻`, + link: link, + item: out, + }; +};