From 9091978f9076f716229878f9fdfd53c3792db2d9 Mon Sep 17 00:00:00 2001 From: Ethan H Date: Mon, 8 Apr 2019 11:24:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A021=E8=B4=A2=E7=BB=8F=E7=9A=84?= =?UTF-8?q?=E9=A2=91=E9=81=93RSS=20(#1855)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/traditional-media.md | 4 ++ lib/router.js | 3 ++ lib/routes/21caijing/channel.js | 74 +++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 lib/routes/21caijing/channel.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index b5a3b3e75..3ec2e8caf 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -209,3 +209,7 @@ category 对应的关键词有 ## 半月谈 + +## 21 财经 + + diff --git a/lib/router.js b/lib/router.js index cbce3b730..ae40bc53b 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1237,4 +1237,7 @@ router.get('/mobdata/report', require('./routes/mobdata/report')); // 谷雨 router.get('/tencent/guyu/channel/:name', require('./routes/tencent/guyu/channel')); +// 21财经 +router.get('/21caijing/channel/:name', require('./routes/21caijing/channel')); + module.exports = router; diff --git a/lib/routes/21caijing/channel.js b/lib/routes/21caijing/channel.js new file mode 100644 index 000000000..ba84e5949 --- /dev/null +++ b/lib/routes/21caijing/channel.js @@ -0,0 +1,74 @@ +const axios = require('../../utils/axios'); +const date = require('../../utils/date'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const channel = ctx.params.name; + const pageUrl = `https://m.21jingji.com/channel/${channel}`; + + const response = await axios({ + method: 'get', + url: pageUrl, + }); + + const $ = cheerio.load(response.data); + const channelName = $(`div.nav a[href='/channel/${channel}']`).text(); + const list = $('div.news_list>a') + .slice(0, 5) + .map((i, e) => $(e).attr('href')) + .get(); + + const out = await Promise.all( + list.map(async (link) => { + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await axios({ + method: 'get', + url: link, + headers: { + Referer: pageUrl, + }, + }); + + const $ = cheerio.load(response.data); + + $('img').each((index, elem) => { + const $elem = $(elem); + const src = $elem.attr('data-original'); + if (src) { + $elem.attr('src', src); + $elem.removeAttr('data-original'); + } + }); + + const div = $('div.editor'); + if (div) { + div.remove(); + } + + const title = $('h1').text(); + const dateStr = $('div.newsDate').text(); + const content = $('div.txtContent').html(); + + const single = { + pubDate: date(dateStr), + link: link, + title: title, + description: content, + }; + + ctx.cache.set(link, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: '21财经-' + channelName, + link: pageUrl, + description: '21财经-' + channelName, + item: out, + }; +};