skyeweave/atlasimagecomposer/backend/atlas.py

113 lines
3.7 KiB
Python
Raw Normal View History

from typing import Annotated, List, NotRequired, Tuple, TypedDict
2024-08-09 16:57:33 +00:00
import requests
from ..config import AtlasDefaults, Paths, ExpressionDefaults
2024-10-05 12:01:50 +00:00
class SpritesheetData(TypedDict):
facesize: Tuple[int, int]
2024-10-05 12:01:50 +00:00
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
}
2024-08-09 16:57:33 +00:00
return returndata
2024-08-09 16:57:33 +00:00
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):
2024-10-05 12:01:50 +00:00
atlasurl_base = f"https://static.atlasacademy.io/{AtlasDefaults.REGION}/CharaFigure/{imageid}"
2024-08-09 16:57:33 +00:00
savefolder = Paths.IMAGES / basefolder / str(imageid)
2024-08-09 16:57:33 +00:00
if not savefolder.is_dir():
2024-10-05 12:01:50 +00:00
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
2024-10-05 12:01:50 +00:00
2024-08-09 16:57:33 +00:00
def fetch_data(servantid: int) -> List[str]:
atlasurl = f"https://api.atlasacademy.io/nice/{AtlasDefaults.REGION}/servant/{servantid}?lore=false&lang=en"
2024-08-09 16:57:33 +00:00
2024-10-05 12:01:50 +00:00
response = requests.get(atlasurl, timeout=AtlasDefaults.TIMEOUT)
2024-08-09 16:57:33 +00:00
if not response.ok:
raise ValueError(f"{response.status_code} - {response.text}")
2024-08-09 16:57:33 +00:00
responsedata = response.json()
svtname = responsedata["name"]
charascripts: List[dict[str, str]] = responsedata["charaScripts"]
chara_ids: List[str] = [chara["id"] for chara in charascripts]
2024-10-05 12:01:50 +00:00
print(f"{svtname} ({servantid}) - {len(chara_ids)} charaIds")
return chara_ids