2023-10-19 10:58:00 +00:00
|
|
|
|
from typing import Optional
|
|
|
|
|
|
2023-08-02 12:11:35 +00:00
|
|
|
|
from gram_core.base_service import BaseService
|
|
|
|
|
from gram_core.basemodel import RegionEnum
|
|
|
|
|
from gram_core.services.cookies.error import CookieServiceError
|
|
|
|
|
from gram_core.services.cookies.models import CookiesStatusEnum, CookiesDataBase as Cookies
|
|
|
|
|
from gram_core.services.cookies.services import (
|
|
|
|
|
CookiesService,
|
|
|
|
|
PublicCookiesService as BasePublicCookiesService,
|
|
|
|
|
NeedContinue,
|
|
|
|
|
)
|
2022-08-04 13:19:17 +00:00
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
|
from simnet import GenshinClient, Region, Game
|
2023-11-22 16:51:46 +00:00
|
|
|
|
from simnet.errors import InvalidCookies, TooManyRequests, BadRequest as SimnetBadRequest, NeedChallenge, InvalidDevice
|
2022-08-04 13:19:17 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
__all__ = ("CookiesService", "PublicCookiesService")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
2023-08-02 12:11:35 +00:00
|
|
|
|
class PublicCookiesService(BaseService, BasePublicCookiesService):
|
2023-10-19 10:58:00 +00:00
|
|
|
|
async def initialize(self) -> None:
|
|
|
|
|
logger.info("正在初始化公共Cookies池")
|
|
|
|
|
await self.refresh()
|
|
|
|
|
logger.success("刷新公共Cookies池成功")
|
|
|
|
|
|
2023-10-17 14:56:03 +00:00
|
|
|
|
async def check_public_cookie(self, region: RegionEnum, cookies: Cookies, public_id: int): # skipcq: PY-R1000 #
|
2023-10-19 10:58:00 +00:00
|
|
|
|
device_id: Optional[str] = None
|
|
|
|
|
device_fp: Optional[str] = None
|
|
|
|
|
devices = await self.devices_repository.get(cookies.account_id)
|
|
|
|
|
if devices:
|
|
|
|
|
device_id = devices.device_id
|
|
|
|
|
device_fp = devices.device_fp
|
|
|
|
|
|
2023-08-02 12:11:35 +00:00
|
|
|
|
if region == RegionEnum.HYPERION:
|
2023-10-19 10:58:00 +00:00
|
|
|
|
client = GenshinClient(
|
|
|
|
|
cookies=cookies.data, region=Region.CHINESE, device_id=device_id, device_fp=device_fp
|
|
|
|
|
)
|
2023-08-02 12:11:35 +00:00
|
|
|
|
elif region == RegionEnum.HOYOLAB:
|
2023-10-19 10:58:00 +00:00
|
|
|
|
client = GenshinClient(
|
|
|
|
|
cookies=cookies.data, region=Region.OVERSEAS, lang="zh-cn", device_id=device_id, device_fp=device_fp
|
|
|
|
|
)
|
2023-08-02 12:11:35 +00:00
|
|
|
|
else:
|
|
|
|
|
raise CookieServiceError
|
|
|
|
|
try:
|
|
|
|
|
if client.account_id is None:
|
|
|
|
|
raise RuntimeError("account_id not found")
|
|
|
|
|
record_cards = await client.get_record_cards()
|
2023-10-19 10:58:00 +00:00
|
|
|
|
for record_card in record_cards:
|
|
|
|
|
if record_card.game == Game.GENSHIN:
|
|
|
|
|
await client.get_partial_genshin_user(record_card.uid)
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
accounts = await client.get_game_accounts()
|
|
|
|
|
for account in accounts:
|
|
|
|
|
if account.game == Game.GENSHIN:
|
|
|
|
|
await client.get_partial_genshin_user(account.uid)
|
2023-04-04 15:45:07 +00:00
|
|
|
|
break
|
2023-08-02 12:11:35 +00:00
|
|
|
|
except InvalidCookies as exc:
|
|
|
|
|
if exc.ret_code in (10001, -100):
|
|
|
|
|
logger.warning("用户 [%s] Cookies无效", public_id)
|
|
|
|
|
elif exc.ret_code == 10103:
|
|
|
|
|
logger.warning("用户 [%s] Cookies有效,但没有绑定到游戏帐户", public_id)
|
|
|
|
|
else:
|
|
|
|
|
logger.warning("Cookies无效 ")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
cookies.status = CookiesStatusEnum.INVALID_COOKIES
|
|
|
|
|
await self._repository.update(cookies)
|
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise NeedContinue
|
|
|
|
|
except TooManyRequests:
|
|
|
|
|
logger.warning("用户 [%s] 查询次数太多或操作频繁", public_id)
|
|
|
|
|
cookies.status = CookiesStatusEnum.TOO_MANY_REQUESTS
|
|
|
|
|
await self._repository.update(cookies)
|
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise NeedContinue
|
2023-11-17 12:01:41 +00:00
|
|
|
|
except NeedChallenge:
|
|
|
|
|
logger.warning("用户 [%s] 触发验证", public_id)
|
|
|
|
|
await self.set_device_valid(client.account_id, False)
|
2023-11-22 16:51:46 +00:00
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise NeedContinue
|
|
|
|
|
except InvalidDevice:
|
|
|
|
|
logger.warning("用户 [%s] 设备信息无效", public_id)
|
|
|
|
|
await self.set_device_valid(client.account_id, False)
|
2023-11-17 12:01:41 +00:00
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise NeedContinue
|
2023-08-02 12:11:35 +00:00
|
|
|
|
except SimnetBadRequest as exc:
|
|
|
|
|
if "invalid content type" in exc.message:
|
|
|
|
|
raise exc
|
2023-11-17 12:01:41 +00:00
|
|
|
|
logger.warning("用户 [%s] 获取账号信息发生错误,错误信息为", public_id)
|
|
|
|
|
logger.exception(exc)
|
2023-08-02 12:11:35 +00:00
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise NeedContinue
|
|
|
|
|
except RuntimeError as exc:
|
|
|
|
|
if "account_id not found" in str(exc):
|
2022-08-04 13:19:17 +00:00
|
|
|
|
cookies.status = CookiesStatusEnum.INVALID_COOKIES
|
2023-03-14 01:27:22 +00:00
|
|
|
|
await self._repository.update(cookies)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
2023-08-02 12:11:35 +00:00
|
|
|
|
raise NeedContinue
|
|
|
|
|
raise exc
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
await self._cache.delete_public_cookies(cookies.user_id, region)
|
|
|
|
|
raise exc
|
|
|
|
|
finally:
|
|
|
|
|
await client.shutdown()
|