fix(route): ifeng (#12697)

* fix(route): ifeng

* fix: image
This commit is contained in:
Tony
2023-06-22 23:31:31 +08:00
committed by GitHub
parent 977e3beccf
commit 48003a068c
4 changed files with 68 additions and 57 deletions
+40 -42
View File
@@ -1,71 +1,69 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { JSDOM } = require('jsdom');
const utils = require('./utils');
const { extractDoc, renderVideo } = require('./utils');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const id = ctx.params.id;
const type = ctx.params.type;
const userResponse = await got({
method: 'get',
url: `https://shankapi.ifeng.com/winter/ishare/getAccountInfo/${id}`,
const { id, type } = ctx.params;
const { data: userResponse } = await got(`https://ishare.ifeng.com/mediaShare/home/${id}/media`, {
headers: {
Referer: `https://feng.ifeng.com/author/${id}`,
},
});
const contentResponse = await got({
method: 'get',
url: `https://shankapi.ifeng.com/season/ishare/getShareListData/${id}/${type}/1/ifengnewsh5/getListData`,
const { data: contentResponse } = await got(`https://shankapi.ifeng.com/season/ishare/getShareListData/${id}/${type}/1/ifengnewsh5/getListData`, {
headers: {
Referer: `https://feng.ifeng.com/author/${id}`,
},
});
const userData = userResponse.data.data.data;
const contentData = JSON.parse(contentResponse.data.match(/"data":(\[(.*?)\])/)[1]);
const $ = cheerio.load(userResponse);
const { sockpuppetInfo: allData } = JSON.parse(
$('script')
.text()
.match(/var allData = (.*?);/)[1]
);
const { data: contentData } = JSON.parse(contentResponse.match(/getListData\((.*)\)/)[1]);
const { weMediaName: mediaName } = allData;
const list = contentData.map((item) => ({
title: item.title,
pubDate: timezone(parseDate(item.newsTime), +8),
author: mediaName,
link: `https:${item.url}`,
}));
const mediaName = userData.weMediaName;
const items = await Promise.all(
(contentData || []).map(async (item) => {
const link = `https:${item.url}`;
const simple = {
title: item.title,
description: `<img src="${item.thumbnail}">${item.title}`,
pubDate: timezone(parseDate(item.newsTime), +8),
author: mediaName,
link,
};
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = cheerio.load(response);
const details = await ctx.cache.tryGet(`ifeng:feng:${link}`, async () => {
const response = await got(link);
const $ = cheerio.load(response.data);
const allData = JSON.parse(
$('script')
.text()
.match(/var allData = ({.*?});/)[1]
);
if (type === 'doc') {
const dom = new JSDOM(`<body><script>${$('[async] + script').html()}</script></body>`, {
runScripts: 'dangerously',
});
const data = dom.window.allData;
return {
description: utils.extractDoc(data),
};
item.description = extractDoc(allData.docData.contentData.contentList);
}
if (type === 'video') {
return {
description: `<img src="${item.thumbnail}"><br><video src="${$('meta[name=og\\:img_video]').attr('content')}">`,
};
item.description = renderVideo(allData.videoInfo);
}
return {
description: $('meta[name=description]').attr('content'),
};
});
return { ...simple, ...details };
})
item.category = allData.keywords.split(',');
item.author = allData.docData?.editorName ?? item.author;
return item;
})
)
);
ctx.state.data = {
title: `大风号-${mediaName}-${type === 'doc' ? '文章' : '视频'}`,
link: `https://feng.ifeng.com/author/${id}`,
description: `${allData.honorDesc} ${allData.description}`,
image: `https:${allData.logo}`,
link: `https://ishare.ifeng.com/mediaShare/home/${id}/media`,
item: items,
};
};
+11 -1
View File
@@ -1,10 +1,12 @@
module.exports = {
'ifeng.com': {
_name: '凤凰网',
'.': [
feng: [
{
title: '大风号',
docs: 'https://docs.rsshub.app/new-media.html#feng-huang-wang',
source: ['/author/:id'],
target: '/ifeng/feng/:id/doc',
},
],
news: [
@@ -14,5 +16,13 @@ module.exports = {
target: (params, url) => `/ifeng/news${new URL(url).toString().match(/ifeng\.com(.*?)$/)[1]}`,
},
],
ishare: [
{
title: '大风号',
docs: 'https://docs.rsshub.app/new-media.html#feng-huang-wang',
source: ['/mediaShare/home/:id/media'],
target: '/ifeng/feng/:id/doc',
},
],
},
};
+5
View File
@@ -0,0 +1,5 @@
{{ if videoInfo.mobileUrl }}
<video controls poster="{{ videoInfo.bigPosterUrl }}" preload="none">
<source src="{{ videoInfo.mobileUrl }}" type="video/mp4">
</video>
{{ /if }}
+12 -14
View File
@@ -1,28 +1,26 @@
const extractDoc = (data) => {
if (data.docData.contentData.contentList) {
return extractContent(data.docData.contentData.contentList);
}
if (data.docData.contentData.image) {
return data.docData.contentData.image.map((item) => `<p><img src="${item.url}"><br>${item.description.replace(/\n/g, '<br>')}</p>`).join('');
}
};
const { art } = require('@/utils/render');
const { join } = require('path');
const extractContent = (data) =>
(data || [])
const extractDoc = (data) =>
data
.map((item) => {
const type = item.type;
if (type === 'video') {
return `<video src="${item.data.mobileUrl}" controls style="width:100%;height:100%"></video>`;
return renderVideo(item.data);
}
if (type === 'text') {
let temp = item.data;
temp = temp.replace(/data-lazyload=(.+?) src=(.+?) style=".+?"/g, 'src=$1');
return temp;
return item.data.replace(/data-lazyload=(.+?) src=(.+?) style=".+?"/g, 'src=$1');
}
return '';
})
.join('<br>');
const renderVideo = (videoInfo) =>
art(join(__dirname, 'templates/video.art'), {
videoInfo,
});
module.exports = {
extractDoc,
renderVideo,
};