Skip to content

Instantly share code, notes, and snippets.

@c2t-r
Created May 4, 2024 07:32
Show Gist options
  • Save c2t-r/0e3c6bbce8747dfd969386e1366c57e8 to your computer and use it in GitHub Desktop.
Save c2t-r/0e3c6bbce8747dfd969386e1366c57e8 to your computer and use it in GitHub Desktop.
download GI map from hoyolab
import json
import os
import requests
from PIL import Image
import asyncio
from io import BytesIO
lang = "en-us"
dir = "GI"
def makemap(obj: dict, name: str):
img = Image.new("RGBA", tuple(obj["total_size"]), (255, 255, 255, 0))
count = 0
max = len(obj["slices"]) * len(obj["slices"][0])
print(f'{int((count/max)*100)}% ({max})', end="\r")
for n in range(len(obj["slices"])):
pics = obj["slices"][n]
for n2 in range(len(pics)):
response = requests.get(pics[n2]["url"])
pic = Image.open(BytesIO(response.content))
img.paste(pic, (2048*n2, 2048*n))
count += 1
print(f'{int((count/max)*100)}% ({max})', end="\r")
print()
name = name.replace(" ", "_").replace(":", "")
img.save(name)
print(name)
async def main():
os.makedirs(dir, exist_ok=True)
data = []
for n in list(range(300)):
url = f'https://sg-public-api-static.hoyolab.com/common/map_user/ys_obc/v1/map/info?app_sn=ys_obc&lang={lang}&map_id={n}'
response = requests.get(url)
obj = response.json()
if obj["retcode"] == 0:
print(n, ":", obj["data"]["info"]["name"])
detail = obj["data"]["info"]["detail"]
if detail:
map_obj = json.loads(detail)
obj["data"]["info"]["detail"] = map_obj
makemap(map_obj, os.path.join(dir, f'{obj["data"]["info"]["id"]}_{obj["data"]["info"]["name"]}.png'))
else:
obj["data"]["info"]["detail"] = {}
data.append(obj["data"]["info"])
with open(f'{url.split("/")[5]}.json', "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False)
print()
else:
print(n, end="\r")
await asyncio.sleep(1)
asyncio.run(main())
print()
@c2t-r
Copy link
Author

c2t-r commented Jul 23, 2024

hoyo changed the way to load map so this script no longer works correctly.
you can use this c2t-r/teyvat_map_downloader.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment