From 7bcb65ea257eb6d5d07fea4dc3a44625ac21a881 Mon Sep 17 00:00:00 2001 From: Fatpandac <1779196284@qq.com> Date: Tue, 8 Feb 2022 20:20:54 +0800 Subject: [PATCH] fix(route): change iresearch's API URL and refactor to V2 (#9019) * Fix(route): change iresearch's API URL and refactor to V2 * Fix(route): add tryGet and timezone --- docs/other.md | 2 +- lib/router.js | 3 -- lib/routes/iresearch/report.js | 63 --------------------------- lib/v2/iresearch/maintainer.js | 3 ++ lib/v2/iresearch/radar.js | 13 ++++++ lib/v2/iresearch/report.js | 60 +++++++++++++++++++++++++ lib/v2/iresearch/router.js | 3 ++ lib/v2/iresearch/templates/report.art | 5 +++ 8 files changed, 85 insertions(+), 67 deletions(-) delete mode 100644 lib/routes/iresearch/report.js create mode 100644 lib/v2/iresearch/maintainer.js create mode 100644 lib/v2/iresearch/radar.js create mode 100644 lib/v2/iresearch/report.js create mode 100644 lib/v2/iresearch/router.js create mode 100644 lib/v2/iresearch/templates/report.art diff --git a/docs/other.md b/docs/other.md index 4c9c2ace90..cc16f58515 100644 --- a/docs/other.md +++ b/docs/other.md @@ -326,7 +326,7 @@ type 为 all 时,category 参数不支持 cost 和 free ### 产业研究报告 - + ## 爱 Q 生活网 diff --git a/lib/router.js b/lib/router.js index dcd742928d..7060899bcd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1546,9 +1546,6 @@ router.get('/infoq/topic/:id', lazyloadRouteHandler('./routes/infoq/topic')); // checkee router.get('/checkee/:dispdate', lazyloadRouteHandler('./routes/checkee/index')); -// 艾瑞 -router.get('/iresearch/report', lazyloadRouteHandler('./routes/iresearch/report')); - // ZAKER migrated to v2 // router.get('/zaker/:type/:id', lazyloadRouteHandler('./routes/zaker/source')); // router.get('/zaker/focusread', lazyloadRouteHandler('./routes/zaker/focusread')); diff --git a/lib/routes/iresearch/report.js b/lib/routes/iresearch/report.js deleted file mode 100644 index 6ba902a96b..0000000000 --- a/lib/routes/iresearch/report.js +++ /dev/null @@ -1,63 +0,0 @@ -const got = require('@/utils/got'); -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 got({ - 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 += `
`; - } - - ctx.cache.set(apiUrl, description); - 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 got({ - 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/v2/iresearch/maintainer.js b/lib/v2/iresearch/maintainer.js new file mode 100644 index 0000000000..407334da12 --- /dev/null +++ b/lib/v2/iresearch/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/report': ['brilon', 'Fatpandac'], +}; diff --git a/lib/v2/iresearch/radar.js b/lib/v2/iresearch/radar.js new file mode 100644 index 0000000000..e96a6e00d5 --- /dev/null +++ b/lib/v2/iresearch/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'iresearch.com.cn': { + _name: '艾瑞', + www: [ + { + title: '研究报告', + docs: 'https://docs.rsshub.app/journal.html#ieee-xplore', + source: ['/report.shtml'], + target: '/iresearch/report', + }, + ], + }, +}; diff --git a/lib/v2/iresearch/report.js b/lib/v2/iresearch/report.js new file mode 100644 index 0000000000..3c34ad4f21 --- /dev/null +++ b/lib/v2/iresearch/report.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); + +function processReport(item, ctx) { + const apiUrl = `https://www.iresearch.com.cn/api/Detail/reportM?id=${item.NewsId}&isfree=0`; + const pageUrl = 'https://www.iresearch.com.cn/m/report.shtml'; + + const description = ctx.cache.tryGet(apiUrl, async () => { + const response = await got({ + method: 'get', + url: apiUrl, + headers: { + Referer: pageUrl, + 'Content-Type': 'application/json', + }, + }); + const data = response.data.List[0]; + + return art(path.join(__dirname, 'templates/report.art'), { + data, + }); + }); + + return description; +} + +module.exports = async (ctx) => { + const limit = isNaN(parseInt(ctx.query.limit)) ? 20 : parseInt(ctx.query.limit); + const apiUrl = `https://www.iresearch.com.cn/api/products/GetReportList?fee=0&date=&lastId=&pageSize=${limit}`; + const pageUrl = 'https://www.iresearch.com.cn/m/report.shtml'; + + const resp = await got({ + 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) => ({ + title: item.Title, + description: await processReport(item, ctx), + pubDate: timezone(parseDate(item.Uptime), +8), + link: item.VisitUrl, + })) + ); + + ctx.state.data = { + title: '艾瑞产业研究报告', + link: pageUrl, + description: '艾瑞产业研究报告', + item: items, + }; +}; diff --git a/lib/v2/iresearch/router.js b/lib/v2/iresearch/router.js new file mode 100644 index 0000000000..a7bfc0bbde --- /dev/null +++ b/lib/v2/iresearch/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/report', require('./report')); +}; diff --git a/lib/v2/iresearch/templates/report.art b/lib/v2/iresearch/templates/report.art new file mode 100644 index 0000000000..8b162f6cd8 --- /dev/null +++ b/lib/v2/iresearch/templates/report.art @@ -0,0 +1,5 @@ +{{@ data.Content }} +<% for (let i = 1; i <= data.PagesCount; i++){ %> + +<% } %> +