From ee137c400700533b2effbec4bb81b7b8d8a5e24f Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 25 Mar 2019 13:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=89=BE=E7=91=9E=E4=BA=A7?= =?UTF-8?q?=E4=B8=9A=E7=A0=94=E7=A9=B6=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/unclassified.md | 4 +++ lib/router.js | 3 ++ lib/routes/iresearch/report.js | 63 ++++++++++++++++++++++++++++++++++ lib/utils/date.js | 3 ++ 4 files changed, 73 insertions(+) create mode 100644 lib/routes/iresearch/report.js diff --git a/docs/unclassified.md b/docs/unclassified.md index b78a8e189..bb0562597 100644 --- a/docs/unclassified.md +++ b/docs/unclassified.md @@ -457,3 +457,7 @@ type 为 all 时,category 参数不支持 cost 和 free + +### 艾瑞 + + diff --git a/lib/router.js b/lib/router.js index 43925dace..1f9ddba63 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1210,4 +1210,7 @@ router.get('/zju/career/:type', require('./routes/universities/zju/career')); router.get('/infoq/recommend', require('./routes/infoq/recommend')); router.get('/infoq/topic/:id', require('./routes/infoq/topic')); +// 艾瑞 +router.get('/iresearch/report', require('./routes/iresearch/report')); + module.exports = router; diff --git a/lib/routes/iresearch/report.js b/lib/routes/iresearch/report.js new file mode 100644 index 000000000..60aa90184 --- /dev/null +++ b/lib/routes/iresearch/report.js @@ -0,0 +1,63 @@ +const axios = require('../../utils/axios'); +const date = require('../../utils/date'); +async function processReport(item, ctx) { + const apiUrl = `http://www.iresearch.com.cn/Detail/reportM?id=${item.NewsId}&isfree=0`; + const pageUrl = 'http://www.iresearch.com.cn/m/report.shtml'; + const cache = await ctx.cache.get(apiUrl); + if (cache) { + return cache; + } + const response = await axios({ + method: 'get', + url: apiUrl, + headers: { + Referer: pageUrl, + 'Content-Type': 'application/json', + }, + }); + + const data = response.data.List[0]; + let description = data.Content + '
'; + const reportId = data.id; + + for (let i = 1; i <= data.PagesCount; i++) { + description += `
`; + } + + await ctx.cache.set(apiUrl, description, 24 * 60 * 60); + return description; +} + +module.exports = async (ctx) => { + const apiUrl = 'http://www.iresearch.com.cn/products/GetReportList?classId=&fee=0&date=&lastId=&pageSize=6'; + const pageUrl = 'http://www.iresearch.com.cn/m/report.shtml'; + + const resp = await axios({ + method: 'get', + url: apiUrl, + headers: { + Referer: pageUrl, + 'Content-Type': 'application/json', + }, + }); + + const list = resp.data.List; + const items = await Promise.all( + list.map(async (item) => { + const other = await processReport(item, ctx); + return { + title: item.Title, + description: other, + pubDate: date(item.Uptime), + link: item.VisitUrl, + }; + }) + ); + + ctx.state.data = { + title: '艾瑞产业研究报告', + link: pageUrl, + description: '艾瑞产业研究报告', + item: items, + }; +}; diff --git a/lib/utils/date.js b/lib/utils/date.js index c235f2ab0..acf662173 100644 --- a/lib/utils/date.js +++ b/lib/utils/date.js @@ -50,6 +50,9 @@ module.exports = (html, timeZone = -serverOffset) => { } else if (/(\d+)-(\d+)/.exec(html)) { math = /(\d+)-(\d+)/.exec(html); date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2]); + } else if (/(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/.exec(html)) { + math = /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/.exec(html); + date = new Date(math[1], parseInt(math[2]) - 1, math[3], math[4], math[5], math[6]); } else if (/(\d+):(\d+)/.exec(html)) { math = /(\d+):(\d+)/.exec(html); date = new Date(date.getFullYear(), date.getMonth(), date.getDate(), math[1], math[2]);