skyeweave/atlasimagecomposer/backend/atlas.py

89 lines
2.7 KiB
Python
Raw Permalink Normal View History

2024-10-05 12:01:50 +00:00
from typing import List, Tuple, TypedDict
2024-08-09 16:57:33 +00:00
import requests
2024-10-05 12:01:50 +00:00
from ..config import AtlasDefaults, ExpressionDefaults, Paths
class SpritesheetData(TypedDict):
facesize: int
position: Tuple[int, int]
class ReturnData(TypedDict):
id: str
faceX: int
faceY: int
extendData: dict[str, int]
2024-08-09 16:57:33 +00:00
2024-10-05 12:01:50 +00:00
def fetch_expression_sheets(servantid: int, imageid: str):
atlasurl_base = f"https://static.atlasacademy.io/{AtlasDefaults.REGION}/CharaFigure/{imageid}"
2024-08-09 16:57:33 +00:00
2024-10-05 12:01:50 +00:00
savefolder = Paths.IMAGES / str(servantid) / 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)
def fetch_info(servantid: int):
2024-10-04 18:18:56 +00:00
atlasurl = f"https://api.atlasacademy.io/basic/{AtlasDefaults.REGION}/servant/{servantid}?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:
print(response)
data = response.json()
print(f"Fetching data and sprites for {data['name']} (ID: {servantid})")
2024-10-05 12:01:50 +00:00
def fetch_data(servantid: int) -> dict[str, SpritesheetData]:
2024-08-09 16:57:33 +00:00
fetch_info(servantid)
2024-10-04 18:18:56 +00:00
atlasurl = f"https://api.atlasacademy.io/raw/{AtlasDefaults.REGION}/servant/{servantid}?lore=false&expand=true&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:
print(response)
2024-10-05 12:01:50 +00:00
data: List[ReturnData] = response.json()["mstSvtScript"]
spritesheet_data: dict[str, SpritesheetData] = {
2024-10-04 17:30:40 +00:00
str(spritesheet["id"]): {
2024-10-05 12:01:50 +00:00
"facesize": spritesheet["extendData"].get("faceSize", ExpressionDefaults.FACESIZE),
2024-10-04 17:30:40 +00:00
"position": (spritesheet["faceX"], spritesheet["faceY"])
2024-10-05 12:01:50 +00:00
} for spritesheet in data
2024-10-04 17:30:40 +00:00
}
2024-10-05 12:01:50 +00:00
print(f"Found a total of {len(spritesheet_data)} spritesheets")
return spritesheet_data