Wrap game resource info from the server

Also add function to get the diff archive (for faster updating)

Download function soon, although I can't make sure that the download function will work properly (like pause, resume download etc.)
This commit is contained in:
2022-02-16 22:18:56 +07:00
parent 81fbdec553
commit da3ee30ab1
14 changed files with 227 additions and 84 deletions

View File

@ -2,7 +2,36 @@ import aiohttp
import locale
from worthless import constants
from pathlib import Path
from worthless.classes import launcher
from worthless.classes import launcher, installer
async def _get(url, **kwargs) -> dict:
# Workaround because miHoYo uses retcode for their API instead of HTTP status code
async with aiohttp.ClientSession() as session:
rsp = await session.get(url, **kwargs)
rsp_json = await rsp.json()
if rsp_json["retcode"] != 0:
# TODO: Add more information to the error message
raise aiohttp.ClientResponseError(code=rsp_json["retcode"],
message=rsp_json["message"],
history=rsp.history,
request_info=rsp.request_info)
return rsp_json
def _get_system_language() -> str:
"""Gets system language compatible with server parameters.
Return:
System language with format xx-xx.
"""
try:
lang = locale.getdefaultlocale()[0]
lowercase_lang = lang.lower().replace("_", "-")
return lowercase_lang
except ValueError:
return "en-us" # Fallback to English if locale is not supported
class Launcher:
@ -10,7 +39,7 @@ class Launcher:
Contains functions to get information from server and client like the official launcher.
"""
def __init__(self, gamedir=Path.cwd(), language=None, overseas=True):
def __init__(self, gamedir: str | Path = Path.cwd(), language: str = None, overseas=True):
"""Initialize the launcher API
Args:
@ -23,7 +52,7 @@ class Launcher:
"key": "gcStgarh",
"launcher_id": "10",
}
self._lang = self._get_system_language() if not language else language.lower().replace("_", "-")
self._lang = language.lower().replace("_", "-") if language else _get_system_language()
else:
self._api = constants.LAUNCHER_API_URL_CN
self._params = {
@ -36,42 +65,13 @@ class Launcher:
gamedir = Path(gamedir)
self._gamedir = gamedir.resolve()
@staticmethod
async def _get(url, **kwargs) -> dict:
# Workaround because miHoYo uses retcode for their API instead of HTTP status code
async with aiohttp.ClientSession() as session:
rsp = await session.get(url, **kwargs)
rsp_json = await rsp.json()
if rsp_json["retcode"] != 0:
# TODO: Add more information to the error message
raise aiohttp.ClientResponseError(code=rsp_json["retcode"],
message=rsp_json["message"],
history=rsp.history,
request_info=rsp.request_info)
return rsp_json
@staticmethod
def _get_system_language() -> str:
"""Gets system language compatible with server parameters.
Return:
System language with format xx-xx.
"""
try:
lang = locale.getdefaultlocale()[0]
lowercase_lang = lang.lower().replace("_", "-")
return lowercase_lang
except ValueError:
return "en-us" # Fallback to English if locale is not supported
async def _get_launcher_info(self, adv=True) -> launcher.Info:
params = self._params | {"filter_adv": str(adv).lower(),
"language": self._lang}
rsp = await self._get(self._api + "/content", params=params)
rsp = await _get(self._api + "/content", params=params)
if rsp["data"]["adv"] is None:
params["language"] = "en-us"
rsp = await self._get(self._api + "/content", params=params)
rsp = await _get(self._api + "/content", params=params)
lc_info = launcher.Info.from_dict(rsp["data"])
return lc_info
@ -94,7 +94,7 @@ class Launcher:
self._lang = language.lower().replace("_", "-")
async def get_version_info(self) -> dict:
async def get_resource_info(self) -> installer.Resource:
"""Gets version info from the server.
This function gets version info including audio pack and their download url from the server.
@ -105,8 +105,8 @@ class Launcher:
aiohttp.ClientResponseError: An error occurred while fetching the information.
"""
rsp = await self._get(self._api + "/resource", params=self._params)
return rsp
rsp = await _get(self._api + "/resource", params=self._params)
return installer.Resource.from_dict(rsp["data"])
async def get_launcher_info(self) -> launcher.Info:
"""Gets short launcher info from the server
@ -143,16 +143,3 @@ class Launcher:
rsp = await self.get_launcher_info()
return rsp.background.background
async def get_system_game_info(self, table_handle, keys, require_all_keys):
# TODO: Implement
raise NotImplementedError("Not implemented yet.")
pass
async def get_system_game_version(self) -> str:
"""Gets the game version from the current system.
:return: str: System game version.
"""
rsp = await self.get_version_info()
return rsp["data"]["system"]["game_version"]