Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created May 16, 2023 16:22
Show Gist options
  • Save scturtle/ea0ef8351212a8c752151adcf430342c to your computer and use it in GitHub Desktop.
Save scturtle/ea0ef8351212a8c752151adcf430342c to your computer and use it in GitHub Desktop.
emuiibo data generator
import json
import time
import random
import struct
import urllib.request
from pathlib import Path
# https://www.amiiboapi.com/api/amiibo/
database = json.load(open("amiibos.json"))
for amiibo in database["amiibo"]:
if amiibo["gameSeries"] != "The Legend of Zelda":
continue
year, month, day = time.gmtime()[:3]
amiiboId = amiibo["head"] + amiibo["tail"]
assert len(amiiboId) == 16
def swap16(i):
return struct.unpack("<H", struct.pack(">H", i))[0]
amiiboData = {
"name": amiibo["name"],
"write_counter": 0,
"version": 0,
"first_write_date": {
"y": year,
"m": month,
"d": day,
},
"last_write_date": {
"y": year,
"m": month,
"d": day,
},
"mii_charinfo_file": "mii-charinfo.bin",
"id": {
"game_character_id": swap16(int(amiiboId[:4], 16)),
"character_variant": int(amiiboId[4:6], 16),
"figure_type": int(amiiboId[6:8], 16),
"model_number": int(amiiboId[8:12], 16),
"series": int(amiiboId[12:14], 16),
},
"uuid": [random.randint(0, 255) for _ in range(7)] + [0, 0, 0],
"use_random_uuid": True,
}
def sanitize(c):
if c in "!?.,'\\":
return ""
if not (0 <= ord(c) < 128):
return ""
if c in " /":
return "_"
return c
amiiboSeries = "".join(map(sanitize, amiibo["amiiboSeries"]))
amiiboName = "".join(map(sanitize, amiibo["name"]))
path = Path("amiibo") / amiiboName
if path.exists():
path = Path("amiibo") / (amiiboName + "_" + amiiboId)
if path.exists():
print(path, "exists")
exit(1)
path.mkdir(parents=True)
(path / "amiibo.flag").touch()
(path / "amiibo.json").write_text(json.dumps(amiiboData, indent=2))
while True:
try:
urllib.request.urlretrieve(amiibo["image"], path / "amiibo.png")
break
except KeyboardInterrupt:
exit(1)
except:
print("retry", amiibo["image"])
continue
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment