From 0321afc91369ca687e7eec217d50c911d1bb294a Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 31 Jul 2022 06:09:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BF=AE=E5=A4=8Dinfoq=20recomme?= =?UTF-8?q?nd=E6=8E=A5=E5=8F=A3=E8=8E=B7=E5=8F=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=89=87=E6=AE=B5=E6=BA=90=E6=95=B0=E6=8D=AE=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E4=BC=9A=E5=AF=BC=E8=87=B4=E6=8A=A5=E9=94=99=E7=9A=84=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E9=94=99=E8=AF=AF=20(#10358)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 修复infoq recommend接口获取代码片段源数据为空会导致报错的边界错误 * refactor: migrate to v2 --- docs/new-media.md | 2 +- lib/router.js | 4 ++-- lib/v2/infoq/maintainer.js | 4 ++++ lib/v2/infoq/radar.js | 19 +++++++++++++++++++ lib/{routes => v2}/infoq/recommend.js | 9 +++------ lib/v2/infoq/router.js | 4 ++++ lib/{routes => v2}/infoq/topic.js | 23 ++++++++++------------- lib/{routes => v2}/infoq/utils.js | 23 +++++++++++++---------- 8 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 lib/v2/infoq/maintainer.js create mode 100644 lib/v2/infoq/radar.js rename lib/{routes => v2}/infoq/recommend.js (75%) create mode 100644 lib/v2/infoq/router.js rename lib/{routes => v2}/infoq/topic.js (64%) rename lib/{routes => v2}/infoq/utils.js (81%) diff --git a/docs/new-media.md b/docs/new-media.md index d2f089aca9..bf7debf1a6 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -659,7 +659,7 @@ Tag ### 话题 - + ## IT 之家 diff --git a/lib/router.js b/lib/router.js index 0788848d29..be86f61ee7 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1466,8 +1466,8 @@ router.get('/zucc/cssearch/latest/:webVpn/:key', lazyloadRouteHandler('./routes/ router.get('/ccnu/career', lazyloadRouteHandler('./routes/universities/ccnu/career')); // Infoq -router.get('/infoq/recommend', lazyloadRouteHandler('./routes/infoq/recommend')); -router.get('/infoq/topic/:id', lazyloadRouteHandler('./routes/infoq/topic')); +// router.get('/infoq/recommend', lazyloadRouteHandler('./routes/infoq/recommend')); +// router.get('/infoq/topic/:id', lazyloadRouteHandler('./routes/infoq/topic')); // checkee router.get('/checkee/:dispdate', lazyloadRouteHandler('./routes/checkee/index')); diff --git a/lib/v2/infoq/maintainer.js b/lib/v2/infoq/maintainer.js new file mode 100644 index 0000000000..2538882c70 --- /dev/null +++ b/lib/v2/infoq/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/recommend': ['brilon'], + '/topic/:id': ['brilon'], +}; diff --git a/lib/v2/infoq/radar.js b/lib/v2/infoq/radar.js new file mode 100644 index 0000000000..e8caa6ac63 --- /dev/null +++ b/lib/v2/infoq/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'infoq.cn': { + _name: 'InfoQ 中文', + '.': [ + { + title: '推荐', + docs: 'https://docs.rsshub.app/new-media.html#infoq-zhong-wen', + source: ['/'], + target: '/infoq/recommend', + }, + { + title: '话题', + docs: 'https://docs.rsshub.app/new-media.html#infoq-zhong-wen', + source: ['/topic/:id'], + target: '/infoq/topic/:id', + }, + ], + }, +}; diff --git a/lib/routes/infoq/recommend.js b/lib/v2/infoq/recommend.js similarity index 75% rename from lib/routes/infoq/recommend.js rename to lib/v2/infoq/recommend.js index 3bfed98876..9296f174b5 100644 --- a/lib/routes/infoq/recommend.js +++ b/lib/v2/infoq/recommend.js @@ -5,14 +5,12 @@ module.exports = async (ctx) => { const apiUrl = 'https://www.infoq.cn/public/v1/my/recommond'; const pageUrl = 'https://www.infoq.cn'; - const resp = await got({ - method: 'post', - url: apiUrl, + const resp = await got.post(apiUrl, { headers: { Referer: pageUrl, }, json: { - size: 5, + size: ctx.query.limit ? Number(ctx.query.limit) : 30, }, }); @@ -20,9 +18,8 @@ module.exports = async (ctx) => { const items = await utils.ProcessFeed(data, ctx.cache); ctx.state.data = { - title: 'InfoQ推荐', + title: 'InfoQ 推荐', link: pageUrl, - description: 'InfoQ推荐', item: items, }; }; diff --git a/lib/v2/infoq/router.js b/lib/v2/infoq/router.js new file mode 100644 index 0000000000..3d09919156 --- /dev/null +++ b/lib/v2/infoq/router.js @@ -0,0 +1,4 @@ +module.exports = (router) => { + router.get('/recommend', require('./recommend')); + router.get('/topic/:id', require('./topic')); +}; diff --git a/lib/routes/infoq/topic.js b/lib/v2/infoq/topic.js similarity index 64% rename from lib/routes/infoq/topic.js rename to lib/v2/infoq/topic.js index 004adcce85..6017214005 100644 --- a/lib/routes/infoq/topic.js +++ b/lib/v2/infoq/topic.js @@ -2,32 +2,28 @@ const got = require('@/utils/got'); const utils = require('./utils'); module.exports = async (ctx) => { - const paramId = parseInt(ctx.params.id); + const paramId = ctx.params.id; const apiUrl = 'https://www.infoq.cn/public/v1/article/getList'; const infoUrl = 'https://www.infoq.cn/public/v1/topic/getInfo'; const pageUrl = `https://www.infoq.cn/topic/${paramId}`; - const info = await got({ - method: 'post', - url: infoUrl, + const infoBody = isNaN(paramId) ? { alias: paramId } : { id: parseInt(paramId) }; + + const info = await got.post(infoUrl, { headers: { Referer: pageUrl, }, - json: { - id: paramId, - }, + json: infoBody, }); const topicName = info.data.data.name; const type = info.data.data.type; - const resp = await got({ - method: 'post', - url: apiUrl, + const resp = await got.post(apiUrl, { headers: { Referer: pageUrl, }, json: { - size: 5, + size: ctx.query.limit ? Number(ctx.query.limit) : 30, type, id: paramId, }, @@ -37,9 +33,10 @@ module.exports = async (ctx) => { const items = await utils.ProcessFeed(data, ctx.cache); ctx.state.data = { - title: `InfoQ话题 - ${topicName}`, + title: `InfoQ 话题 - ${topicName}`, + description: info.data.data.desc, + image: info.data.data.cover, link: pageUrl, - description: `InfoQ话题 - ${topicName}`, item: items, }; }; diff --git a/lib/routes/infoq/utils.js b/lib/v2/infoq/utils.js similarity index 81% rename from lib/routes/infoq/utils.js rename to lib/v2/infoq/utils.js index 5bdddf268a..468030bbb6 100644 --- a/lib/routes/infoq/utils.js +++ b/lib/v2/infoq/utils.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); const ProcessFeed = async (list, cache) => { const detailUrl = 'https://www.infoq.cn/public/v1/article/getDetail'; @@ -8,9 +9,7 @@ const ProcessFeed = async (list, cache) => { const uuid = e.uuid; const single = await cache.tryGet(uuid, async () => { const link = `https://www.infoq.cn/article/${uuid}`; - const resp = await got({ - method: 'post', - url: detailUrl, + const resp = await got.post(detailUrl, { headers: { Referer: link, }, @@ -21,19 +20,19 @@ const ProcessFeed = async (list, cache) => { const data = resp.data.data; const author = data.author ? data.author.map((p) => p.nickname).join(',') : data.no_author; - const pubDate = new Date(); - pubDate.setTime(data.publish_time); + const category = e.topic.map((t) => t.name).concat(e.label.map((l) => l.name)); return { title: data.article_title, description: parseContent(data.content), - pubDate, + pubDate: parseDate(e.publish_time, 'x'), + category, author, link, }; }); - return Promise.resolve(single); + return single; }) ); @@ -67,9 +66,13 @@ const parseToSimpleTexts = (content) => return ``; }, codeblock: () => { - const lang = i.attrs.lang; - const code = parseToSimpleText(i.content); - return `${code}`; + if (i.content) { + const lang = i.attrs.lang; + const code = parseToSimpleText(i.content); + return `${code}`; + } else { + return ''; + } }, link: () => { const href = i.attrs.href;