Add Verify Client

This commit is contained in:
luoshuijs 2023-11-19 00:22:22 +08:00 committed by GitHub
parent 720e7c5832
commit 9d2a984620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,86 @@
import json
import re
import time
from typing import Optional
from httpx import Headers
from simnet import Region
from simnet.client.base import BaseClient
from simnet.client.routes import URL
from simnet.errors import RegionNotSupported
class VerifyClient(BaseClient):
"""VerifyClient component."""
HOST = URL("https://api-takumi-record.mihoyo.com")
VERIFICATION_HOST = URL("https://api.geetest.com")
CREATE_VERIFICATION_URL = HOST / "/game_record/app/card/wapi/createVerification"
VERIFY_VERIFICATION_URL = HOST / "/game_record/app/card/wapi/verifyVerification"
AJAX_URL = VERIFICATION_HOST / "/ajax.php"
async def create_verification(self, is_high: bool = False):
"""Create a verification challenge.
Args:
is_high (bool): Ifs the challenge for a high risk device.
Returns:
str: The challenge.
"""
if self.region != Region.CHINESE:
raise RegionNotSupported("This method is only available for the Chinese region.")
params = {"is_high": "true" if is_high else "false"}
return await self.request_lab(self.CREATE_VERIFICATION_URL, params=params)
async def verify_verification(self, challenge: str, validate: str):
"""Verify a verification challenge.
Args:
challenge (str): The challenge code.
validate (str): The validate code.
Returns:
str: The verification result.
"""
if self.region != Region.CHINESE:
raise RegionNotSupported("This method is only available for the Chinese region.")
data = {"geetest_challenge": challenge, "geetest_validate": validate, "geetest_seccode": f"{validate}|jordan"}
return await self.request_lab(self.VERIFY_VERIFICATION_URL, data=data)
async def request_verify_ajax(self, referer: str, gt: str, challenge: str) -> Optional[str]:
"""Get the ajax validate code.
Args:
referer (str): The web referer.
gt (str): The gt challenge.
challenge (str): The challenge code.
Returns:
Optional[str]: The ajax validate code.
"""
if self.region != Region.CHINESE:
raise RegionNotSupported("This method is only available for the Chinese region.")
params = {
"gt": gt,
"challenge": challenge,
"lang": "zh-cn",
"pt": 3,
"client_type": "web_mobile",
"callback": f"geetest_{int(time.time() * 1000)}",
}
headers = Headers()
headers["user-agent"] = self.user_agent
headers["x-rpc-app_version"] = self.app_version
headers["x-rpc-client_type"] = self.client_type
headers["x-rpc-device_id"] = self.get_device_id()
headers["x-rpc-device_fp"] = self.get_device_fp()
headers["referer"] = referer
response = await self.request("GET", self.AJAX_URL, params=params, headers=headers)
text = response.text
json_data = re.findall(r"^.*?\((\{.*?)\)$", text)[0]
data = json.loads(json_data)
if "success" in data["status"] and "success" in data["data"]["result"]:
return data["data"]["validate"]
return None

View File

@ -7,6 +7,7 @@ from simnet.client.components.daily import DailyRewardClient
from simnet.client.components.diary.genshin import GenshinDiaryClient
from simnet.client.components.lab import LabClient
from simnet.client.components.transaction import TransactionClient
from simnet.client.components.verify import VerifyClient
from simnet.client.components.wish.genshin import GenshinWishClient
from simnet.utils.enum_ import Game
@ -22,6 +23,7 @@ class GenshinClient(
DailyRewardClient,
LabClient,
TransactionClient,
VerifyClient,
):
"""A simple http client for Genshin endpoints."""

View File

@ -7,10 +7,12 @@ from simnet.client.components.daily import DailyRewardClient
from simnet.client.components.diary.genshin import GenshinDiaryClient
from simnet.client.components.lab import LabClient
from simnet.client.components.transaction import TransactionClient
from simnet.client.components.verify import VerifyClient
from simnet.client.components.wish.genshin import GenshinWishClient
from simnet.utils.enum_ import Region
from simnet.utils.types import CookieTypes, HeaderTypes, TimeoutTypes
class GenshinClient(
CalculatorClient,
GenshinBattleChronicleClient,
@ -20,6 +22,7 @@ class GenshinClient(
DailyRewardClient,
LabClient,
TransactionClient,
VerifyClient,
):
def __init__(
self,

View File

@ -5,6 +5,7 @@ from simnet.client.components.chronicle.starrail import StarRailBattleChronicleC
from simnet.client.components.daily import DailyRewardClient
from simnet.client.components.diary.starrail import StarrailDiaryClient
from simnet.client.components.lab import LabClient
from simnet.client.components.verify import VerifyClient
from simnet.client.components.wish.starrail import StarRailWishClient
from simnet.utils.enum_ import Game
@ -18,6 +19,7 @@ class StarRailClient(
DailyRewardClient,
AuthClient,
LabClient,
VerifyClient,
):
"""A simple http client for StarRail endpoints."""

View File

@ -5,12 +5,20 @@ from simnet.client.components.chronicle.starrail import StarRailBattleChronicleC
from simnet.client.components.daily import DailyRewardClient
from simnet.client.components.diary.starrail import StarrailDiaryClient
from simnet.client.components.lab import LabClient
from simnet.client.components.verify import VerifyClient
from simnet.client.components.wish.starrail import StarRailWishClient
from simnet.utils.enum_ import Region
from simnet.utils.types import CookieTypes, HeaderTypes, TimeoutTypes
class StarRailClient(
StarRailBattleChronicleClient, StarRailWishClient, StarrailDiaryClient, DailyRewardClient, AuthClient, LabClient
StarRailBattleChronicleClient,
StarRailWishClient,
StarrailDiaryClient,
DailyRewardClient,
AuthClient,
LabClient,
VerifyClient,
):
def __init__(
self,