* style: add eslint-unicorn * style: fix unicorn/no-useless-spread * style: fix unicorn/no-useless-promise-resolve-reject * style: fix unicorn/no-for-loop * fix: codeql bad HTML filtering regexp * fix: codeql incomplete replace * fix: unicorn/no-abusive-eslint-disable * style: fix unicorn/no-new-array * style: fix unicorn/no-typeof-undefined * style: fix unicorn/no-zero-fractions * style: fix unicorn/no-empty-file * style: fix unicorn/prefer-date-now * revert: auto fix unicorn/prefer-switch on lib/v2/kuaidi100/utils.js * style: fix unicorn/prefer-array-find * style: fix unicorn/prefer-array-flat * style: fix unicorn/prefer-array-flat-map * style: fix unicorn/prefer-at * style: fix unicorn/prefer-string-starts-ends-with * style: fix unicorn/prefer-includes * fix: codeql URL substring sanitization * style: fix unicorn/prefer-optional-catch-binding * style: fix unicorn/catch-error-name * style: fix unicorn/escape-case * style: fix unicorn/prefer-native-coercion-functions * style: fix unicorn/prefer-regexp-test * style: fix unicorn/require-array-join-separator * style: fix unicorn/prefer-math-trunc * style: fix unicorn/prefer-negative-index * style: fix unicorn/prefer-dom-node-dataset * style: fix unicorn/prefer-dom-node-text-content * style: fix unicorn/prefer-query-selector * style: fix unicorn/no-array-for-each * style: fix unicorn/no-negated-condition * style: fix unicorn/prefer-add-event-listener * style: fix unicorn/import-style * style: fix prefer-regex-literals * style: disable unicorn/no-useless-switch-case * style: disable unicorn/text-encoding-identifier-case * style: fix unicorn/prefer-set-has * style: fix unicorn/prefer-spread * revert: auto fix on lib/routes/universities/ynnu/edu/base64.js * style: fix unicorn/no-useless-undefined * style: fix unicorn/no-array-push-push * style: fix unicorn/no-useless-undefined again * style: fix unicorn/no-lonely-if * style: fix unicorn/prefer-reflect-apply * style: fix unicorn/switch-case-braces * style: fix unicorn/prefer-switch * style: fix unicorn/prefer-array-some * fix: deepscan UNUSED_VAR_ASSIGN * style: fix unicorn/prefer-ternary * fix: follow-up of unicorn/prefer-ternary * revert: auto fix of unicorn/prefer-string-slice for substring() * style: disable unicorn/prefer-string-slice fix: auto fix slice over deprecated substr * style: fix unicorn/throw-new-error * style: fix unicorn/filename-case * test: fix dateParser renaming * style: fix unicorn/better-regex * style: fix unicorn/prefer-string-replace-all * fix(deps): add sanitize-html * style: fix no-prototype-builtins * style: fix unicorn/consistent-destructuring * style: fix unicorn/consistent-function-scoping * style: fix unicorn/prefer-regexp-test * style: fix unicorn/prefer-logical-operator-over-ternary * style: fix unicorn/no-array-callback-reference * style: add prefer-object-has-own * style: warn unicorn/no-empty-file * style: fix unicorn/prefer-number-properties * style: fix no-useless-undefined again * style: fix unicorn/numeric-separators-style * style: disable unicorn/no-array-callback-reference false postive with cheerio
198 lines
7.7 KiB
JavaScript
198 lines
7.7 KiB
JavaScript
const { parseDate } = require('@/utils/parse-date');
|
||
const got = require('@/utils/got');
|
||
const cheerio = require('cheerio');
|
||
const config = require('@/config').value;
|
||
|
||
const videoAPI = 'https://api.ruguoapp.com/1.0/mediaMeta/play?type=ORIGINAL_POST';
|
||
|
||
module.exports = {
|
||
topicDataHanding: (data, ctx) =>
|
||
data.posts.map((item) => {
|
||
let audioName, videoName, linkName;
|
||
|
||
// 获取纯文字内容 和 即刻原文链接
|
||
let content, link;
|
||
switch (item.type) {
|
||
case 'ORIGINAL_POST':
|
||
content = item.content;
|
||
link = `https://m.okjike.com/originalPosts/${item.id}`;
|
||
break;
|
||
|
||
/* case 'QUESTION':
|
||
content = item.title;
|
||
link = `https://m.okjike.com/questions/${item.id}`;
|
||
break;
|
||
case 'OFFICIAL_MESSAGE':
|
||
content = item.content;
|
||
link = `https://web.okjike.com/message-detail/${item.id}/officialMessage`;
|
||
break;*/
|
||
default:
|
||
content = '未知类型,请前往GitHub提交issue';
|
||
link = 'https://github.com/DIYgod/RSSHub/issues';
|
||
}
|
||
|
||
// rss内容
|
||
let description = '';
|
||
// 作者昵称
|
||
let author = '';
|
||
|
||
// 添加内容作者信息
|
||
if (item.user) {
|
||
author = item.user.screenName;
|
||
if (ctx.params.showUid) {
|
||
description += `<span>用户昵称:${author} <br> Username:${item.user.username}</span><br>`;
|
||
}
|
||
}
|
||
|
||
if (item.linkInfo) {
|
||
const linkUrl = item.linkInfo.originalLinkUrl || item.linkInfo.linkUrl;
|
||
|
||
// 对于即刻抓取的微信公众号文章 特殊处理
|
||
// 此时 Rss原文链接 变为 微信公众号链接
|
||
if (new URL(linkUrl).host === 'mp.weixin.qq.com') {
|
||
link = linkUrl;
|
||
}
|
||
|
||
// 1. 音频
|
||
const audioObject = item.linkInfo.audio || item.audio;
|
||
if (audioObject) {
|
||
const audioImage = audioObject.image.picUrl || audioObject.image.thumbnailUrl;
|
||
const audioLink = linkUrl;
|
||
const audioTitle = audioObject.title;
|
||
const audioAuthor = audioObject.author;
|
||
audioName = `${audioTitle} - ${audioAuthor}`;
|
||
description += `
|
||
<img src="${audioImage}">
|
||
<a href="${audioLink}">${audioName}</a>
|
||
`;
|
||
}
|
||
|
||
// 2. 视频
|
||
const videoObject = item.linkInfo.video || item.video;
|
||
if (videoObject) {
|
||
const videoImage = videoObject.image.picUrl || videoObject.image.thumbnailUrl;
|
||
const videoLink = linkUrl;
|
||
const videoDuration = Math.floor(videoObject.duration / 60000);
|
||
videoName = item.linkInfo.title;
|
||
description += `
|
||
<img src="${videoImage}">
|
||
<a href="${videoLink}">${videoName || '观看视频'} - 约${videoDuration}分钟</a>
|
||
`;
|
||
}
|
||
|
||
// 3. 链接
|
||
if (!audioObject && !videoObject && linkUrl) {
|
||
// 部分链接有标题
|
||
linkName = item.linkInfo.title;
|
||
const linkTitle = linkName || '访问原文';
|
||
// 部分链接有缩略图
|
||
const linkImage = item.linkInfo.pictureUrl;
|
||
const imageTag = `<img src="${linkImage}">`;
|
||
description += `
|
||
${linkImage ? imageTag : ''}
|
||
<a href="${linkUrl}">${linkTitle}</a>
|
||
`;
|
||
}
|
||
}
|
||
|
||
// 4. 文字内容
|
||
description += description ? `<br>${content}` : content;
|
||
|
||
// 5. 图片
|
||
if (item.pictures) {
|
||
for (const pic of item.pictures) {
|
||
if (pic.format === 'gif') {
|
||
description += `<img src="${pic.picUrl.split('?imageMogr2/')[0]}">`;
|
||
} else {
|
||
// jpeg, bmp, png, gif, webp
|
||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
|
||
// let type = 'jpeg';
|
||
// switch (pic.format) {
|
||
// case 'bmp':
|
||
// type = 'bmp';
|
||
// break;
|
||
// case 'png':
|
||
// type = 'png';
|
||
// break;
|
||
// default:
|
||
// break;
|
||
// }
|
||
const imgUrl = /\.[\da-z]+?\?imageMogr2/.test(pic.picUrl) ? pic.picUrl.split('?imageMogr2/')[0] : pic.picUrl.replace(/thumbnail\/.+/, '');
|
||
description += `<br><img src="${imgUrl}">`;
|
||
// description += `<br><picture><source srcset="${
|
||
// pic.picUrl.split('/thumbnail/')[0]
|
||
// }/strip/format/webp" type="image/webp"><source srcset="${imgUrl}" type="image/${type}"><img src="${imgUrl}"></picture>`;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 6. 视频
|
||
if (item.video) {
|
||
description += `<br><video src="${item.video.url}" controls></video>`;
|
||
}
|
||
|
||
// rss标题
|
||
// 优先将音频和视频名作为标题
|
||
// 其次将正文内容作为标题
|
||
// 若都没有 则是推送型消息,将连接标题作为主题
|
||
// “无题” fallback
|
||
const title = audioName || videoName || content || linkName || '无题';
|
||
|
||
return {
|
||
title,
|
||
description: description.trim().replaceAll('\n', '<br>'),
|
||
pubDate: parseDate(item.createdAt),
|
||
author,
|
||
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();
|
||
const data = JSON.parse(raw).props.pageProps;
|
||
data.posts = await Promise.all(
|
||
data.posts.map(async (item) => {
|
||
if (!item.video) {
|
||
return item;
|
||
}
|
||
|
||
const videoUrl = `${videoAPI}&id=${item.id}`;
|
||
const videoRes = await got(videoUrl);
|
||
item.video = videoRes.data;
|
||
|
||
return item;
|
||
})
|
||
);
|
||
|
||
return data;
|
||
},
|
||
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;
|
||
},
|
||
};
|