From 2f9b999055654c2020c8c7fcfb94cbc164c2875d Mon Sep 17 00:00:00 2001 From: Fatpandac <1779196284@qq.com> Date: Mon, 13 Jun 2022 22:00:48 +0800 Subject: [PATCH] fix(route): hk01 get fulltext and refactor to V2 (#9937) * fix(route): hk01 get fulltext and refactor to V2 * Update lib/v2/hk01/channel.js Co-authored-by: Tony * Update lib/v2/hk01/issue.js Co-authored-by: Tony * fix: teaser pubDate --- docs/traditional-media.md | 10 ++++---- lib/radar-rules.js | 35 ---------------------------- lib/router.js | 10 ++++---- lib/routes/hk01/channel.js | 22 ------------------ lib/routes/hk01/issue.js | 22 ------------------ lib/v2/hk01/channel.js | 40 ++++++++++++++++++++++++++++++++ lib/{routes => v2}/hk01/hot.js | 3 ++- lib/v2/hk01/issue.js | 41 +++++++++++++++++++++++++++++++++ lib/v2/hk01/maintainer.js | 7 ++++++ lib/v2/hk01/radar.js | 37 +++++++++++++++++++++++++++++ lib/v2/hk01/router.js | 7 ++++++ lib/{routes => v2}/hk01/tag.js | 3 ++- lib/{routes => v2}/hk01/zone.js | 5 ++-- 13 files changed, 149 insertions(+), 93 deletions(-) delete mode 100644 lib/routes/hk01/channel.js delete mode 100644 lib/routes/hk01/issue.js create mode 100644 lib/v2/hk01/channel.js rename lib/{routes => v2}/hk01/hot.js (85%) create mode 100644 lib/v2/hk01/issue.js create mode 100644 lib/v2/hk01/maintainer.js create mode 100644 lib/v2/hk01/radar.js create mode 100644 lib/v2/hk01/router.js rename lib/{routes => v2}/hk01/tag.js (86%) rename lib/{routes => v2}/hk01/zone.js (87%) diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 45eb70655..37025c648 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -1528,23 +1528,23 @@ category 对应的关键词有 ### 热门 - + ### 栏目 - + ### 子栏目 - + ### 专题 - + ### 标签 - + ## 香港電台 diff --git a/lib/radar-rules.js b/lib/radar-rules.js index 1e4b4d312..24e743932 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -1589,41 +1589,6 @@ module.exports = { }, ], }, - 'hk01.com': { - _name: '香港01', - www: [ - { - title: '最 Hit', - docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', - source: ['/hot', '/'], - target: '/hk01/hot', - }, - { - title: 'zone', - docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', - source: '/zone/:id/:title', - target: '/hk01/zone/:id', - }, - { - title: 'channel', - docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', - source: '/channel/:id/:title', - target: '/hk01/channel/:id', - }, - { - title: 'issue', - docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', - source: '/issue/:id/:title', - target: '/hk01/issue/:id', - }, - { - title: 'tag', - docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', - source: '/tag/:id/:title', - target: '/hk01/tag/:id', - }, - ], - }, 'popiapp.cn': { _name: 'Popi 提问箱', www: [ diff --git a/lib/router.js b/lib/router.js index 0b90b4599..5370ebf7e 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2201,11 +2201,11 @@ router.get('/mcbbs/post/:tid/:authorid?', lazyloadRouteHandler('./routes/mcbbs/p router.get('/pocket/trending', lazyloadRouteHandler('./routes/pocket/trending')); // HK01 -router.get('/hk01/zone/:id', lazyloadRouteHandler('./routes/hk01/zone')); -router.get('/hk01/channel/:id', lazyloadRouteHandler('./routes/hk01/channel')); -router.get('/hk01/issue/:id', lazyloadRouteHandler('./routes/hk01/issue')); -router.get('/hk01/tag/:id', lazyloadRouteHandler('./routes/hk01/tag')); -router.get('/hk01/hot', lazyloadRouteHandler('./routes/hk01/hot')); +// router.get('/hk01/zone/:id', lazyloadRouteHandler('./routes/hk01/zone')); +// router.get('/hk01/channel/:id', lazyloadRouteHandler('./routes/hk01/channel')); +// router.get('/hk01/issue/:id', lazyloadRouteHandler('./routes/hk01/issue')); +// router.get('/hk01/tag/:id', lazyloadRouteHandler('./routes/hk01/tag')); +// router.get('/hk01/hot', lazyloadRouteHandler('./routes/hk01/hot')); // 码农周刊 router.get('/manong-weekly', lazyloadRouteHandler('./routes/manong-weekly/issues')); diff --git a/lib/routes/hk01/channel.js b/lib/routes/hk01/channel.js deleted file mode 100644 index af455d42b..000000000 --- a/lib/routes/hk01/channel.js +++ /dev/null @@ -1,22 +0,0 @@ -const got = require('@/utils/got'); - -module.exports = async (ctx) => { - const id = ctx.params.id; - - const response = await got(`https://web-data.api.hk01.com/v2/page/category/${id}`); - const data = response.data; - const list = data.sections[0].items; - - ctx.state.data = { - title: `香港01 - ${data.category.publishName}`, - description: data.meta.metaDesc, - link: data.category.publishUrl, - item: list.map((item) => ({ - title: item.data.title, - author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), - description: `

${item.data.description}…

`, - pubDate: new Date(item.data.lastModifyTime * 1000), - link: item.data.canonicalUrl, - })), - }; -}; diff --git a/lib/routes/hk01/issue.js b/lib/routes/hk01/issue.js deleted file mode 100644 index 8a4eb3027..000000000 --- a/lib/routes/hk01/issue.js +++ /dev/null @@ -1,22 +0,0 @@ -const got = require('@/utils/got'); - -module.exports = async (ctx) => { - const id = ctx.params.id; - - const response = await got(`https://web-data.api.hk01.com/v2/page/issue/${id}`); - const data = response.data; - const list = data.issue.blocks[0].articles; - - ctx.state.data = { - title: `香港01 - ${data.issue.title}`, - description: data.meta.metaDesc, - link: data.issue.publishUrl, - item: list.map((item) => ({ - title: item.data.title, - author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), - description: `

${item.data.description}…

`, - pubDate: new Date(item.data.lastModifyTime * 1000), - link: item.data.canonicalUrl, - })), - }; -}; diff --git a/lib/v2/hk01/channel.js b/lib/v2/hk01/channel.js new file mode 100644 index 000000000..c5a5019b3 --- /dev/null +++ b/lib/v2/hk01/channel.js @@ -0,0 +1,40 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got(`https://web-data.api.hk01.com/v2/page/category/${id}`); + const data = response.data; + const list = data.sections[0].items; + + await Promise.all( + list.map((item) => + ctx.cache.tryGet(`https://www.hk01.com/${item.data.mainCategory}/${item.data.articleId}`, async () => { + const detailResponse = await got(item.data.canonicalUrl); + const $ = cheerio.load(detailResponse.data); + + $('article > div.view-tracker').remove(); + $('article > div.flex.flex-col').remove(); + + item.data.description = $('article').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `香港01 - ${data.category.publishName}`, + description: data.meta.metaDesc, + link: data.category.publishUrl, + item: list.map((item) => ({ + title: item.data.title, + author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), + description: `

${item.data.description}

`, + pubDate: parseDate(item.data.lastModifyTime * 1000), + link: item.data.canonicalUrl, + })), + }; +}; diff --git a/lib/routes/hk01/hot.js b/lib/v2/hk01/hot.js similarity index 85% rename from lib/routes/hk01/hot.js rename to lib/v2/hk01/hot.js index b991a7ff0..2ac71304d 100644 --- a/lib/routes/hk01/hot.js +++ b/lib/v2/hk01/hot.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const response = await got('https://web-data.api.hk01.com/v2/page/hot/'); @@ -13,7 +14,7 @@ module.exports = async (ctx) => { title: item.data.title, author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), description: `

${item.data.description}

`, - pubDate: new Date(item.data.lastModifyTime * 1000), + pubDate: parseDate(item.data.lastModifyTime * 1000), link: item.data.canonicalUrl, })), }; diff --git a/lib/v2/hk01/issue.js b/lib/v2/hk01/issue.js new file mode 100644 index 000000000..6f1eed29c --- /dev/null +++ b/lib/v2/hk01/issue.js @@ -0,0 +1,41 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const response = await got(`https://web-data.api.hk01.com/v2/page/issue/${id}`); + const data = response.data; + const list = data.issue.blocks[0].articles; + + await Promise.all( + list.map((item) => + ctx.cache.tryGet(`https://www.hk01.com/${item.data.mainCategory}/${item.data.articleId}`, async () => { + const detailResponse = await got(item.data.canonicalUrl); + const $ = cheerio.load(detailResponse.data); + + $('div.lazyload-placeholder').remove(); + $('article > div.view-tracker').remove(); + $('article > div.flex.flex-col').remove(); + + item.data.description = $('article').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `香港01 - ${data.issue.title}`, + description: data.meta.metaDesc, + link: data.issue.publishUrl, + item: list.map((item) => ({ + title: item.data.title, + author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), + description: `

${item.data.description}

`, + pubDate: parseDate(item.data.lastModifyTime * 1000), + link: item.data.canonicalUrl, + })), + }; +}; diff --git a/lib/v2/hk01/maintainer.js b/lib/v2/hk01/maintainer.js new file mode 100644 index 000000000..f3ea0b6cf --- /dev/null +++ b/lib/v2/hk01/maintainer.js @@ -0,0 +1,7 @@ +module.exports = { + '/channel/:id': ['hoilc', 'Fatpandac'], + '/hot': ['hoilc', 'Fatpandac'], + '/issue/:id': ['hoilc', 'Fatpandac'], + '/tag/:id': ['hoilc', 'Fatpandac'], + '/zone/:id': ['hoilc', 'Fatpandac'], +}; diff --git a/lib/v2/hk01/radar.js b/lib/v2/hk01/radar.js new file mode 100644 index 000000000..db0b2e7c1 --- /dev/null +++ b/lib/v2/hk01/radar.js @@ -0,0 +1,37 @@ +module.exports = { + 'hk01.com': { + _name: '香港01', + www: [ + { + title: '最 Hit', + docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', + source: ['/hot', '/'], + target: '/hk01/hot', + }, + { + title: 'zone', + docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', + source: '/zone/:id/:title', + target: '/hk01/zone/:id', + }, + { + title: 'channel', + docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', + source: '/channel/:id/:title', + target: '/hk01/channel/:id', + }, + { + title: 'issue', + docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', + source: '/issue/:id/:title', + target: '/hk01/issue/:id', + }, + { + title: 'tag', + docs: 'https://docs.rsshub.app/traditional-media.html#xiang-gang-01', + source: '/tag/:id/:title', + target: '/hk01/tag/:id', + }, + ], + }, +}; diff --git a/lib/v2/hk01/router.js b/lib/v2/hk01/router.js new file mode 100644 index 000000000..bb2969da9 --- /dev/null +++ b/lib/v2/hk01/router.js @@ -0,0 +1,7 @@ +module.exports = function (router) { + router.get('/channel/:id', require('./channel')); + router.get('/hot', require('./hot')); + router.get('/issue/:id', require('./issue')); + router.get('/tag/:id', require('./tag')); + router.get('/zone/:id', require('./zone')); +}; diff --git a/lib/routes/hk01/tag.js b/lib/v2/hk01/tag.js similarity index 86% rename from lib/routes/hk01/tag.js rename to lib/v2/hk01/tag.js index 46186f9d8..0e50473ae 100644 --- a/lib/routes/hk01/tag.js +++ b/lib/v2/hk01/tag.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -15,7 +16,7 @@ module.exports = async (ctx) => { title: item.data.title, author: item.data.authors && item.data.authors.map((e) => e.publishName).join(', '), description: `

${item.data.description}

`, - pubDate: new Date(item.data.lastModifyTime * 1000), + pubDate: parseDate(item.data.lastModifyTime * 1000), link: item.data.canonicalUrl, })), }; diff --git a/lib/routes/hk01/zone.js b/lib/v2/hk01/zone.js similarity index 87% rename from lib/routes/hk01/zone.js rename to lib/v2/hk01/zone.js index 6299be010..ceeb25414 100644 --- a/lib/routes/hk01/zone.js +++ b/lib/v2/hk01/zone.js @@ -1,4 +1,5 @@ const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -19,12 +20,12 @@ module.exports = async (ctx) => { case 1: author = item.data.authors && item.data.authors.map((e) => e.publishName).join(', '); description = `

${item.data.description}

`; - pubDate = new Date(item.data.lastModifyTime * 1000); + pubDate = parseDate(item.data.lastModifyTime * 1000); break; case 2: author = item.data.zonePublishName; description = `${item.data.teaser.map((e) => '

' + e + '

').join('')}`; - pubDate = new Date(); + pubDate = parseDate(item.data.lastModifyTime * 1000); break; default: break;