From c8dfc01275fc35d37492a099df5df9f168b1d054 Mon Sep 17 00:00:00 2001 From: zytomorrow Date: Mon, 23 Dec 2019 17:11:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E5=BE=AE=E5=8D=9A?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=BA=BF=E6=98=BE=E7=A4=BA=E5=86=85=E5=AE=B9?= =?UTF-8?q?=20(#3587)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/social-media.md | 2 +- lib/routes/weibo/timeline.js | 132 ++++++++++++++++++++++++++++++----- 2 files changed, 116 insertions(+), 18 deletions(-) diff --git a/docs/social-media.md b/docs/social-media.md index 7e5c300a7..cdd3f2256 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -706,7 +706,7 @@ rule ### 个人时间线 - + ::: warning 注意 diff --git a/lib/routes/weibo/timeline.js b/lib/routes/weibo/timeline.js index d81220906..a6a10946a 100644 --- a/lib/routes/weibo/timeline.js +++ b/lib/routes/weibo/timeline.js @@ -8,19 +8,102 @@ module.exports = async (ctx) => { if (token) { const response = await got.get(`https://api.weibo.com/2/statuses/home_timeline.json?access_token=${token}&count=100&feature=${feature}`); - const data = response.data.statuses; + // 获取所有表情 + const weiboEmotion = await ctx.cache.tryGet('weiboEmotion', async () => { + const tmpEmotionList = {}; + await Promise.all( + ['face', 'ani', 'cartoon'].map(async (type) => { + try { + const rep = await got.get(`https://api.weibo.com/2/emotions.json?access_token=${token}&type=${type}`); + rep.data.forEach(function(item) { + tmpEmotionList[item.phrase] = item.url; + }); + return Promise.resolve(rep); + } catch (err) { + // pass + } + }) + ); + return tmpEmotionList; + }); - const links = await Promise.all( - data.map(async (item) => { - const url = await ctx.cache.tryGet('weibotimelineurl' + item.user.id + item.id, async () => { - let result = 'https://weibo.com'; + const data = await Promise.all( + response.data.statuses.map(async (item) => { + const realContent = await ctx.cache.tryGet('weibotimelineurl' + item.user.id + item.id, async () => { + const res = []; + const videoList = []; + let picUrlList = []; + picUrlList = picUrlList.concat(item.pic_urls); + try { + const rep = await got.get(`https://m.weibo.cn/${item.user.id}/${item.id}`); + rep.body.replace(/"text": "([\s\S]*?)",/g, function(s, value) { + res.push(value.replace(/\\"/g, "'")); + }); + rep.body.replace(/"mp4_720p_mp4": "([\s\S]*?)",/, function(s, value) { + videoList.push(value); + }); + if (res.length > 1) { + const sub_rep = await got.get(`https://m.weibo.cn/${item.retweeted_status.user.id}/${item.retweeted_status.id}`); + sub_rep.body.replace(/"text": "([\s\S]*?)",/g, function(s, value) { + res[1] = value.replace(/\\"/g, "'"); + }); + sub_rep.body.replace(/"mp4_720p_mp4": "([\s\S]*?)",/, function(s, value) { + videoList.push(value); + }); + picUrlList = picUrlList.concat(item.retweeted_status.pic_urls); + } + } catch (err) { + res.push(item.text); + if (item.retweeted_status) { + res.push(item.retweeted_status.text); + picUrlList = picUrlList.concat(item.retweeted_status.pic_urls); + } + // 非全文获取的微博内容需要替换表情 + res.forEach(function(content, index) { + const emotion = []; + content.replace(/(\[.*?\])/g, function(s, value) { + emotion.push(`${value}`); + }); + + if (content.length > 0) { + emotion.forEach(function(item) { + res[index] = content.replace(new RegExp(`\\${item}`, 'g'), `${item}`); + }); + } + }); + } + let content = ''; + try { + if (res.length >= 2) { + content = `${res[0]}


${item.retweeted_status.user.screen_name}:

${res[1]}
`; + } else if (res.length === 1) { + content = res[0]; + } + + // 手机端reeder查看内容时,表情会导致换行-此处做转换 + content.replace(/([\s\S]*?)<\/span>/g, function(s, value) { + content = content.replace(s, value); + }); + } catch (error) { + // pass + } + + let title = ''; + if (res.length >= 2) { + title = `${item.user.name}--转发微博`; + } else if (item.pic_urls.length > 0) { + title = `${item.user.name}--原创图文微博`; + } else { + title = `${item.user.name}--原创微博`; + } + let link = 'https://weibo.com'; try { await got.get(`http://api.weibo.com/2/statuses/go?access_token=${token}&id=${item.id}&uid=${item.user.id}`, { hooks: { beforeRedirect: [ (options) => { options.followRedirect = false; - result = options.href; + link = options.href; }, ], }, @@ -28,28 +111,43 @@ module.exports = async (ctx) => { } catch (error) { // nothing } - - return result; + return { + title: title, + content: content, + author: item.user.name, + date: item.created_at, + link: link, + pic_urls: picUrlList, + videoList, + }; }); - return Promise.resolve(url); + return Promise.resolve(realContent); }) ); ctx.state.data = { title: '个人微博时间线', link: 'https://weibo.com', - item: data.map((item, index) => { - let description = item.text; + item: data.map((item) => { + let description = ''; + if (item.content !== null) { + description = item.content; + } + item.pic_urls.forEach((pic) => { - description += `
`; + description += `配图
`; }); - description += item.source; + if (item.videoList.length !== 0) { + item.videoList.forEach((url) => { + description += ``; + }); + } return { description, - link: links[index], - pubDate: item.created_at, - title: `${item.user.name}: ${item.text}`, - author: item.user.name, + link: item.link, + pubDate: item.date, + title: item.title, + author: item.author, }; }), };