fix: douban people status: more checks in case of unstable and erroneous api response (#6442)

This commit is contained in:
Shun Zi
2020-12-14 11:55:00 +00:00
committed by GitHub
parent e31ce8cf61
commit eb3a26ba6b
+122 -23
View File
@@ -2,6 +2,62 @@ const querystring = require('querystring');
const got = require('@/utils/got');
const { fallback, queryToBoolean, queryToInteger } = require('@/utils/readable-social');
function tryFixStatus(status) {
let result = { isFixSuccess: true, why: '' };
const now = new Date();
if (!status) {
result = {
isFixSuccess: false,
// 添加方括号,与 status.msg 的样式统一
why: '[ 无内容 ]',
};
status = {}; // dummy
} else if (status.deleted) {
result = {
isFixSuccess: false,
why: status.msg ? status.msg : '[ 内容已被删除 ]',
};
} else if (status.hidden) {
result = {
isFixSuccess: false,
why: status.msg ? status.msg : '[ 内容已被设为不可见 ]',
};
} else if (!status.activity || status.text === undefined || status.text === null || !status.uri) {
result = {
isFixSuccess: false,
why: status.msg ? status.msg : '[ 内容已不可访问 ]',
};
} else {
if (!status.author) {
status.author = {};
}
if (!status.author.url) {
status.author.url = 'https://www.douban.com/people/1/';
}
if (!status.author.name) {
status.author.name = '[作者不可见]';
}
if (!status.author.avatar) {
status.author.avatar = 'https://img1.doubanio.com/icon/user_normal.jpg';
}
if (!status.create_time) {
status.create_time = now.toLocaleString();
}
if (!status.entities) {
status.entities = [];
}
}
if (!result.isFixSuccess) {
status.sharing_url = 'https://www.douban.com?rsshub_failed=' + now.getTime().toString();
if (!status.create_time) {
status.create_time = now.toLocaleString();
}
}
return result;
}
function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
const routeParams = querystring.parse(ctx.params.routeParams);
@@ -46,17 +102,27 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
} = params;
const { status, comments } = item;
const { isFixSuccess, why } = tryFixStatus(status);
if (!isFixSuccess) {
return {
title: why,
description: why,
};
}
let description = '';
let title = '';
let activityInDesc;
let activityInTitle;
const { isFixSuccess: isResharedFixSuccess, why: resharedWhy } = tryFixStatus(status.reshared_status);
if (status.activity !== '转发') {
activityInDesc = status.activity;
activityInTitle = status.activity;
} else {
if (status.reshared_status.deleted) {
if (!isResharedFixSuccess) {
activityInDesc = `转发广播`;
activityInTitle = `转发广播`;
} else {
@@ -137,7 +203,7 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
title += status.text.replace('\n', '');
}
if (status.images.length) {
if (status.images && status.images.length) {
if (readable) {
description += `<br clear="both" /><div style="clear: both"></div>`;
} else {
@@ -148,11 +214,19 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
// 让所有配图在description的最前面再次出现一次,但宽高设为0
let picsPrefix = '';
status.images.forEach(function (image) {
if (!(image.large && image.large.url)) {
return;
}
picsPrefix += `<img width="0" height="0" hidden="true" src="${image.large.url}">`;
});
picsPrefixes.push(picsPrefix);
status.images.forEach((image) => {
if (!(image.large && image.large.url)) {
description += '[无法显示的图片]';
return;
}
if (addLinkForPics) {
description += '<a href="' + image.large.url + '" target="_blank" rel="noopener noreferrer">';
}
@@ -193,25 +267,34 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
title += showEmojiForRetweet ? ' 🔁 ' : ' Fw: ';
}
let usernameAndAvatar = '';
const { isFixSuccess: isParentFixSuccess, why: parentWhy } = tryFixStatus(status.parent_status);
if (readable) {
usernameAndAvatar += `<a href="${status.parent_status.author === undefined ? 'https://www.douban.com/people/1/' : status.parent_status.author.url}">`;
}
if (authorNameBold) {
usernameAndAvatar += `<strong>`;
}
usernameAndAvatar += status.parent_status.author === undefined ? '[原转发不可访问]' : status.parent_status.author.name;
if (authorNameBold) {
usernameAndAvatar += `</strong>`;
}
if (readable) {
usernameAndAvatar += `</a>`;
}
usernameAndAvatar += `:&ensp;`;
description += usernameAndAvatar + (status.parent_status.text === undefined ? '[原转发不可访问]' : status.parent_status.text);
if (showRetweetTextInTitle) {
title += status.parent_status.text === undefined ? '[原转发不可访问]' : status.parent_status.author.name + ': ' + status.parent_status.text;
if (!isParentFixSuccess) {
description += parentWhy;
if (showRetweetTextInTitle) {
title += parentWhy;
}
} else {
let usernameAndAvatar = '';
if (readable) {
usernameAndAvatar += `<a href="${status.parent_status.author.url}">`;
}
if (authorNameBold) {
usernameAndAvatar += `<strong>`;
}
usernameAndAvatar += status.parent_status.author.name;
if (authorNameBold) {
usernameAndAvatar += `</strong>`;
}
if (readable) {
usernameAndAvatar += `</a>`;
}
usernameAndAvatar += `:&ensp;`;
description += usernameAndAvatar + status.parent_status.text;
if (showRetweetTextInTitle) {
title += status.parent_status.author.name + ': ' + status.parent_status.text;
}
}
}
@@ -230,6 +313,17 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
if (image) {
description += `<img src="${image.url}" ${readable ? 'vspace="0" hspace="12" align="left" height="75" style="height: 75px;"' : ''} />`;
}
if (!status.card.title) {
status.card.title = '[空]';
}
if (!status.card.subtitle) {
status.card.subtitle = '[空]';
}
if (!status.card.url) {
status.card.url = 'https://www.douban.com';
}
description += `<a href="${status.card.url}" target="_blank" rel="noopener noreferrer"><strong>${status.card.title}</strong><br><small>${status.card.subtitle}</small>`;
if (status.card.rating) {
description += `<br><small>评分:${status.card.rating}</small>`;
@@ -249,6 +343,11 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
}
const videoCover = status.video_card.video_info && status.video_card.video_info.cover_url;
const videoSrc = status.video_card.video_info && status.video_card.video_info.video_url;
if (!status.video_card.url) {
status.video_card.url = 'https://www.douban.com';
}
description += `${videoSrc ? `<video src="${videoSrc}" ${videoCover ? `poster="${videoCover}"` : ''}></video>` : ''}<br>${status.video_card.title ? `<a href="${status.video_card.url}">${status.video_card.title}</a>` : ''}`;
if (readable) {
description += `</blockquote>`;
@@ -267,9 +366,9 @@ function getContentByActivity(ctx, item, params = {}, picsPrefixes = []) {
title += ' | ';
}
if (status.reshared_status.deleted) {
description += `[原动态不可访问]`;
title += `[原动态不可访问]`;
if (!isResharedFixSuccess) {
description += resharedWhy;
title += resharedWhy;
} else {
description += getContentByActivity(
ctx,