Skip to content

Commit

Permalink
fix(route): dongqiudi (DIYgod#13781)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored Nov 13, 2023
1 parent 90b4940 commit 9d1b29f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
34 changes: 13 additions & 21 deletions lib/v2/dongqiudi/special.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
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;
})
)
);

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),
};
};
26 changes: 25 additions & 1 deletion lib/v2/dongqiudi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
};

0 comments on commit 9d1b29f

Please sign in to comment.