from typing import Annotated, List, NotRequired, Tuple, TypedDict import requests from ..config import AtlasDefaults, Paths, ExpressionDefaults class SpritesheetData(TypedDict): facesize: Tuple[int, int] position: Tuple[int, int] class ExtendData(TypedDict): faceSizeRect: NotRequired[Annotated[List[int], 2]] faceSize: NotRequired[int] def fetch_config(chara_id: str) -> SpritesheetData: url = f"https://api.atlasacademy.io/raw/JP/svtScript?charaId={chara_id}" response = requests.get(url, timeout=AtlasDefaults.TIMEOUT) if not response.ok: raise ValueError(f"{response.status_code} - {response.text}") resp_data = response.json()[0] extend_data: ExtendData = resp_data["extendData"] if "faceSizeRect" in extend_data: facesize: Tuple[int, int] = tuple(extend_data["faceSizeRect"]) # type: ignore else: facesize = tuple(2 * [ extend_data.get("faceSize", ExpressionDefaults.FACESIZE) ]) # type: ignore position: tuple[int, int] = (resp_data["faceX"], resp_data["faceY"]) returndata: SpritesheetData = { "facesize": facesize, "position": position } return returndata def fetch_mstsvtjson(): url = AtlasDefaults.MST_SVT_JSON filelocation = Paths.IMAGES / "mstsvt.json" if filelocation.exists(): print("Found cached asset for mstsvt.json") return with open(filelocation, 'wb') as handle: response = requests.get(url, stream=True, timeout=AtlasDefaults.TIMEOUT) status = response.status_code if status != 200: raise ValueError("Could not fetch mstsvnt.json from atlas - please check your network connection") for block in response.iter_content(1024): if not block: break handle.write(block) def fetch_expression_sheets(basefolder: str, imageid: str): atlasurl_base = f"https://static.atlasacademy.io/{AtlasDefaults.REGION}/CharaFigure/{imageid}" savefolder = Paths.IMAGES / basefolder / str(imageid) if not savefolder.is_dir(): savefolder.mkdir(exist_ok=True, parents=True) idx, status = 0, 200 while status == 200: filelocation = savefolder / f"{idx}.png" postfix = "" if idx == 1: postfix = "f" elif idx > 1: postfix = f"f{idx}" if filelocation.exists(): print(f"Found cached asset for {imageid}{postfix}.png") idx += 1 continue filename = f"{imageid}{postfix}.png" atlasurl = f"{atlasurl_base}/{filename}" with open(filelocation, 'wb') as handle: response = requests.get(atlasurl, stream=True, timeout=AtlasDefaults.TIMEOUT) status = response.status_code if status != 200: continue for block in response.iter_content(1024): if not block: break handle.write(block) print(f"Finished downloading {filename}") idx += 1 p = savefolder / f"{idx}.png" p.unlink(missing_ok=True) return savefolder def fetch_data(servantid: int) -> List[str]: atlasurl = f"https://api.atlasacademy.io/nice/{AtlasDefaults.REGION}/servant/{servantid}?lore=false&lang=en" response = requests.get(atlasurl, timeout=AtlasDefaults.TIMEOUT) if not response.ok: raise ValueError(f"{response.status_code} - {response.text}") responsedata = response.json() svtname = responsedata["name"] charascripts: List[dict[str, str]] = responsedata["charaScripts"] chara_ids: List[str] = [chara["id"] for chara in charascripts] print(f"{svtname} ({servantid}) - {len(chara_ids)} charaIds") return chara_ids