2023-02-15 08:33:49 +00:00
|
|
|
from base64 import b64decode as parse_token
|
2022-10-22 03:56:36 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from typing import Iterator
|
|
|
|
|
2022-10-07 05:02:49 +00:00
|
|
|
import ujson as json
|
|
|
|
from aiofiles import open as async_open
|
2023-03-14 01:27:22 +00:00
|
|
|
from httpx import URL, AsyncClient, RemoteProtocolError, Response
|
2022-10-07 05:02:49 +00:00
|
|
|
|
|
|
|
from utils.const import AMBR_HOST, PROJECT_ROOT
|
2022-10-22 03:56:36 +00:00
|
|
|
from utils.log import logger
|
2022-10-07 05:02:49 +00:00
|
|
|
|
2023-02-16 08:47:48 +00:00
|
|
|
__all__ = ["update_metadata_from_ambr", "update_metadata_from_github", "make_github_fast", "RESOURCE_DEFAULT_PATH"]
|
2023-02-15 08:33:49 +00:00
|
|
|
GENSHIN_PY_DATA_REPO = parse_token("aHR0cHM6Ly9naXRsYWIuY29tL0RpbWJyZWF0aC9nYW1lZGF0YS8tL3Jhdy9tYXN0ZXIv").decode()
|
|
|
|
RESOURCE_REPO = "PaiGramTeam/PaiGram_Resources"
|
|
|
|
RESOURCE_BRANCH = "remote"
|
2022-11-03 10:33:13 +00:00
|
|
|
RESOURCE_ROOT = "Resources"
|
|
|
|
RESOURCE_DEFAULT_PATH = f"{RESOURCE_REPO}/{RESOURCE_BRANCH}/{RESOURCE_ROOT}/"
|
2022-10-07 05:02:49 +00:00
|
|
|
|
|
|
|
client = AsyncClient()
|
|
|
|
|
|
|
|
|
|
|
|
async def update_metadata_from_ambr(overwrite: bool = True):
|
|
|
|
result = []
|
2022-10-10 11:07:28 +00:00
|
|
|
targets = ["material", "weapon", "avatar", "reliquary"]
|
2022-10-07 05:02:49 +00:00
|
|
|
for target in targets:
|
2022-10-10 11:07:28 +00:00
|
|
|
path = PROJECT_ROOT.joinpath(f"metadata/data/{target}.json")
|
2022-10-07 05:02:49 +00:00
|
|
|
if not overwrite and path.exists():
|
|
|
|
continue
|
|
|
|
url = AMBR_HOST.join(f"v2/chs/{target}")
|
|
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
response = await client.get(url)
|
2022-10-10 11:07:28 +00:00
|
|
|
json_data = json.loads(response.text)["data"]["items"]
|
|
|
|
async with async_open(path, mode="w", encoding="utf-8") as file:
|
2022-10-07 05:02:49 +00:00
|
|
|
data = json.dumps(json_data, ensure_ascii=False)
|
|
|
|
await file.write(data)
|
|
|
|
result.append(json_data)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-10-22 03:56:36 +00:00
|
|
|
@contextmanager
|
|
|
|
async def stream_request(method, url) -> Iterator[Response]:
|
2022-12-25 12:01:18 +00:00
|
|
|
async with client.stream(method=method, url=url) as response:
|
2022-10-22 03:56:36 +00:00
|
|
|
yield response
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyShadowingNames
|
2022-10-07 05:02:49 +00:00
|
|
|
async def update_metadata_from_github(overwrite: bool = True):
|
2022-10-10 11:07:28 +00:00
|
|
|
path = PROJECT_ROOT.joinpath("metadata/data/namecard.json")
|
2022-10-07 05:02:49 +00:00
|
|
|
if not overwrite and path.exists():
|
|
|
|
return
|
|
|
|
|
2022-10-22 03:56:36 +00:00
|
|
|
hosts = [
|
2022-11-03 10:33:13 +00:00
|
|
|
URL(f"https://ghproxy.net/https://raw.githubusercontent.com/{RESOURCE_DEFAULT_PATH}"),
|
|
|
|
URL(f"https://github.91chi.fun/https://raw.githubusercontent.com/{RESOURCE_DEFAULT_PATH}"),
|
|
|
|
URL(f"https://raw.fastgit.org/{RESOURCE_DEFAULT_PATH}"),
|
|
|
|
URL(f"https://raw.githubusercontent.com/{RESOURCE_DEFAULT_PATH}"),
|
2022-10-22 03:56:36 +00:00
|
|
|
]
|
|
|
|
for num, host in enumerate(hosts):
|
|
|
|
try:
|
|
|
|
text_map_url = host.join("TextMap/TextMapCHS.json")
|
|
|
|
material_url = host.join("ExcelBinOutput/MaterialExcelConfigData.json")
|
2022-10-07 05:02:49 +00:00
|
|
|
|
2022-10-22 03:56:36 +00:00
|
|
|
material_json_data = []
|
|
|
|
async with client.stream("GET", material_url) as response:
|
|
|
|
started = False
|
|
|
|
cell = []
|
|
|
|
async for line in response.aiter_lines():
|
2023-04-12 03:23:16 +00:00
|
|
|
if line == " {\n":
|
2022-10-22 03:56:36 +00:00
|
|
|
started = True
|
|
|
|
continue
|
2023-04-12 03:23:16 +00:00
|
|
|
if line in [" },\n", " }\n"]:
|
2022-10-22 03:56:36 +00:00
|
|
|
started = False
|
|
|
|
if any("MATERIAL_NAMECARD" in x for x in cell):
|
|
|
|
material_json_data.append(json.loads("{" + "".join(cell) + "}"))
|
|
|
|
cell = []
|
|
|
|
continue
|
|
|
|
if started:
|
|
|
|
if "materialType" in line and "MATERIAL_NAMECARD" not in line:
|
|
|
|
cell = []
|
|
|
|
started = False
|
|
|
|
continue
|
|
|
|
cell.append(line.strip(" \n"))
|
2022-10-07 05:02:49 +00:00
|
|
|
|
2022-10-22 03:56:36 +00:00
|
|
|
string_ids = []
|
|
|
|
for namecard_data in material_json_data:
|
|
|
|
string_ids.append(str(namecard_data["nameTextMapHash"]))
|
|
|
|
string_ids.append(str(namecard_data["descTextMapHash"]))
|
2022-10-07 05:02:49 +00:00
|
|
|
|
2022-10-22 03:56:36 +00:00
|
|
|
text_map_json_data = {}
|
|
|
|
async with client.stream("GET", text_map_url) as response:
|
|
|
|
async for line in response.aiter_lines():
|
|
|
|
if (string_id := (splits := line.split(":"))[0].strip(' "')) in string_ids:
|
2022-11-03 10:33:13 +00:00
|
|
|
text_map_json_data[string_id] = splits[1].strip('\n ,"')
|
2022-10-22 03:56:36 +00:00
|
|
|
string_ids.remove(string_id)
|
|
|
|
if not string_ids:
|
|
|
|
break
|
|
|
|
|
|
|
|
data = {}
|
|
|
|
for namecard_data in material_json_data:
|
|
|
|
name = text_map_json_data[str(namecard_data["nameTextMapHash"])]
|
|
|
|
icon = namecard_data["icon"]
|
|
|
|
navbar = namecard_data["picPath"][0]
|
|
|
|
banner = namecard_data["picPath"][1]
|
|
|
|
rank = namecard_data["rankLevel"]
|
|
|
|
description = text_map_json_data[str(namecard_data["descTextMapHash"])].replace("\\n", "\n")
|
|
|
|
data.update(
|
|
|
|
{
|
|
|
|
str(namecard_data["id"]): {
|
|
|
|
"id": namecard_data["id"],
|
|
|
|
"name": name,
|
|
|
|
"rank": rank,
|
|
|
|
"icon": icon,
|
|
|
|
"navbar": navbar,
|
|
|
|
"profile": banner,
|
|
|
|
"description": description,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async with async_open(path, mode="w", encoding="utf-8") as file:
|
|
|
|
data = json.dumps(data, ensure_ascii=False)
|
|
|
|
await file.write(data)
|
|
|
|
return data
|
2022-12-25 13:26:08 +00:00
|
|
|
except RemoteProtocolError as exc:
|
|
|
|
logger.warning("在从 %s 下载元数据的过程中遇到了错误: %s", host, str(exc))
|
2022-10-22 03:56:36 +00:00
|
|
|
continue
|
2022-12-25 13:26:08 +00:00
|
|
|
except Exception as exc:
|
2022-10-22 03:56:36 +00:00
|
|
|
if num != len(hosts) - 1:
|
2022-12-25 13:26:08 +00:00
|
|
|
logger.error("在从 %s 下载元数据的过程中遇到了错误: %s", host, str(exc))
|
2022-10-22 03:56:36 +00:00
|
|
|
continue
|
2022-12-25 13:26:08 +00:00
|
|
|
raise exc
|
2022-11-03 10:33:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_github_fast(url: str) -> str:
|
|
|
|
url = url.replace("Dimbreath/GenshinData/master/", RESOURCE_DEFAULT_PATH)
|
2023-02-15 08:33:49 +00:00
|
|
|
return url.replace(
|
|
|
|
GENSHIN_PY_DATA_REPO,
|
2023-03-27 01:04:08 +00:00
|
|
|
f"https://raw.githubusercontent.com/{RESOURCE_DEFAULT_PATH}",
|
2023-02-15 08:33:49 +00:00
|
|
|
)
|