From 5a2e252eb368a16315ee480e3e815877d0c35ee6 Mon Sep 17 00:00:00 2001 From: Shun Zi <1652270@tongji.edu.cn> Date: Sun, 18 Oct 2020 09:46:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20more=20readable=20=E5=BE=AE=E5=8D=9A?= =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E6=97=B6=E9=97=B4=E7=BA=BF,=20Twitter=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=97=B6=E9=97=B4=E7=BA=BF,=20=E8=B1=86?= =?UTF-8?q?=E7=93=A3=E7=94=A8=E6=88=B7=E5=B9=BF=E6=92=AD=20(#5886)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: codefactor-io --- lib/routes/douban/people/status.js | 165 ++++++++++++++++++++--------- lib/routes/twitter/user.js | 9 +- lib/routes/twitter/utils.js | 42 ++++++-- lib/routes/weibo/user.js | 11 +- lib/routes/weibo/utils.js | 41 +++++-- 5 files changed, 194 insertions(+), 74 deletions(-) diff --git a/lib/routes/douban/people/status.js b/lib/routes/douban/people/status.js index 4f6c1173f..a0f181a87 100644 --- a/lib/routes/douban/people/status.js +++ b/lib/routes/douban/people/status.js @@ -1,56 +1,125 @@ const got = require('@/utils/got'); -function getContentByActivity(status) { +function getContentByActivity(item, isRenderingRepost = false) { + const { status, comments } = item; let description = ''; let title = ''; - switch (status.activity) { - case '说': - title = `${status.author.name} ${status.activity}:${status.text}`; - description = status.text; - status.images.forEach((image) => { - description += `

`; - }); - break; - case '转发': - description = `${status.text}
`; - if (status.reshared_status.deleted) { - title = `${status.author.name} ${status.activity} 广播:原动态已被发布者删除`; - description += `原动态已被发布者删除`; - } else { - description += getContentByActivity(status.reshared_status).title; - title = `${status.author.name} ${status.activity} ${status.reshared_status.author.name} 的广播:${status.reshared_status.text}`; - status.reshared_status.images.forEach((image) => { - description += `

`; - }); - } - break; - default: - description = `${status.text ? `${status.text}

` : ''}`; - title = `${status.author.name}${status.activity}: ${description}`; - if (status.card) { - let image; - if (status.card.image && (status.card.image.large || status.card.image.normal)) { - image = (status.card.image.large || status.card.image.normal).url; - } - if (status.card.rating) { - description += `豆瓣评分:${status.card.rating}
${status.card.subtitle}${image ? `
` : ''}
《${status.card.title}》`; - title = `${status.author.name}${status.activity}:《${status.card.title}》`; - } else { - description += `${status.card.subtitle}${image ? `
` : ''}
${status.card.url ? `${status.card.title}` : ''}`; - title = `${status.author.name} ${status.activity}:「${status.card.title}」${status.text}`; - } - } - if (status.video_card) { - 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; - description += `${videoSrc ? `
` : ''}

${ - status.video_card.title ? `${status.video_card.title}` : '' - }`; - title = `${status.author.name}${status.activity}:《${status.video_card.title}》`; - } - break; + let usernameAndAvatar = ``; + if (!isRenderingRepost) { + usernameAndAvatar += ``; } + usernameAndAvatar += `${status.author.name}  `; + + title += `${status.author.name} ${status.activity}: `; + description += usernameAndAvatar + `${status.activity}`; + + description += `
${status.create_time}
`; + + let text = status.text; + let lastIndex = 0; + const replacedTextSegements = []; + for (const entity of status.entities) { + replacedTextSegements.push(text.slice(lastIndex, entity.start)); + replacedTextSegements.push(`${entity.title}`); + lastIndex = entity.end; + } + replacedTextSegements.push(text.slice(lastIndex)); + text = replacedTextSegements.join(''); + + // text // images // video_info // parent status + + description += text; + + if (status.card) { + if (status.card.rating) { + title += `《${status.card.title}》`; + } else { + title += `「${status.card.title}」`; + } + } + + title += status.text.replace('\n', ''); + + if (status.images.length) { + description += `
`; + status.images.forEach((image) => { + description += ''; + }); + } + + if (status.video_info) { + description += `
`; + const videoCover = status.video_info.cover_url; + const videoSrc = status.video_info.video_url; + description += `${videoSrc ? `` : ''}`; + } + + if (status.parent_status) { + description += ' 🔁 '; + title += ' 🔁 '; + + let usernameAndAvatar = ``; + usernameAndAvatar += `${status.parent_status.author.name}:  `; + description += usernameAndAvatar + status.parent_status.text; + title += usernameAndAvatar + status.parent_status.text; + } + + // card + if (status.card) { + let image; + if (status.card.image && (status.card.image.large || status.card.image.normal)) { + image = status.card.image.large || status.card.image.normal; + } + + description += `
`; + if (image) { + description += ``; + } + description += `${status.card.title}
${status.card.subtitle}`; + if (status.card.rating) { + description += `
评分:${status.card.rating}`; + } + description += `

`; + } + + // video_card + if (status.video_card) { + description += `
`; + 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; + description += `${videoSrc ? `` : ''}
${status.video_card.title ? `${status.video_card.title}` : ''}`; + description += `
`; + } + + // reshared_status + if (status.reshared_status) { + description += `
`; + + if (status.reshared_status.deleted) { + description += `原动态已被发布者删除`; + title += ` | 原动态已被发布者删除`; + } else { + description += getContentByActivity({ status: status.reshared_status, comments: [] }, true).description; + title += ' | ' + status.reshared_status.text; + } + + const reshared_url = status.reshared_status.uri.replace('douban://douban.com', 'https://www.douban.com/doubanapp/dispatch?uri='); + + description += `
原动态:${reshared_url}
`; + } + + // comments + if (!isRenderingRepost) { + if (comments.length > 0) { + description += '
'; + } + for (const comment of comments) { + description += `
${comment.text} - ${comment.author.name}`; + } + } + + description = description.trim().replace(/\n/g, '
'); return { title, description }; } @@ -78,7 +147,7 @@ module.exports = async (ctx) => { items .filter((item) => !item.deleted) .map((item) => { - const r = getContentByActivity(item.status); + const r = getContentByActivity(item); return { title: r.title, link: item.status.sharing_url, diff --git a/lib/routes/twitter/user.js b/lib/routes/twitter/user.js index 06c2f6138..a5caf0918 100644 --- a/lib/routes/twitter/user.js +++ b/lib/routes/twitter/user.js @@ -37,8 +37,11 @@ module.exports = async (ctx) => { link: `https://twitter.com/${id}/`, image: profileImageUrl, description: userInfo.description, - item: utils.ProcessFeed({ - data, - }), + item: utils.ProcessFeed( + { + data, + }, + true + ), }; }; diff --git a/lib/routes/twitter/utils.js b/lib/routes/twitter/utils.js index d6801a731..afb801d92 100644 --- a/lib/routes/twitter/utils.js +++ b/lib/routes/twitter/utils.js @@ -2,7 +2,7 @@ const URL = require('url'); const config = require('@/config').value; const Twit = require('twit'); -const ProcessFeed = ({ data = [] }, showAuthor = false) => { +const ProcessFeed = ({ data = [] }, showAuthor = false, showAuthorInTitle = false) => { const getQueryParams = (url) => URL.parse(url, true).query; const getOrigionImg = (url) => { // https://greasyfork.org/zh-CN/scripts/2312-resize-image-on-open-image-in-new-tab/code#n150 @@ -28,7 +28,12 @@ const ProcessFeed = ({ data = [] }, showAuthor = false) => { }; const replaceBreak = (text) => text.replace(/

|
/g, ' '); - const formatText = (text) => text.replace(/https:\/\/t\.co(.*)/g, '').replace(/\n/g, '
'); + const formatText = (text) => + text + .replace(/https:\/\/t\.co(.*)/g, '') + .trim() + .replace(/\n/g, '
'); + const formatTextToPlain = (text) => text.replace(/https:\/\/t\.co(.*)/g, '').trim(); const formatVideo = (media) => { let content = ''; const video = media.video_info.variants.reduce((video, item) => { @@ -40,7 +45,7 @@ const ProcessFeed = ({ data = [] }, showAuthor = false) => { if (video.url) { const gifAutoPlayAttr = media.type === 'animated_gif' ? `autoplay loop muted webkit-playsinline playsinline` : ''; - content = `
`; + content = ``; } return content; @@ -52,6 +57,7 @@ const ProcessFeed = ({ data = [] }, showAuthor = false) => { item.extended_entities.media.forEach((item) => { // https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object let content = ''; + let originImg; switch (item.type) { case 'animated_gif': case 'video': @@ -60,39 +66,51 @@ const ProcessFeed = ({ data = [] }, showAuthor = false) => { case 'photo': default: - content = `
`; + originImg = getOrigionImg(item.media_url_https); + content = ``; break; } img += content; }); + if (img) { + img = `
` + img; + } return img; }; const formatUrl = (item) => { let url = ''; item.entities.urls.forEach((u) => { - url += `${u.expanded_url}`; + url += `
${u.expanded_url}`; }); return url; }; return data.map((item) => { + const originalItem = item; item = item.retweeted_status || item; item.full_text = formatText(item.full_text); const img = formatMedia(item); let url = ''; let quote = ''; + let quoteOnTitle = ''; if (item.is_quote_status) { const quoteData = item.quoted_status; if (quoteData) { const author = quoteData.user; - quote += `

${author.name}: ${formatText(quoteData.full_text)}
`; + quote += `
${author.name}:  `; + quote += `${formatText(quoteData.full_text)}`; quote += formatMedia(quoteData); quote += formatUrl(quoteData); + quoteOnTitle += `| ${author.name}: ${formatTextToPlain(quoteData.full_text)}`; + + quote += `
Source: https://twitter.com/${author.screen_name}/status/${quoteData.id_str}`; + quote += '
' + new Date(quoteData.created_at).toLocaleString(); + quote += `
`; } else { url = formatUrl(item); } @@ -101,11 +119,13 @@ const ProcessFeed = ({ data = [] }, showAuthor = false) => { } return { - title: `${showAuthor ? item.user.name + ': ' : ''}${item.in_reply_to_screen_name ? 'Re ' : ''}${replaceBreak(item.full_text)}`, - author: item.user.name, - description: `${showAuthor ? `${item.user.name}:

` : ''}${item.in_reply_to_screen_name ? 'Re ' : ''}${ - item.full_text - }${url}${img}${quote}`, + title: + originalItem.user.name + + `: ${originalItem === item ? '' : '🔁 '}${showAuthorInTitle || originalItem !== item ? item.user.name + ': ' : ''}${item.in_reply_to_screen_name ? '↩️ ' : ''}${replaceBreak(item.full_text)}${quoteOnTitle}`, + author: originalItem.user.name, + description: `${ + showAuthor ? `${item.user.name}:  ` : '' + }${item.in_reply_to_screen_name ? '🔁 ' : ''}${item.full_text}${url}${img}${quote}
${new Date(item.created_at).toLocaleString()}`, pubDate: new Date(item.created_at).toUTCString(), link: `https://twitter.com/${item.user.screen_name}/status/${item.id_str}`, }; diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index ed3e2a19c..62fcba715 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -49,6 +49,10 @@ module.exports = async (ctx) => { const isDataOK = data !== undefined && data.text; if (isDataOK) { item.mblog.text = data.text; + item.mblog.created_at = data.created_at; + if (item.mblog.retweeted_status && data.retweeted_status) { + item.mblog.retweeted_status.created_at = data.retweeted_status.created_at; + } } // 转发的长微博处理 const retweet = item.mblog.retweeted_status; @@ -60,8 +64,10 @@ module.exports = async (ctx) => { } const link = `https://weibo.com/${uid}/${item.mblog.bid}`; - let description = weiboUtils.format(item.mblog); - const title = description.replace(//g, '[图片]').replace(/<.*?>/g, ''); + const formatWithRetweet = weiboUtils.formatWithRetweet(item.mblog); + let description = formatWithRetweet.description; + const retweetedOrOriginal = formatWithRetweet.retweetedOrOriginal; + const title = item.mblog.user.screen_name + ': ' + retweetedOrOriginal.replace(//g, '[图片]').replace(/<.*?>/g, ''); const pubDate = isDataOK ? new Date(data.created_at).toUTCString() : date(item.mblog.created_at, 8); // 视频的处理 @@ -74,6 +80,7 @@ module.exports = async (ctx) => { description: description, link: link, pubDate: pubDate, + author: item.mblog.user.screen_name, }; return Promise.resolve(it); }) diff --git a/lib/routes/weibo/utils.js b/lib/routes/weibo/utils.js index 02fac9c5a..cb3605248 100644 --- a/lib/routes/weibo/utils.js +++ b/lib/routes/weibo/utils.js @@ -1,9 +1,11 @@ const got = require('@/utils/got'); const weiboUtils = { - format: (status) => { + format: (status) => weiboUtils.formatWithRetweet(status).description, + formatWithRetweet: (status, showAuthorAvatar = true) => { + let retweetedOrOriginal = ''; // 长文章的处理 - let temp = (status.longText && status.longText.longTextContent.replace(/\n/g, '
')) || status.text || ''; + let temp = (status.longText && status.longText.longTextContent) || status.text || ''; // 表情图标转换为文字 temp = temp.replace(/<\/span>/g, '$1'); // 去掉外部链接的图标 @@ -19,24 +21,43 @@ const weiboUtils = { return decodeURIComponent(p1); }); + let originalText = temp; + temp = temp.replace(/\n/g, '
'); + + // 添加用户名和头像 + let usernameAndAvatar = ``; + if (showAuthorAvatar) { + usernameAndAvatar += ``; + } + usernameAndAvatar += `${status.user.screen_name}:  `; + temp = usernameAndAvatar + temp; + // 处理转发的微博 if (status.retweeted_status) { - // 当转发的微博被删除时user为null - if (status.retweeted_status.user) { - temp += `
- 转发 @${status.retweeted_status.user.screen_name}: `; + temp += `
`; + if (!status.retweeted_status.user) { + // 当转发的微博被删除时user为null + status.retweeted_status.user = '微博已删除'; } // 插入转发的微博 - temp += weiboUtils.format(status.retweeted_status); - temp += '
'; + retweetedOrOriginal += weiboUtils.formatWithRetweet(status.retweeted_status, false).description; + temp += + retweetedOrOriginal + + `
原博:https://weibo.com/${status.retweeted_status.user.id}/${status.retweeted_status.bid}
` + + new Date(status.retweeted_status.created_at).toLocaleString() + + '
'; } // 添加微博配图 if (status.pics) { + temp += '
'; status.pics.forEach(function (item) { - temp += '

'; + temp += ''; + originalText += ''; }); } - return temp; + + return { description: temp, retweetedOrOriginal: status.retweeted_status ? '🔁 ' + retweetedOrOriginal : originalText.replace('\n', '') }; }, getShowData: async (uid, bid) => { const link = `https://m.weibo.cn/statuses/show?id=${bid}`; @@ -58,7 +79,7 @@ const weiboUtils = { const posterUrl = pagePic ? pagePic.url : ''; const videoUrl = mediaInfo ? mediaInfo.stream_url_hd || mediaInfo.stream_url || mediaInfo.mp4_hd_url || mediaInfo.mp4_sd_url || mediaInfo.mp4_720p_mp4 : ''; if (videoUrl) { - const video = `
`; + const video = `
`; itemDesc += video; } }