From 008182e408b2ff89652d33b9b42c7b7381feecaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=89=E5=87=89?= Date: Tue, 11 Dec 2018 11:33:00 +0800 Subject: [PATCH] Enhancement: twitter gif (#1251) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Enhancement: twitter gif * media type 为 animated_gif 或者 video 时,取最大 bitrate 项 --- routes/twitter/user.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/routes/twitter/user.js b/routes/twitter/user.js index 60e1113b5..3a9e7f12c 100644 --- a/routes/twitter/user.js +++ b/routes/twitter/user.js @@ -7,7 +7,6 @@ const T = new Twit(config.twitter); module.exports = async (ctx) => { const id = ctx.params.id; - const formatText = (text) => text.replace(/https:\/\/t\.co(.*)/g, ''); 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 @@ -31,11 +30,43 @@ module.exports = async (ctx) => { return url; } }; + + const formatText = (text) => text.replace(/https:\/\/t\.co(.*)/g, ''); + const formatVideo = (media) => { + let content = ''; + const video = media.video_info.variants.reduce((video, item) => { + if ((item.bitrate || 0) > (video.bitrate || 0)) { + video = item; + } + return video; + }, {}); + + if (video.url) { + content = `
`; + } + + return content; + }; + const formatMedia = (item) => { let img = ''; item.extended_entities && item.extended_entities.media.forEach((item) => { - img += `
${item.type === 'video' ? 'Video: ' : ''}`; + // https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object + let content = ''; + switch (item.type) { + case 'animated_gif': + case 'video': + content = formatVideo(item); + break; + + case 'photo': + default: + content = `
`; + break; + } + + img += content; }); return img;