diff --git a/lib/routes/douban/people/status.js b/lib/routes/douban/people/status.js index 44c404a330..d413ab77d2 100644 --- a/lib/routes/douban/people/status.js +++ b/lib/routes/douban/people/status.js @@ -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 += `
`; } 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 += ``; }); picsPrefixes.push(picsPrefix); status.images.forEach((image) => { + if (!(image.large && image.large.url)) { + description += '[无法显示的图片]'; + return; + } + if (addLinkForPics) { description += ''; } @@ -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 += ``; - } - if (authorNameBold) { - usernameAndAvatar += ``; - } - usernameAndAvatar += status.parent_status.author === undefined ? '[原转发不可访问]' : status.parent_status.author.name; - if (authorNameBold) { - usernameAndAvatar += ``; - } - if (readable) { - usernameAndAvatar += ``; - } - usernameAndAvatar += `: `; - 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 += ``; + } + if (authorNameBold) { + usernameAndAvatar += ``; + } + usernameAndAvatar += status.parent_status.author.name; + if (authorNameBold) { + usernameAndAvatar += ``; + } + if (readable) { + usernameAndAvatar += ``; + } + usernameAndAvatar += `: `; + 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 += ``; } + + 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 += `${status.card.title}
${status.card.subtitle}`; if (status.card.rating) { description += `
评分:${status.card.rating}`; @@ -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 ? `` : ''}
${status.video_card.title ? `
${status.video_card.title}` : ''}`; if (readable) { description += ``; @@ -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,