download.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. """
  2. Manage the logic of downloading the pokedex and source images.
  3. """
  4. import re
  5. import json
  6. import asyncio
  7. from pathlib import Path
  8. from dataclasses import dataclass, asdict
  9. from collections import defaultdict
  10. from aiohttp import ClientSession
  11. JS_TO_JSON = re.compile(r"\b([a-zA-Z][a-zA-Z0-9]*?):")
  12. # the dex from showdown assumes only strawberry alcremie, since
  13. # that's what's in showdown, but we might as well add the rest
  14. ALCREMIE_SWEETS = [
  15. "Strawberry", "Berry", "Love", "Star",
  16. "Clover", "Flower", "Ribbon",
  17. ]
  18. # https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_with_gender_differences
  19. # there are some pokemon with notable gender diffs that the dex doesn't cover
  20. # judgement calls made arbitrarily
  21. GENDER_DIFFS = (
  22. "hippopotas", "hippowdon",
  23. "unfezant",
  24. "frillish", "jellicent",
  25. "pyroar",
  26. # meowstic, indeedee, basculegion, oinkologne are already handled in the dex
  27. )
  28. @dataclass
  29. class Form:
  30. name: str
  31. traits: list[str]
  32. types: list[str]
  33. color: str
  34. @dataclass
  35. class Pokemon:
  36. num: int
  37. species: str
  38. forms: list[Form]
  39. async def download_pokedex() -> dict:
  40. async with ClientSession() as session:
  41. async with session.get("https://play.pokemonshowdown.com/data/pokedex.js") as res:
  42. res.raise_for_status()
  43. text = await res.text("utf-8")
  44. # this is not json of course, but it's close
  45. # start by taking out the ; and definition
  46. cleaned = text.replace("exports.BattlePokedex = ", "").strip(";")
  47. # then convert the keys to strings
  48. converted = re.sub(JS_TO_JSON, lambda m: f'"{m.group(1)}":', cleaned)
  49. # and fix Type: Null, Farfetch'd, Sirfetch'd
  50. fixed = converted.replace(
  51. '""Type": Null"', '"Type: Null"').replace("\u2019", "'")
  52. # then, parse it
  53. return json.loads(fixed)
  54. def get_traits(species: str, kind: str) -> list[str]:
  55. traits = []
  56. if kind in ("mega", "mega-x", "mega-y", "primal"):
  57. traits.extend(("mega", "nostart"))
  58. if kind in ("gmax", "eternamax", "rapid-strike-gmax"):
  59. traits.extend(("gmax", "nostart"))
  60. if kind in ("alola", "galar", "hisui", "paldea"):
  61. traits.extend(("regional", kind))
  62. # special cases
  63. if species == "Castform" and kind != "base":
  64. # castform can't start battle in weather forms
  65. traits.append("nostart")
  66. if species == "Cherrim" and kind != "base":
  67. # castform can't start battle in sunshine form
  68. traits.append("nostart")
  69. if species == "Aegislash" and kind != "base":
  70. # aegislash can't start battle in blade form
  71. traits.append("nostart")
  72. if species == "Wishiwashi" and kind != "base":
  73. # wishiwashi can't start battle in schooling form
  74. traits.append("nostart")
  75. if species == "Mimikyu" and kind != "base":
  76. # mimikyu can't start battle in busted form
  77. traits.append("nostart")
  78. if species == "Necrozma" and kind == "ultra":
  79. # necrozma can't start battle in ultra form
  80. traits.append("nostart")
  81. if species == "Cramorant" and kind != "base":
  82. # cramorant can't start battle in gulping/gorging form
  83. traits.append("nostart")
  84. if species == "Tauros" and "paldea" in kind:
  85. # paldean tauros has dumb names
  86. traits.extend(("regional", "paldea"))
  87. if species == "Minior" and kind != "meteor":
  88. # minior can only start the battle in meteor form
  89. traits.append("nostart")
  90. if species == "Darmanitan" and "zen" in kind:
  91. # darmanitan cannot start in zen form
  92. traits.append("nostart")
  93. if "galar" in kind:
  94. # also there's a galar-zen form to handle
  95. traits.extend(("regional", "galar"))
  96. if species == "Palafin" and kind == "hero":
  97. # palafin can only start in zero form
  98. traits.append("nostart")
  99. if species == "Gimmighoul" and kind == "roaming":
  100. # gimmighoul roaming is only in PGO
  101. traits.append("nostart")
  102. return sorted(set(traits))
  103. def clean_dex(raw: dict) -> dict[int, Pokemon]:
  104. regrouped = defaultdict(list)
  105. for key, entry in raw.items():
  106. isNonstandard = entry.get("isNonstandard", None)
  107. baseSpecies = entry.get("baseSpecies", None)
  108. forme = entry.get("forme", None)
  109. if isNonstandard not in (None, "Past", "Unobtainable"):
  110. continue # remove CAP etc.
  111. if baseSpecies in ("Pikachu", "Pichu") and forme is not None:
  112. continue # remove pikachu spam + spiky ear pichu
  113. if forme is not None and "Totem" in forme:
  114. continue # remove totem pokemon
  115. if baseSpecies == "Toxtricity" and forme == "Low-Key-Gmax":
  116. continue # remove low-key-gmax since it is sort of a duplicate
  117. num = entry["num"]
  118. # non-cosmetic forms get separate entries automatically
  119. # but keeping the separate unown forms would be ridiculous
  120. if key != "unown" and len(cosmetic := entry.get("cosmeticFormes", [])) > 0:
  121. cosmetic.append(f'{entry["name"]}-{entry["baseForme"]}')
  122. if key == "alcremie":
  123. # oh god this thing
  124. cosmetic = [
  125. f"{cf}-{sweet}"
  126. for cf in cosmetic
  127. for sweet in ALCREMIE_SWEETS
  128. ]
  129. regrouped[num].extend({
  130. **entry,
  131. "forme": cf.replace(" ", "-"),
  132. "formeKind": "cosmetic",
  133. } for cf in cosmetic)
  134. elif key in GENDER_DIFFS:
  135. regrouped[num].append({
  136. **entry,
  137. "forme": f'{entry["name"]}-M',
  138. "formeKind": "cosmetic",
  139. })
  140. regrouped[num].append({
  141. **entry,
  142. "forme": f'{entry["name"]}-F',
  143. "formeKind": "cosmetic",
  144. })
  145. else:
  146. regrouped[num].append({
  147. **entry,
  148. "forme": entry["name"],
  149. "formeKind": entry.get("forme", "base").lower(),
  150. })
  151. return {
  152. i: Pokemon(
  153. num=i,
  154. species=(
  155. # doubles as an assertion that forms is not empty
  156. species := (forms := regrouped[i])[0].get("baseSpecies", forms[0]["name"])
  157. ),
  158. forms=[
  159. Form(
  160. name=f.get("forme", f["name"]),
  161. traits=get_traits(species, f["formeKind"].lower()),
  162. types=f["types"],
  163. color=f["color"],
  164. ) for f in forms
  165. ]
  166. ) for i in range(1, max(regrouped.keys()) + 1)
  167. }
  168. async def load_pokedex(dex_file: Path, force_dex: bool) -> dict:
  169. if dex_file.is_file() and not force_dex:
  170. with open(dex_file) as infile:
  171. loaded = json.load(infile)
  172. dex = {
  173. int(num): Pokemon(
  174. num=entry["num"],
  175. species=entry["species"],
  176. forms=[Form(**f) for f in entry["forms"]],
  177. ) for num, entry in loaded.items()
  178. }
  179. else:
  180. # first download the pokedex
  181. raw_dex = await download_pokedex()
  182. # clean and reorganize it
  183. dex = clean_dex(raw_dex)
  184. # output dex for auditing and reloading
  185. with open(dex_file, "w") as out:
  186. json.dump({
  187. str(i): asdict(pkmn)
  188. for i, pkmn in dex.items()
  189. }, out, indent=2)
  190. return dex
  191. SHOWDOWN_REPLACEMENTS = [
  192. ("mega-", "mega"), # charizard, mewtwo
  193. ("paldea-", "paldea"), # tauros
  194. ("mr. ", "mr"), # mr mime + mr rime
  195. (" jr.", "jr"), # mime jr
  196. ("'d", "d"), # farfetch'd and sirfetch'd
  197. ("nidoran-m", "nidoranm"),
  198. ("-f", "f"), # gender diff forms
  199. (re.compile(r"-m$"), ""), # gender diff forms
  200. (re.compile(r"^ho-oh$"), "hooh"),
  201. ("burmy-plant", "burmy"),
  202. ("shellos-west", "shellos"),
  203. ("gastrodon-west", "gastrodon"),
  204. ("hippopotasf", "hippopotas-f"),
  205. ("hippowdonf", "hippowdon-f"),
  206. ("rotomf", "rotom-f"), # rotom fan + frost special case
  207. ("porygon-z", "porygonz"),
  208. ("arceusf", "arceus-f"), # fire, flying, fairy, fighting special case
  209. ("unfezantf", "unfezant-f"),
  210. ("-striped", "striped"), # basculin special cases
  211. ("-galar-zen", "-galarzen"), # darmanitan special cases
  212. ("-spring", ""), # deerling, sawsbuck special cases
  213. ("frillishf", "frillish-f"),
  214. ("jellicentf", "jellicent-f"),
  215. ("pyroarf", "pyroar-f"),
  216. ("flabébé", "flabebe"),
  217. ("flabebe-red", "flabebe"),
  218. ("floette-red", "floette"),
  219. ("florges-red", "florges"),
  220. ("vivillonfancy", "vivillon-fancy"),
  221. ("vivillon-meadow", "vivillon"),
  222. ("vivillon-icy-snow", "vivillon-icysnow"),
  223. ("vivillon-high-plains", "vivillon-highplains"),
  224. ("meowsticf", "meowstic-f"),
  225. ("furfrou-la-reine", "furfrou-lareine"),
  226. ("furfrou-natural", "furfrou"),
  227. ("%", ""), # zygarde 10%,
  228. ("oricorio-pom-pom", "oricorio-pompom"),
  229. ("oricorio-pa'u", "oricorio-pau"),
  230. ("type: null", "typenull"),
  231. ("silvallyf", "silvally-f"), # fire, flying, fairy, fighting special case
  232. ("minior-red", "minior"),
  233. ("mo-o", "moo"), # jangmo-o, hakamo-o, kommo-o
  234. ("tapu ", "tapu"), # tapus
  235. ("dusk-mane", "duskmane"), # necrozma
  236. ("dawn-wings", "dawnwings"), # necrozma
  237. ]
  238. def get_showdown_urls(form: Form) -> list[tuple[str, str]]:
  239. name = form.name.lower()
  240. for pat, ins in SHOWDOWN_REPLACEMENTS:
  241. if isinstance(pat, re.Pattern):
  242. name = re.sub(pat, ins, name)
  243. else:
  244. name = name.replace(pat, ins)
  245. return [
  246. (f"https://play.pokemonshowdown.com/sprites/ani/{name}.gif", "gif"),
  247. (f"https://play.pokemonshowdown.com/sprites/ani-back/{name}.gif", "gif"),
  248. (f"https://play.pokemonshowdown.com/sprites/gen5/{name}.png", "png"),
  249. (f"https://play.pokemonshowdown.com/sprites/gen5-back/{name}.png", "png"),
  250. ]
  251. SEREBII_SPECIAL = {
  252. "Castform-Rainy": "r",
  253. "Castform-Snowy": "i",
  254. "Castform-Sunny": "s",
  255. "Deoxys-Attack": "a",
  256. "Deoxys-Defense": "d",
  257. "Deoxys-Speed": "s",
  258. "Burmy-Plant": "p",
  259. "Burmy-Sandy": "s",
  260. "Burmy-Trash": "t",
  261. # wormadam plant is default form
  262. "Wormadam-Sandy": "c",
  263. "Wormadam-Trash": "t",
  264. "Cherrim-Sunshine": "s",
  265. "Shellos-East": "e",
  266. "Gastrodon-East": "e",
  267. "Tauros-Paldea-Blaze": "b",
  268. "Tauros-Paldea-Aqua": "a",
  269. "Rotom-Heat": "h",
  270. "Rotom-Wash": "w",
  271. "Rotom-Frost": "f",
  272. "Rotom-Fan": "s",
  273. "Rotom-Mow": "m",
  274. "Dialga-Origin": "o",
  275. "Palkia-Origin": "o",
  276. "Giratina-Origin": "o",
  277. "Shaymin-Sky": "s",
  278. "Unfezant-F": "f",
  279. "Basculin-Blue-Striped": "b",
  280. "Basculin-White-Striped": "w",
  281. "Darmanitan-Zen": "z",
  282. "Darmanitan-Galar-Zen": "gz",
  283. "Deerling-Summer": "s",
  284. "Deerling-Autumn": "a",
  285. "Deerling-Winter": "w",
  286. "Sawsbuck-Summer": "s",
  287. "Sawsbuck-Autumn": "a",
  288. "Sawsbuck-Winter": "w",
  289. "Frillish-F": "f",
  290. "Jellicent-F": "f",
  291. "Kyurem-Black": "b",
  292. "Kyurem-White": "w",
  293. "Keldeo-Resolute": "r",
  294. "Meloetta-Pirouette": "s",
  295. "Genesect-Douse": "w",
  296. "Genesect-Shock": "e",
  297. "Genesect-Burn": "f",
  298. "Genesect-Chill": "i",
  299. "Greninja-Ash": "a",
  300. "Vivillon-Archipelago": "a",
  301. "Vivillon-Continental": "c",
  302. "Vivillon-Elegant": "e",
  303. "Vivillon-Fancy": "f",
  304. "Vivillon-Garden": "g",
  305. "Vivillon-High-Plains": "h",
  306. "Vivillon-Icy-Snow": "i",
  307. "Vivillon-Jungle": "j",
  308. "Vivillon-Marine": "ma",
  309. "Vivillon-Meadow": "m",
  310. "Vivillon-Modern": "mo",
  311. "Vivillon-Monsoon": "mon",
  312. "Vivillon-Ocean": "o",
  313. "Vivillon-Pokeball": "pb",
  314. "Vivillon-Polar": "p",
  315. "Vivillon-River": "r",
  316. "Vivillon-Sandstorm": "s",
  317. "Vivillon-Savanna": "sa",
  318. "Vivillon-Sun": "su",
  319. "Vivillon-Tundra": "t",
  320. "Pyroar-F": "f",
  321. "Flabébé-Blue": "b",
  322. "Flabébé-Orange": "o",
  323. "Flabébé-White": "w",
  324. "Flabébé-Yellow": "y",
  325. "Flabébé-Red": "r",
  326. "Floette-Blue": "b",
  327. "Floette-Orange": "o",
  328. "Floette-White": "w",
  329. "Floette-Yellow": "y",
  330. "Floette-Red": "r",
  331. "Florges-Blue": "b",
  332. "Florges-Orange": "o",
  333. "Florges-White": "w",
  334. "Florges-Yellow": "y",
  335. "Florges-Red": "r",
  336. "Meowstic-F": "f",
  337. "Furfrou-Dandy": "da",
  338. "Furfrou-Debutante": "de",
  339. "Furfrou-Diamond": "d",
  340. "Furfrou-Heart": "h",
  341. "Furfrou-Kabuki": "k",
  342. "Furfrou-La-Reine": "l",
  343. "Furfrou-Matron": "m",
  344. "Furfrou-Pharaoh": "p",
  345. "Furfrou-Star": "s",
  346. "Aegislash-Blade": "b",
  347. "Zygarde-10%": "10",
  348. "Zygarde-Complete": "c",
  349. "Hoopa-Unbound": "u",
  350. "Pumpkaboo-Small": "s", # kinda dumb but w/e
  351. "Pumpkaboo-Large": "l",
  352. "Pumpkaboo-Super": "h",
  353. "Gourgeist-Small": "s",
  354. "Gourgeist-Large": "l",
  355. "Gourgeist-Super": "h",
  356. "Oricorio-Pom-Pom": "p",
  357. "Oricorio-Pa'u": "pau",
  358. "Oricorio-Sensu": "s",
  359. "Lycanroc-Midnight": "m",
  360. "Lycanroc-Dusk": "d",
  361. "Wishiwashi-School": "s",
  362. "Minior-Blue": "b",
  363. "Minior-Green": "g",
  364. "Minior-Indigo": "i",
  365. "Minior-Orange": "o",
  366. "Minior-Red": "r",
  367. "Minior-Violet": "v",
  368. "Minior-Yellow": "y",
  369. "Mimikyu-Busted": "b",
  370. "Necrozma-Dusk-Mane": "dm",
  371. "Necrozma-Dawn-Wings": "dw",
  372. "Necrozma-Ultra": "m",
  373. "Magearna-Original": "o",
  374. }
  375. SEREBII_USE_SPECIES = (
  376. "Shellos-West", "Gastrodon-West",
  377. "Hippopotas-M", "Hippowdon-M",
  378. "Unfezant-M",
  379. "Deerling-Spring", "Sawsbuck-Spring",
  380. "Frillish-M", "Jellicent-M",
  381. "Pyroar-M",
  382. "Furfrou-Natural",
  383. "Minior-Meteor",
  384. )
  385. SEREBII_IGNORE_MISSING = ("Hippopotas-F", "Hippowdon-F", "Floette-Eternal")
  386. def get_serebii_url(pkmn: Pokemon, form: Form) -> str | None:
  387. if form.name == pkmn.species or form.name in SEREBII_USE_SPECIES:
  388. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}.png"
  389. if form.name in SEREBII_SPECIAL:
  390. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-{SEREBII_SPECIAL[form.name]}.png"
  391. if "gmax" in form.traits:
  392. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-gi.png"
  393. if "mega" in form.traits:
  394. if "Mega-X" in form.name:
  395. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-mx.png"
  396. elif "Mega-Y" in form.name:
  397. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-my.png"
  398. else:
  399. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-m.png"
  400. if "alola" in form.traits:
  401. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-a.png"
  402. if "galar" in form.traits:
  403. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-g.png"
  404. if "hisui" in form.traits:
  405. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-h.png"
  406. if "paldea" in form.traits:
  407. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-p.png"
  408. if pkmn.num in (493, 773): # arceus and silvally
  409. type_name = form.name.split('-')[1].lower()
  410. return f"https://www.serebii.net/pokemon/art/{pkmn.num}-{type_name}.png"
  411. if "-Therian" in form.name:
  412. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-s.png"
  413. if form.name not in SEREBII_IGNORE_MISSING:
  414. print(f"No Serebii URL known for {form.name}")
  415. async def download(session: ClientSession, url: str, filename: Path) -> tuple[str, Exception | bool]:
  416. if filename.is_file():
  417. return url, False
  418. try:
  419. async with session.get(url) as res:
  420. res.raise_for_status()
  421. with open(filename, "wb") as out:
  422. out.write(await res.read())
  423. except Exception as ex:
  424. return url, ex
  425. return url, True
  426. async def download_all_for_pokemon(pkmn: Pokemon, image_dir: Path) -> dict[str, dict[str, Exception | bool]]:
  427. results = defaultdict(dict)
  428. async with ClientSession() as session:
  429. for form in pkmn.forms:
  430. urls = []
  431. urls += get_showdown_urls(form)
  432. urls.append((get_serebii_url(pkmn, form), "png"))
  433. # TODO more sources
  434. results[form.name].update(await asyncio.gather(*[
  435. download(session, url, image_dir.joinpath(f"{form.name}-{i}.{ext}"))
  436. for i, (url, ext) in enumerate(urls) if url is not None
  437. ]))
  438. return results
  439. async def download_all(image_dir: Path, pkmn: list[Pokemon]) -> dict[str, dict[str, Exception | bool]]:
  440. image_dir.mkdir(parents=True, exist_ok=True)
  441. log = {}
  442. for p in pkmn:
  443. log.update(await download_all_for_pokemon(p, image_dir))
  444. return log
  445. KNOWN_MISSING = [
  446. "https://play.pokemonshowdown.com/sprites/ani/venusaur-gmax.gif",
  447. "https://play.pokemonshowdown.com/sprites/ani-back/venusaur-gmax.gif",
  448. "https://play.pokemonshowdown.com/sprites/ani/blastoise-gmax.gif",
  449. "https://play.pokemonshowdown.com/sprites/ani-back/blastoise-gmax.gif",
  450. "https://play.pokemonshowdown.com/sprites/ani/growlithe-hisui.gif",
  451. "https://play.pokemonshowdown.com/sprites/ani-back/growlithe-hisui.gif",
  452. "https://play.pokemonshowdown.com/sprites/ani/arcanine-hisui.gif",
  453. "https://play.pokemonshowdown.com/sprites/ani-back/arcanine-hisui.gif",
  454. "https://play.pokemonshowdown.com/sprites/ani/voltorb-hisui.gif",
  455. "https://play.pokemonshowdown.com/sprites/ani-back/voltorb-hisui.gif",
  456. "https://play.pokemonshowdown.com/sprites/ani/electrode-hisui.gif",
  457. "https://play.pokemonshowdown.com/sprites/ani-back/electrode-hisui.gif",
  458. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeacombat.gif",
  459. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeacombat.gif",
  460. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeablaze.gif",
  461. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeablaze.gif",
  462. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeaaqua.gif",
  463. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeaaqua.gif",
  464. "https://play.pokemonshowdown.com/sprites/ani/wooper-paldea.gif",
  465. "https://play.pokemonshowdown.com/sprites/ani-back/wooper-paldea.gif",
  466. "https://play.pokemonshowdown.com/sprites/ani/qwilfish-hisui.gif",
  467. "https://play.pokemonshowdown.com/sprites/ani-back/qwilfish-hisui.gif",
  468. "https://play.pokemonshowdown.com/sprites/ani/sneasel-hisui.gif",
  469. "https://play.pokemonshowdown.com/sprites/ani-back/sneasel-hisui.gif",
  470. "https://play.pokemonshowdown.com/sprites/ani/dialga-origin.gif",
  471. "https://play.pokemonshowdown.com/sprites/ani-back/dialga-origin.gif",
  472. "https://play.pokemonshowdown.com/sprites/ani/palkia-origin.gif",
  473. "https://play.pokemonshowdown.com/sprites/ani-back/palkia-origin.gif",
  474. "https://play.pokemonshowdown.com/sprites/ani/lilligant-hisui.gif",
  475. "https://play.pokemonshowdown.com/sprites/ani-back/lilligant-hisui.gif",
  476. "https://play.pokemonshowdown.com/sprites/ani/basculin-whitestriped.gif",
  477. "https://play.pokemonshowdown.com/sprites/ani-back/basculin-whitestriped.gif",
  478. "https://play.pokemonshowdown.com/sprites/ani/braviary-hisui.gif",
  479. "https://play.pokemonshowdown.com/sprites/ani-back/braviary-hisui.gif",
  480. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-archipelago.png",
  481. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-archipelago.png",
  482. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-continental.png",
  483. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-continental.png",
  484. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-elegant.png",
  485. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-elegant.png",
  486. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-garden.png",
  487. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-garden.png",
  488. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-highplains.png",
  489. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-highplains.png",
  490. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-icysnow.png",
  491. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-icysnow.png",
  492. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-jungle.png",
  493. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-jungle.png",
  494. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-marine.png",
  495. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-marine.png",
  496. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-modern.png",
  497. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-modern.png",
  498. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-monsoon.png",
  499. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-monsoon.png",
  500. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-ocean.png",
  501. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-ocean.png",
  502. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-polar.png",
  503. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-polar.png",
  504. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-river.png",
  505. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-river.png",
  506. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-sandstorm.png",
  507. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-sandstorm.png",
  508. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-savanna.png",
  509. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-savanna.png",
  510. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-sun.png",
  511. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-sun.png",
  512. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-tundra.png",
  513. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-tundra.png",
  514. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-dandy.png",
  515. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-dandy.png",
  516. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-debutante.png",
  517. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-debutante.png",
  518. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-diamond.png",
  519. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-diamond.png",
  520. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-heart.png",
  521. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-heart.png",
  522. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-kabuki.png",
  523. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-kabuki.png",
  524. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-lareine.png",
  525. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-lareine.png",
  526. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-matron.png",
  527. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-matron.png",
  528. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-pharaoh.png",
  529. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-pharaoh.png",
  530. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-star.png",
  531. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-star.png",
  532. "https://play.pokemonshowdown.com/sprites/ani/sliggoo-hisui.gif",
  533. "https://play.pokemonshowdown.com/sprites/ani-back/sliggoo-hisui.gif",
  534. "https://play.pokemonshowdown.com/sprites/ani/goodra-hisui.gif",
  535. "https://play.pokemonshowdown.com/sprites/ani-back/goodra-hisui.gif",
  536. "https://play.pokemonshowdown.com/sprites/ani/avalugg-hisui.gif",
  537. "https://play.pokemonshowdown.com/sprites/ani-back/avalugg-hisui.gif",
  538. ]
  539. async def main(
  540. dex_file: Path, image_dir: Path, startIndex: int, endIndex: int,
  541. log_skipped: bool, force_dex: bool, dex_only: bool
  542. ):
  543. dex = await load_pokedex(dex_file, force_dex)
  544. if dex_only:
  545. return
  546. log = await download_all(image_dir, (dex[i] for i in range(startIndex, endIndex + 1)))
  547. new_downloads = 0
  548. for form, result in log.items():
  549. for url, info in result.items():
  550. if isinstance(info, Exception):
  551. if url not in KNOWN_MISSING:
  552. print(f"{form}: FAILED {url} - {info}")
  553. elif not info:
  554. if log_skipped:
  555. print(f"{form}: SKIPPED {url} - {info}")
  556. else:
  557. print(f"{form}: SUCCESS {url}")
  558. new_downloads += 1
  559. print(f"New Downloads: {new_downloads}")
  560. if __name__ == "__main__":
  561. from argparse import ArgumentParser
  562. parser = ArgumentParser(
  563. prog="Image Retriever",
  564. description="Retrieve pokedex and images",
  565. )
  566. parser.add_argument(
  567. "-d", "--pokedex", default="data/pokedex.json", type=Path, help="Pokedex file"
  568. )
  569. parser.add_argument(
  570. "--refresh-dex", action="store_true", help="Update the pokedex"
  571. )
  572. parser.add_argument(
  573. "--pokedex-only", action="store_true", help="Quit before image download"
  574. )
  575. parser.add_argument(
  576. "-o", "--output", default="images", type=Path, help="Image output directory"
  577. )
  578. parser.add_argument(
  579. "--log-skipped", action="store_true", help="Log skipped images"
  580. )
  581. parser.add_argument(
  582. "bounds", type=lambda a: map(int, a.split("-")), default="1-151", nargs="?",
  583. help="Range of dex numbers to download, inclusive"
  584. )
  585. args = parser.parse_args()
  586. start, end = args.bounds
  587. asyncio.run(main(
  588. args.pokedex, args.output, start, end,
  589. args.log_skipped, args.refresh_dex, args.pokedex_only
  590. ))