mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 11:07:54 +08:00
refactor(route/jike): extract repeated logic; fix await (#9698)
* refactor(route/jike): rename `common` -> `utils` Signed-off-by: Rongrong <i@rong.moe> * refactor(route/jike): extract repeated logic Signed-off-by: Rongrong <i@rong.moe> * fix(route/jike): lack await Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
@@ -1,53 +1,42 @@
|
|||||||
const got = require('@/utils/got');
|
const got = require('@/utils/got');
|
||||||
const common = require('./common');
|
const { topicDataHanding } = require('./utils');
|
||||||
const cheerio = require('cheerio');
|
const cheerio = require('cheerio');
|
||||||
const dayjs = require('dayjs');
|
const dayjs = require('dayjs');
|
||||||
|
const { constructTopicEntry } = require('@/v2/jike/utils');
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const id = ctx.params.id;
|
const id = ctx.params.id;
|
||||||
|
const topicUrl = `https://m.okjike.com/topics/${id}`;
|
||||||
|
|
||||||
const response = await got(`https://m.okjike.com/topics/${id}`);
|
const data = await constructTopicEntry(ctx, topicUrl);
|
||||||
|
|
||||||
const html = response.data;
|
if (data) {
|
||||||
const $ = cheerio.load(html);
|
ctx.state.data.item = topicDataHanding(data);
|
||||||
const raw = $('[type = "application/json"]').html();
|
if (id === '553870e8e4b0cafb0a1bef68' || id === '55963702e4b0d84d2c30ce6f') {
|
||||||
const data = JSON.parse(raw).props.pageProps;
|
ctx.state.data.item = await Promise.all(
|
||||||
|
ctx.state.data.item.map(async (one) => {
|
||||||
if (common.emptyResponseCheck(ctx, data)) {
|
const item = { ...one };
|
||||||
return;
|
const regResult = /https:\/\/www\.okjike\.com\/medium\/[a-zA-Z0-9]*/.exec(item.description);
|
||||||
}
|
if (regResult) {
|
||||||
|
const newsUrl = regResult[0];
|
||||||
const topic = data.topic;
|
item.description = await ctx.cache.tryGet(newsUrl, async () => {
|
||||||
ctx.state.data = {
|
const { data } = await got(newsUrl);
|
||||||
title: `${topic.content} - 即刻圈子`,
|
const $ = cheerio.load(data);
|
||||||
link: `https://m.okjike.com/topics/${id}`,
|
const upper = $('ul.main > li.item');
|
||||||
description: topic.briefIntro,
|
const links = upper.find('a').map((_, ele) => $(ele).attr('href'));
|
||||||
image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl,
|
const texts = upper.find('span.text').map((_, ele) => $(ele).text());
|
||||||
item: common.topicDataHanding(data),
|
let description = '';
|
||||||
};
|
for (let i = 0; i < links.length; i++) {
|
||||||
if (id === '553870e8e4b0cafb0a1bef68' || id === '55963702e4b0d84d2c30ce6f') {
|
description += `${i + 1}、<a href="${links[i]}">${texts[i]}</a><br>`;
|
||||||
const promises = ctx.state.data.item.map((one) => {
|
}
|
||||||
const item = { ...one };
|
description = description.replace(/<br>$/, '');
|
||||||
const regResult = /https:\/\/www\.okjike\.com\/medium\/[a-zA-Z0-9]*/.exec(item.description);
|
return description;
|
||||||
if (regResult) {
|
});
|
||||||
const newsUrl = regResult[0];
|
|
||||||
item.description = ctx.cache.tryGet(newsUrl, async () => {
|
|
||||||
const { data } = await got(newsUrl);
|
|
||||||
const $ = cheerio.load(data);
|
|
||||||
const upper = $('ul.main > li.item');
|
|
||||||
const links = upper.find('a').map((_, ele) => $(ele).attr('href'));
|
|
||||||
const texts = upper.find('span.text').map((_, ele) => $(ele).text());
|
|
||||||
let description = '';
|
|
||||||
for (let i = 0; i < links.length; i++) {
|
|
||||||
description += `${i + 1}、<a href="${links[i]}">${texts[i]}</a><br>`;
|
|
||||||
}
|
}
|
||||||
return description;
|
item.title = `${data.topic.content} ${dayjs(one.pubDate).format('MM月DD日')}`;
|
||||||
});
|
return item;
|
||||||
}
|
})
|
||||||
item.title = `${topic.content} ${dayjs(one.pubDate).format('MM月DD日')}`;
|
);
|
||||||
return item;
|
}
|
||||||
});
|
|
||||||
|
|
||||||
ctx.state.data.item = await Promise.all(promises);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,36 +1,21 @@
|
|||||||
const got = require('@/utils/got');
|
|
||||||
const dayjs = require('dayjs');
|
const dayjs = require('dayjs');
|
||||||
const common = require('./common');
|
const { constructTopicEntry } = require('./utils');
|
||||||
const cheerio = require('cheerio');
|
|
||||||
|
|
||||||
module.exports = async (ctx) => {
|
module.exports = async (ctx) => {
|
||||||
const id = ctx.params.id;
|
const id = ctx.params.id;
|
||||||
|
const topicUrl = `https://m.okjike.com/topics/${id}`;
|
||||||
|
|
||||||
const response = await got(`https://m.okjike.com/topics/${id}`);
|
const data = await constructTopicEntry(ctx, topicUrl);
|
||||||
|
|
||||||
const html = response.data;
|
if (data) {
|
||||||
const $ = cheerio.load(html);
|
ctx.state.data.item = data.posts.map((item) => {
|
||||||
const raw = $('[type = "application/json"]').html();
|
|
||||||
const data = JSON.parse(raw).props.pageProps;
|
|
||||||
|
|
||||||
if (common.emptyResponseCheck(ctx, data)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const title = data.topic.content;
|
|
||||||
|
|
||||||
ctx.state.data = {
|
|
||||||
title: `${title} - 即刻`,
|
|
||||||
link: `https://m.okjike.com/topics/${id}`,
|
|
||||||
description: `${title} - 即刻`,
|
|
||||||
item: data.posts.map((item) => {
|
|
||||||
const date = dayjs(item.createdAt);
|
const date = dayjs(item.createdAt);
|
||||||
return {
|
return {
|
||||||
title: `${title} ${date.format('MM月DD日')}`,
|
title: `${data.topic.content} ${date.format('MM月DD日')}`,
|
||||||
description: item.content.replace(/\n/g, '<br>'),
|
description: item.content.replace(/\n/g, '<br>'),
|
||||||
pubDate: date.toDate(),
|
pubDate: date.toDate(),
|
||||||
link: `https://m.okjike.com/originalPosts/${item.id}`,
|
link: `https://m.okjike.com/originalPosts/${item.id}`,
|
||||||
};
|
};
|
||||||
}),
|
});
|
||||||
};
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
const { parseDate } = require('@/utils/parse-date');
|
const { parseDate } = require('@/utils/parse-date');
|
||||||
|
const got = require('@/utils/got');
|
||||||
|
const cheerio = require('cheerio');
|
||||||
|
const config = require('@/config').value;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
emptyResponseCheck: (ctx, data) => {
|
|
||||||
if (data.length === 0) {
|
|
||||||
ctx.state.data = {
|
|
||||||
title: '主题 ID 不存在,或该主题暂无内容',
|
|
||||||
};
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
topicDataHanding: (data) =>
|
topicDataHanding: (data) =>
|
||||||
data.posts.map((item) => {
|
data.posts.map((item) => {
|
||||||
let audioName, videoName, linkName;
|
let audioName, videoName, linkName;
|
||||||
@@ -133,4 +128,37 @@ module.exports = {
|
|||||||
link,
|
link,
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
constructTopicEntry: async (ctx, url) => {
|
||||||
|
const data = await ctx.cache.tryGet(
|
||||||
|
url,
|
||||||
|
async () => {
|
||||||
|
const resp = await got(url);
|
||||||
|
const html = resp.data;
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
const raw = $('[type = "application/json"]').html();
|
||||||
|
return JSON.parse(raw).props.pageProps;
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
config.cache.routeExpire
|
||||||
|
);
|
||||||
|
|
||||||
|
if (data.length === 0) {
|
||||||
|
ctx.state.data = {
|
||||||
|
title: '主题 ID 不存在,或该主题暂无内容',
|
||||||
|
};
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const topic = data.topic;
|
||||||
|
|
||||||
|
ctx.state.data = {
|
||||||
|
title: `${topic.content} - 即刻圈子`,
|
||||||
|
link: url,
|
||||||
|
description: topic.briefIntro,
|
||||||
|
image: topic.squarePicture.picUrl || topic.squarePicture.middlePicUrl || topic.squarePicture.thumbnailUrl,
|
||||||
|
// item: topicDataHanding(data),
|
||||||
|
};
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user