fix: 调整微博时间线显示内容 (#3587)

This commit is contained in:
zytomorrow 2019-12-23 17:11:25 +08:00 committed by DIYgod
parent 957a244737
commit c8dfc01275
2 changed files with 116 additions and 18 deletions

View File

@ -706,7 +706,7 @@ rule
### 个人时间线
<Route author="zytomorrow DIYgod" example="/weibo/timeline/3306934123" path="/weibo/timeline/:uid/:feature?" :paramsDesc="['用户的uid', ' 过滤类型ID0全部、1原创、2图片、3视频、4音乐默认为0。']">
<Route author="zytomorrow DIYgod" example="/weibo/timeline/3306934123" path="/weibo/timeline/:uid/:feature?" :paramsDesc="['用户的uid', ' 过滤类型ID0全部、1原创、2图片、3视频、4音乐默认为0。']" anticrawler="1">
::: warning 注意

View File

@ -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'), `<img alt="${item}" src="${weiboEmotion[item]}" style="width:1em; height:1em;" referrerpolicy="no-referrer">`);
});
}
});
}
let content = '';
try {
if (res.length >= 2) {
content = `${res[0]}<br><br><hr style="height:5px;border:none;border-top:5px ridge green;" /><span style="background-color:#71adec">${item.retweeted_status.user.screen_name}:<hr style="height:1px;border:none;border-top:1px solid #555555;" /><br>${res[1]}</span>`;
} else if (res.length === 1) {
content = res[0];
}
// 手机端reeder查看内容时表情会导致换行-此处做转换
content.replace(/<span class='url-icon'>([\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 += `<img src="${pic.thumbnail_pic}"><br>`;
description += `<img alt="配图" style="margin: 0 auto" src="${pic.thumbnail_pic.replace('thumbnail', 'large')}"><br>`;
});
description += item.source;
if (item.videoList.length !== 0) {
item.videoList.forEach((url) => {
description += `<iframe height=720 width=1080 src='${url}' frameborder=0 'allowfullscreen'></iframe>`;
});
}
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,
};
}),
};