获取头像失败时尝试用用户名随机生成

This commit is contained in:
John Smith
2023-09-16 11:31:46 +08:00
parent b1916608ee
commit ef7f0dc0ac
5 changed files with 26 additions and 11 deletions

View File

@@ -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

View File

@@ -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: