feat: 微博全文和准确的微博发布时间 (#2546)
This commit is contained in:
+41
-13
@@ -1,6 +1,5 @@
|
||||
const got = require('@/utils/got');
|
||||
const weiboUtils = require('./utils');
|
||||
const date = require('@/utils/date');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const uid = ctx.params.uid;
|
||||
@@ -28,22 +27,51 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
});
|
||||
|
||||
const resultItem = await Promise.all(
|
||||
response.data.data.cards
|
||||
.filter((item) => item.mblog && !item.mblog.isTop)
|
||||
.map(async (item) => {
|
||||
let data = {};
|
||||
const key = 'weibo:user:' + item.mblog.bid;
|
||||
const value = await ctx.cache.get(key);
|
||||
if (value) {
|
||||
data = JSON.parse(value);
|
||||
} else {
|
||||
// 可以判断出是否是长微博,如果不是那么不调下边的接口获取完整全文也行,但是拿不到准确的created_at,而是“几小时前”这种
|
||||
// 索性就都再调接口获取详细data了,这个接口只返回json所以性能消耗不大。另外做了缓存处理
|
||||
const link = `https://m.weibo.cn/statuses/show?id=${item.mblog.bid}`;
|
||||
const itemResponse = await got.get(link, {
|
||||
headers: {
|
||||
Referer: `https://m.weibo.cn/u/${uid}`,
|
||||
'MWeibo-Pwa': 1,
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
|
||||
},
|
||||
});
|
||||
data = itemResponse.data.data;
|
||||
ctx.cache.set(key, JSON.stringify(data));
|
||||
}
|
||||
|
||||
item.mblog.text = data.text;
|
||||
const description = weiboUtils.format(item.mblog);
|
||||
const title = description.replace(/<img.*?>/g, '[图片]').replace(/<.*?>/g, '');
|
||||
const pubDate = new Date(data.created_at).toUTCString();
|
||||
|
||||
const it = {
|
||||
title: title,
|
||||
description: description,
|
||||
link: `https://weibo.com/${uid}/${item.mblog.bid}`,
|
||||
pubDate: pubDate,
|
||||
};
|
||||
return Promise.resolve(it);
|
||||
})
|
||||
);
|
||||
|
||||
ctx.state.data = {
|
||||
title: `${name}的微博`,
|
||||
link: `http://weibo.com/${uid}/`,
|
||||
description: description,
|
||||
image: profileImageUrl,
|
||||
item: response.data.data.cards
|
||||
.filter((item) => item.mblog && !item.mblog.isTop)
|
||||
.map((item) => {
|
||||
const description = weiboUtils.format(item.mblog);
|
||||
const title = description.replace(/<img.*?>/g, '[图片]').replace(/<.*?>/g, '');
|
||||
return {
|
||||
title,
|
||||
description: description,
|
||||
pubDate: date(item.mblog.created_at, 8),
|
||||
link: `https://weibo.com/${uid}/${item.mblog.bid}`,
|
||||
};
|
||||
}),
|
||||
item: resultItem,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user