From 219ea0dcd3a389fab389b94d3c5857149dc8fd51 Mon Sep 17 00:00:00 2001 From: Jin Date: Tue, 16 Jul 2019 12:07:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=95=81=E5=AE=A2?= =?UTF-8?q?=E7=BD=91im2maker=20RSS=20(#2607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/other.md | 12 ++++++++ lib/router.js | 3 ++ lib/routes/im2maker/index.js | 60 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 lib/routes/im2maker/index.js diff --git a/docs/other.md b/docs/other.md index 67711e8b4..09d30a47a 100644 --- a/docs/other.md +++ b/docs/other.md @@ -606,6 +606,18 @@ type 为 all 时,category 参数不支持 cost 和 free +## 镁客网 im2maker + +### 镁客网频道 + + + +| 最新文章 | 行业快讯 | 行业观察 | 镁客请讲 | 硬科技 100 人 | 投融界 | 万象 | +| -------- | -------- | -------- | -------- | ------------- | -------- | ---------- | +| 默认空 | fresh | industry | talk | intech | investor | everything | + + + ## 每日安全 ### 推送 diff --git a/lib/router.js b/lib/router.js index 57ab3dffb..f41320d93 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1495,6 +1495,9 @@ router.get('/aptonic/action', require('./routes/aptonic/action')); // 印记中文周刊 router.get('/docschina/jsweekly', require('./routes/docschina/jsweekly')); +// im2maker +router.get('/im2maker/:channel?', require('./routes/im2maker/index')); + // 巨潮资讯 router.get('/cninfo/stock_announcement/:code', require('./routes/cninfo/stock_announcement')); diff --git a/lib/routes/im2maker/index.js b/lib/routes/im2maker/index.js new file mode 100644 index 000000000..73e1a2c34 --- /dev/null +++ b/lib/routes/im2maker/index.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const category = { + default: '最新文章', + fresh: '行业快讯', + industry: '行业观察', + talk: '镁客请讲', + intech: '硬科技100人', + investor: '投融界', + everything: '万象', +}; + +module.exports = async (ctx) => { + let host = 'https://www.im2maker.com'; + + let channel = 'default'; + if (ctx.params.channel) { + channel = ctx.params.channel.toLowerCase(); + host = `${host}/category/${channel}`; + } + const titleCategory = category[channel]; + + const response = await got.get(host); + + const $ = cheerio.load(response.data); + const hrefs = $("a[class='title info_flow_news_title']") + .map((i, e) => e.attribs.href) + .get(); + + const out = await Promise.all( + hrefs.map(async (itemUrl) => { + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await got.get(itemUrl); + const $ = cheerio.load(response.data); + + const item = { + title: $('title').text(), + link: itemUrl, + author: $('.single-post-header-meta .author .name').text(), + description: $('.article').html(), + pubDate: $('.single-post-header-meta .author .item').text(), + }; + + ctx.cache.set(itemUrl, JSON.stringify(item)); + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `镁客网 ${titleCategory}`, + link: host, + description: '镁客网', + item: out, + }; +};