🐛 Fix Bug Due to enkanetwork.py Code Update

5b344f609f
eaab479b8b
This commit is contained in:
洛水居室 2023-02-08 22:56:25 +08:00
parent 138aa27dd6
commit 2cf4b7e55d
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 18 additions and 13 deletions

View File

@ -4,7 +4,7 @@ from typing import Iterable, List, Optional, Sequence
from aiohttp import ClientConnectorError from aiohttp import ClientConnectorError
from arkowrapper import ArkoWrapper from arkowrapper import ArkoWrapper
from enkanetwork import Assets as EnkaAssets, EnkaNetworkAPI, VaildateUIDError, UIDNotFounded, HTTPException from enkanetwork import Assets as EnkaAssets, EnkaNetworkAPI, VaildateUIDError, HTTPException, EnkaPlayerNotFound
from genshin import Client, GenshinException, InvalidCookies from genshin import Client, GenshinException, InvalidCookies
from genshin.models import CalculatorCharacterDetails, CalculatorTalent, Character from genshin.models import CalculatorCharacterDetails, CalculatorTalent, Character
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, Update, User from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, Update, User
@ -65,7 +65,7 @@ class AvatarListPlugin(Plugin, BasePlugin):
self.cookies_service = cookies_service self.cookies_service = cookies_service
self.assets_service = assets_service self.assets_service = assets_service
self.template_service = template_service self.template_service = template_service
self.enka_client = EnkaNetworkAPI(lang="chs", agent=config.enka_network_api_agent) self.enka_client = EnkaNetworkAPI(lang="chs", user_agent=config.enka_network_api_agent)
self.enka_client.set_cache(RedisCache(redis.client, key="plugin:avatar_list:enka_network", ttl=60 * 60 * 3)) self.enka_client.set_cache(RedisCache(redis.client, key="plugin:avatar_list:enka_network", ttl=60 * 60 * 3))
self.enka_assets = EnkaAssets(lang="chs") self.enka_assets = EnkaAssets(lang="chs")
@ -184,7 +184,7 @@ class AvatarListPlugin(Plugin, BasePlugin):
else: else:
rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(response.player.avatar.id)] rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(response.player.avatar.id)]
return name_card, avatar, nickname, rarity return name_card, avatar, nickname, rarity
except (VaildateUIDError, UIDNotFounded, HTTPException) as exc: except (VaildateUIDError, EnkaPlayerNotFound, HTTPException) as exc:
logger.warning("EnkaNetwork 请求失败: %s", str(exc)) logger.warning("EnkaNetwork 请求失败: %s", str(exc))
except (AioHttpTimeoutException, ClientConnectorError) as exc: except (AioHttpTimeoutException, ClientConnectorError) as exc:
logger.warning("EnkaNetwork 请求超时: %s", str(exc)) logger.warning("EnkaNetwork 请求超时: %s", str(exc))

View File

@ -11,12 +11,13 @@ from enkanetwork import (
Equipments, Equipments,
EquipmentsStats, EquipmentsStats,
EquipmentsType, EquipmentsType,
Forbidden,
HTTPException, HTTPException,
Stats, Stats,
StatsPercentage, StatsPercentage,
UIDNotFounded,
VaildateUIDError, VaildateUIDError,
EnkaServerMaintanance,
EnkaServerUnknown,
EnkaServerRateLimit,
) )
from pydantic import BaseModel from pydantic import BaseModel
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
@ -50,7 +51,7 @@ class PlayerCards(Plugin, BasePlugin):
self, user_service: UserService = None, template_service: TemplateService = None, redis: RedisDB = None self, user_service: UserService = None, template_service: TemplateService = None, redis: RedisDB = None
): ):
self.user_service = user_service self.user_service = user_service
self.client = EnkaNetworkAPI(lang="chs", agent=config.enka_network_api_agent, cache=False) self.client = EnkaNetworkAPI(lang="chs", user_agent=config.enka_network_api_agent, cache=False)
self.cache = RedisCache(redis.client, key="plugin:player_cards:enka_network") self.cache = RedisCache(redis.client, key="plugin:player_cards:enka_network")
self.player_cards_file = PlayerCardsFile() self.player_cards_file = PlayerCardsFile()
self.template_service = template_service self.template_service = template_service
@ -61,22 +62,26 @@ class PlayerCards(Plugin, BasePlugin):
data = await self.cache.get(uid) data = await self.cache.get(uid)
if data is not None: if data is not None:
return EnkaNetworkResponse.parse_obj(data) return EnkaNetworkResponse.parse_obj(data)
user = await self.client.http.fetch_user(uid) user = await self.client.http.fetch_user_by_uid(uid)
data = user["content"].decode("utf-8", "surrogatepass") # type: ignore data = user["content"].decode("utf-8", "surrogatepass") # type: ignore
data = ujson.loads(data) data = ujson.loads(data)
data = await self.player_cards_file.merge_info(uid, data) data = await self.player_cards_file.merge_info(uid, data)
await self.cache.set(uid, data) await self.cache.set(uid, data)
return EnkaNetworkResponse.parse_obj(data) return EnkaNetworkResponse.parse_obj(data)
except EnkaServerError:
error = "Enka.Network 服务请求错误,请稍后重试"
except Forbidden:
error = "Enka.Network 服务请求被拒绝,请稍后重试"
except AioHttpTimeoutException: except AioHttpTimeoutException:
error = "Enka.Network 服务请求超时,请稍后重试" error = "Enka.Network 服务请求超时,请稍后重试"
except EnkaServerRateLimit:
error = "Enka.Network 已对此API进行速率限制请稍后重试"
except EnkaServerMaintanance:
error = "Enka.Network 正在维护请等待5-8小时或1天"
except EnkaServerError:
error = "Enka.Network 服务请求错误,请稍后重试"
except EnkaServerUnknown:
error = "Enka.Network 服务瞬间爆炸,请稍后重试"
except (VaildateUIDError, VaildateUIDError):
error = "UID 未找到,可能为服务器抽风,请稍后重试"
except HTTPException: except HTTPException:
error = "Enka.Network HTTP 服务请求错误,请稍后重试" error = "Enka.Network HTTP 服务请求错误,请稍后重试"
except (UIDNotFounded, VaildateUIDError):
error = "UID 未找到,可能为服务器抽风,请稍后重试"
old_data = await self.player_cards_file.load_history_info(uid) old_data = await self.player_cards_file.load_history_info(uid)
if old_data is not None: if old_data is not None:
logger.warning("UID %s | 角色卡片使用历史数据 | %s", uid, error) logger.warning("UID %s | 角色卡片使用历史数据 | %s", uid, error)