mirror of
https://github.com/xfgryujk/blivechat.git
synced 2025-01-13 13:50:10 +08:00
获取头像失败时尝试用用户名随机生成
This commit is contained in:
parent
b1916608ee
commit
ef7f0dc0ac
@ -266,7 +266,7 @@ class ChatHandler(tornado.websocket.WebSocketHandler):
|
||||
# 测试用
|
||||
async def _send_test_message(self):
|
||||
base_data = {
|
||||
'avatarUrl': await services.avatar.get_avatar_url(300474),
|
||||
'avatarUrl': await services.avatar.get_avatar_url(300474, 'xfgryujk'),
|
||||
'timestamp': int(time.time()),
|
||||
'authorName': 'xfgryujk',
|
||||
}
|
||||
@ -372,9 +372,10 @@ class RoomInfoHandler(api.base.ApiHandler):
|
||||
class AvatarHandler(api.base.ApiHandler):
|
||||
async def get(self):
|
||||
uid = int(self.get_query_argument('uid'))
|
||||
username = self.get_query_argument('username', '')
|
||||
avatar_url = await services.avatar.get_avatar_url_or_none(uid)
|
||||
if avatar_url is None:
|
||||
avatar_url = services.avatar.DEFAULT_AVATAR_URL
|
||||
avatar_url = services.avatar.get_default_avatar_url(uid, username)
|
||||
# 缓存3分钟
|
||||
self.set_header('Cache-Control', 'private, max-age=180')
|
||||
else:
|
||||
|
@ -89,10 +89,11 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase {
|
||||
authorType = 0
|
||||
}
|
||||
|
||||
let authorName = info[2][1]
|
||||
let data = {
|
||||
avatarUrl: await chat.getAvatarUrl(uid),
|
||||
avatarUrl: await chat.getAvatarUrl(uid, authorName),
|
||||
timestamp: info[0][4] / 1000,
|
||||
authorName: info[2][1],
|
||||
authorName: authorName,
|
||||
authorType: authorType,
|
||||
content: info[1],
|
||||
privilegeType: privilegeType,
|
||||
@ -137,7 +138,7 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase {
|
||||
let data = command.data
|
||||
data = {
|
||||
id: getUuid4Hex(),
|
||||
avatarUrl: await chat.getAvatarUrl(data.uid),
|
||||
avatarUrl: await chat.getAvatarUrl(data.uid, data.username),
|
||||
timestamp: data.start_time,
|
||||
authorName: data.username,
|
||||
privilegeType: data.guard_level
|
||||
|
@ -20,11 +20,12 @@ export function processAvatarUrl(avatarUrl) {
|
||||
return avatarUrl
|
||||
}
|
||||
|
||||
export async function getAvatarUrl(uid) {
|
||||
export async function getAvatarUrl(uid, username) {
|
||||
let res
|
||||
try {
|
||||
res = (await axios.get('/api/avatar_url', { params: {
|
||||
uid: uid
|
||||
uid: uid,
|
||||
username: username
|
||||
} })).data
|
||||
} catch {
|
||||
return DEFAULT_AVATAR_URL
|
||||
|
@ -2,6 +2,7 @@
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import datetime
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
from typing import *
|
||||
@ -54,13 +55,24 @@ async def _do_init():
|
||||
_avatar_fetchers = fetchers
|
||||
|
||||
|
||||
async def get_avatar_url(user_id) -> str:
|
||||
async def get_avatar_url(user_id, username) -> str:
|
||||
avatar_url = await get_avatar_url_or_none(user_id)
|
||||
if avatar_url is None:
|
||||
avatar_url = DEFAULT_AVATAR_URL
|
||||
avatar_url = get_default_avatar_url(user_id, username)
|
||||
return avatar_url
|
||||
|
||||
|
||||
def get_default_avatar_url(user_id=0, username=''):
|
||||
if user_id != 0:
|
||||
str_to_hash = str(user_id)
|
||||
elif username != '':
|
||||
str_to_hash = username
|
||||
else:
|
||||
return DEFAULT_AVATAR_URL
|
||||
id_hash = hashlib.md5(str_to_hash.encode('utf-8')).hexdigest()
|
||||
return f'//cravatar.cn/avatar/{id_hash}?s=256&d=robohash&f=y'
|
||||
|
||||
|
||||
async def get_avatar_url_or_none(user_id) -> Optional[str]:
|
||||
if user_id == 0:
|
||||
return None
|
||||
|
@ -394,7 +394,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
|
||||
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
|
||||
else:
|
||||
# 先异步调用再获取房间,因为返回时房间可能已经不存在了
|
||||
avatar_url = await services.avatar.get_avatar_url(message.uid)
|
||||
avatar_url = await services.avatar.get_avatar_url(message.uid, message.uname)
|
||||
|
||||
room = client_room_manager.get_room(client.room_key)
|
||||
if room is None:
|
||||
@ -481,7 +481,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
|
||||
@staticmethod
|
||||
async def __on_buy_guard(client: WebLiveClient, message: dm_web_models.GuardBuyMessage):
|
||||
# 先异步调用再获取房间,因为返回时房间可能已经不存在了
|
||||
avatar_url = await services.avatar.get_avatar_url(message.uid)
|
||||
avatar_url = await services.avatar.get_avatar_url(message.uid, message.username)
|
||||
|
||||
room = client_room_manager.get_room(client.room_key)
|
||||
if room is None:
|
||||
|
Loading…
Reference in New Issue
Block a user