worthless-launcher/worthless/launcher.py

83 lines
3.1 KiB
Python
Raw Normal View History

2022-01-28 13:08:53 +00:00
import aiohttp
import locale
from worthless import constants
class Launcher:
def __init__(self):
self._api = constants.LAUNCHER_API_URL
self._lang = self._get_system_language()
# Workaround because miHoYo uses retcode for their API
@staticmethod
async def _get(url, **kwargs) -> dict:
async with aiohttp.ClientSession() as session:
rsp = await session.get(url, **kwargs)
rsp_json = await rsp.json()
# Why retcode seriously? They can just make the page not returning 200 status code.
if rsp_json["retcode"] != 0:
raise aiohttp.ClientResponseError(code=rsp_json["retcode"],
message=rsp_json["message"],
history=rsp.history,
request_info=rsp.request_info)
return rsp_json
async def _get_launcher_info(self, adv=True) -> dict:
params = {"key": "gcStgarh",
"filter_adv": str(adv).lower(),
"launcher_id": "10",
"language": self._lang}
rsp = await self._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)
return rsp
@staticmethod
def _get_system_language() -> str:
"""
Get system language compatible with server parameters.
"""
try:
lang = locale.getdefaultlocale()[0]
lowercase_lang = lang.lower().replace("_", "-")
return lowercase_lang
except ValueError:
return "en-us"
async def override_language(self, language: str) -> None:
"""
Override system detected language with another language.
:param language: The language to override with.
:return: None
"""
self._lang = language.lower().replace("_", "-")
async def get_version_info(self) -> dict:
rsp = await self._get(self._api + "/resource", params={"key": "gcStgarh",
"launcher_id": "10"})
return rsp
async def get_launcher_info(self) -> dict:
"""
This function will get short launcher info from the server (only contains background image and FAQ)
:return: dict: Launcher info from the server.
"""
return await self._get_launcher_info(adv=True)
async def get_launcher_full_info(self) -> dict:
"""
This function will get full launcher info from the server.
Since the server content is very long for a short explanation, you should see it manually.
:return: dict: Launcher info from the server.
"""
return await self._get_launcher_info(adv=False)
async def get_launcher_background_url(self) -> str:
"""
This function will get launcher background image from the server.
:return: str: Background image URL.
"""
rsp = await self.get_launcher_info()
return rsp["data"]["adv"]["background"]