From f735bd6fd7667c6c964a7a2b91239dd3ae9b3edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 31 Jul 2022 23:47:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20`ServiceEnum`=20=E6=9B=B4=E5=90=8D?= =?UTF-8?q?=E4=B8=BA=20`RegionEnum`=20=E5=B9=B6=E4=BF=AE=E6=94=B9=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=20`region`=20=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/user/models.py | 6 +++--- model/base.py | 16 +++++++++------ utils/helpers.py | 49 ++++++++++++++++++++++++++-------------------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/app/user/models.py b/app/user/models.py index 81056a2..2214d76 100644 --- a/app/user/models.py +++ b/app/user/models.py @@ -1,11 +1,11 @@ -from model.base import ServiceEnum +from model.base import RegionEnum from model.baseobject import BaseObject class User(BaseObject): def __init__(self, user_id: int = 0, yuanshen_game_uid: int = 0, genshin_game_uid: int = 0, - default_service: ServiceEnum = ServiceEnum.NULL): + region: RegionEnum = RegionEnum.NULL): self.user_id = user_id self.yuanshen_game_uid = yuanshen_game_uid self.genshin_game_uid = genshin_game_uid - self.default_service = default_service + self.region = region diff --git a/model/base.py b/model/base.py index 971b330..703caae 100644 --- a/model/base.py +++ b/model/base.py @@ -41,13 +41,17 @@ class ArtworkImage: self.page = page -class ServiceEnum(Enum): - """ - 该名称来源于米忽悠的安卓BBS包名结尾,考虑到大部分重要的功能确实是在移动端实现了 - """ +class RegionEnum(Enum): + """注册服务器的列举型别 + + HYPERION名称来源于米忽悠BBS的安卓端包名结尾 + + 查了一下确实有点意思 考虑到大部分重要的功能确实是在移动端实现了 + + 干脆用这个还好听 )""" NULL = None - HYPERION = 1 # 米忽悠国服 - HOYOLAB = 2 # 米忽悠国际服 + HYPERION = 1 # 米忽悠国服 hyperion + HOYOLAB = 2 # 米忽悠国际服 hoyolab class GameItem(BaseObject): diff --git a/utils/helpers.py b/utils/helpers.py index 109fc30..da90695 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -1,6 +1,6 @@ import hashlib import os -from typing import Optional +from typing import Union import aiofiles import genshin @@ -11,7 +11,7 @@ from httpx import UnsupportedProtocol from app.cookies.service import CookiesService from app.user import UserService from logger import Log -from model.base import ServiceEnum +from model.base import RegionEnum USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/90.0.4430.72 Safari/537.36" @@ -21,14 +21,14 @@ cache_dir = os.path.join(current_dir, "cache") if not os.path.exists(cache_dir): os.mkdir(cache_dir) -SERVICE_MAP = { - "1": ServiceEnum.HYPERION, - "2": ServiceEnum.HYPERION, - "5": ServiceEnum.HYPERION, - "6": ServiceEnum.HOYOLAB, - "7": ServiceEnum.HOYOLAB, - "8": ServiceEnum.HOYOLAB, - "9": ServiceEnum.HOYOLAB, +REGION_MAP = { + "1": RegionEnum.HYPERION, + "2": RegionEnum.HYPERION, + "5": RegionEnum.HYPERION, + "6": RegionEnum.HOYOLAB, + "7": RegionEnum.HOYOLAB, + "8": RegionEnum.HOYOLAB, + "9": RegionEnum.HOYOLAB, } @@ -61,24 +61,31 @@ async def url_to_file(url: str, prefix: str = "file://") -> str: async def get_genshin_client(user_id: int, user_service: UserService, cookies_service: CookiesService, - game_service: Optional[ServiceEnum] = None) -> Client: + region: RegionEnum = RegionEnum.NULL) -> Client: user = await user_service.get_user_by_id(user_id) - cookies = await cookies_service.read_cookies(user_id, game_service) - if game_service is None: - game_service = user.default_service - if game_service == ServiceEnum.HYPERION: + cookies = await cookies_service.get_cookies(user_id, region) + if region is None: + region = user.region + if region == RegionEnum.HYPERION: uid = user.yuanshen_game_uid client = genshin.Client(cookies=cookies, game=types.Game.GENSHIN, region=types.Region.CHINESE, uid=uid) - else: + elif region == RegionEnum.HOYOLAB: uid = user.genshin_game_uid client = genshin.Client(cookies=cookies, game=types.Game.GENSHIN, region=types.Region.OVERSEAS, lang="zh-cn", uid=uid) + else: + raise TypeError(f"region is not RegionEnum.NULL") return client -def get_server(uid: int) -> ServiceEnum: - server = SERVICE_MAP.get(str(uid)[0]) - if server: - return server +def region_server(uid: Union[int, str]) -> RegionEnum: + if isinstance(uid, int): + region = REGION_MAP.get(str(uid)[0]) + elif isinstance(uid, str): + region = REGION_MAP.get(str(uid)[0]) else: - raise TypeError(f"UID {uid} isn't associated with any server") + raise TypeError(f"UID variable type error") + if region: + return region + else: + raise TypeError(f"UID {uid} isn't associated with any region")