From 9d1b29fcbb7187331c237540310421fa73ff5a37 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 13 Nov 2023 16:49:59 +0000 Subject: [PATCH] fix(route): dongqiudi (#13781) --- lib/v2/dongqiudi/special.js | 34 +++++++++++++--------------------- lib/v2/dongqiudi/utils.js | 26 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/v2/dongqiudi/special.js b/lib/v2/dongqiudi/special.js index 4e6506a6dfb40e..e98e2d734bb4de 100644 --- a/lib/v2/dongqiudi/special.js +++ b/lib/v2/dongqiudi/special.js @@ -1,32 +1,24 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); const utils = require('./utils'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const id = ctx.params.id; - const response = await got(`https://www.dongqiudi.com/special/${id}`); + const { data: response } = await got(`https://www.dongqiudi.com/api/old/columns/${id}`); - const $ = cheerio.load(response.data); - - const host = 'https://www.dongqiudi.com'; - - const list = $('.detail.special ul li h3') - .slice(0, ctx.query.limit ? Number(ctx.query.limit) : 5) - .toArray() - .map((item) => { - item = $(item); - return { - title: item.find('a').text(), - link: host + item.find('a').attr('href'), - }; - }); + const list = response.data.map((item) => ({ + title: item.title, + link: `https://www.dongqiudi.com/articles/${item.aid}.html`, + mobileLink: `https://m.dongqiudi.com/article/${item.aid}.html`, + pubDate: parseDate(item.show_time, 'X'), + })); const out = await Promise.all( list.map((item) => ctx.cache.tryGet(item.link, async () => { - const { data: response } = await got(item.link); + const { data: response } = await got(item.mobileLink); - utils.ProcessFeedType2(item, response); + utils.ProcessFeedType3(item, response); return item; }) @@ -34,9 +26,9 @@ module.exports = async (ctx) => { ); ctx.state.data = { - title: `懂球帝专题-${$('.detail.special h1').text()}`, - description: $('.detail.special h4').text(), + title: `懂球帝专题-${response.title}`, + description: response.description, link: `https://www.dongqiudi.com/special/${id}`, - item: out.filter((e) => e !== undefined), + item: out.filter((e) => e), }; }; diff --git a/lib/v2/dongqiudi/utils.js b/lib/v2/dongqiudi/utils.js index e18631b02e3c97..ff27f04cee3183 100644 --- a/lib/v2/dongqiudi/utils.js +++ b/lib/v2/dongqiudi/utils.js @@ -129,7 +129,7 @@ const ProcessFeedType2 = (item, response) => { } if (Object.keys(data).length > 0) { - const body = ProcessVideo(cheerio.load(data.body)); + const body = ProcessVideo(cheerio.load(data.body, null, false)); ProcessHref(body('a')); ProcessImg(body('img')); item.description = body.html(); @@ -138,10 +138,34 @@ const ProcessFeedType2 = (item, response) => { } }; +const ProcessFeedType3 = (item, response) => { + const $ = cheerio.load(response); + const initialState = JSON.parse( + $('script:contains("window.__INITIAL_STATE__")') + .text() + .match(/window\.__INITIAL_STATE__\s*=\s*(.*?);\(/)[1] + ); + + // filter out undefined item + if (!initialState) { + return; + } + + if (Object.keys(initialState.articleContent).length) { + const data = Object.values(initialState.articleContent)[0]; + const body = ProcessVideo(cheerio.load(data.body, null, false)); + ProcessHref(body('a')); + ProcessImg(body('img')); + item.description = body.html(); + item.author = data.writer; + } +}; + module.exports = { ProcessVideo, ProcessFeed, ProcessFeedType2, + ProcessFeedType3, ProcessHref, ProcessImg, };