download.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 == "Tauros" and "paldea" in kind:
  73. # paldean tauros has dumb names
  74. traits.extend(("regional", "paldea"))
  75. if species == "Minior" and kind != "meteor":
  76. # minior can only start the battle in meteor form
  77. traits.append("nostart")
  78. if species == "Darmanitan" and "zen" in kind:
  79. # darmanitan cannot start in zen form
  80. traits.append("nostart")
  81. if "galar" in kind:
  82. # also there's a galar-zen form to handle
  83. traits.extend(("regional", "galar"))
  84. if species == "Palafin" and kind == "hero":
  85. # palafin can only start in zero form
  86. traits.append("nostart")
  87. if species == "Gimmighoul" and kind == "roaming":
  88. # gimmighoul roaming is only in PGO
  89. traits.append("nostart")
  90. return sorted(set(traits))
  91. def clean_dex(raw: dict) -> dict[int, Pokemon]:
  92. regrouped = defaultdict(list)
  93. for key, entry in raw.items():
  94. isNonstandard = entry.get("isNonstandard", None)
  95. baseSpecies = entry.get("baseSpecies", None)
  96. forme = entry.get("forme", None)
  97. if isNonstandard not in (None, "Past", "Unobtainable"):
  98. continue # remove CAP etc.
  99. if baseSpecies in ("Pikachu", "Pichu") and forme is not None:
  100. continue # remove pikachu spam + spiky ear pichu
  101. if forme is not None and "Totem" in forme:
  102. continue # remove totem pokemon
  103. num = entry["num"]
  104. # non-cosmetic forms get separate entries automatically
  105. # but keeping the separate unown forms would be ridiculous
  106. if key != "unown" and len(cosmetic := entry.get("cosmeticFormes", [])) > 0:
  107. cosmetic.append(f'{entry["name"]}-{entry["baseForme"]}')
  108. if key == "alcremie":
  109. # oh god this thing
  110. cosmetic = [
  111. f"{cf}-{sweet}"
  112. for cf in cosmetic
  113. for sweet in ALCREMIE_SWEETS
  114. ]
  115. regrouped[num].extend({
  116. **entry,
  117. "forme": cf.replace(" ", "-"),
  118. "formeKind": "cosmetic",
  119. } for cf in cosmetic)
  120. elif key in GENDER_DIFFS:
  121. regrouped[num].append({
  122. **entry,
  123. "forme": f'{entry["name"]}-M',
  124. "formeKind": "cosmetic",
  125. })
  126. regrouped[num].append({
  127. **entry,
  128. "forme": f'{entry["name"]}-F',
  129. "formeKind": "cosmetic",
  130. })
  131. else:
  132. regrouped[num].append({
  133. **entry,
  134. "forme": entry["name"],
  135. "formeKind": entry.get("forme", "base").lower(),
  136. })
  137. return {
  138. i: Pokemon(
  139. num=i,
  140. species=(
  141. # doubles as an assertion that forms is not empty
  142. species := (forms := regrouped[i])[0].get("baseSpecies", forms[0]["name"])
  143. ),
  144. forms=[
  145. Form(
  146. name=f.get("forme", f["name"]),
  147. traits=get_traits(species, f["formeKind"].lower()),
  148. types=f["types"],
  149. color=f["color"],
  150. ) for f in forms
  151. ]
  152. ) for i in range(1, max(regrouped.keys()) + 1)
  153. }
  154. async def load_pokedex(dex_file: Path, force_dex: bool) -> dict:
  155. if dex_file.is_file() and not force_dex:
  156. with open(dex_file) as infile:
  157. loaded = json.load(infile)
  158. dex = {
  159. int(num): Pokemon(
  160. num=entry["num"],
  161. species=entry["species"],
  162. forms=[Form(**f) for f in entry["forms"]],
  163. ) for num, entry in loaded.items()
  164. }
  165. else:
  166. # first download the pokedex
  167. raw_dex = await download_pokedex()
  168. # clean and reorganize it
  169. dex = clean_dex(raw_dex)
  170. # output dex for auditing and reloading
  171. with open(dex_file, "w") as out:
  172. json.dump({
  173. str(i): asdict(pkmn)
  174. for i, pkmn in dex.items()
  175. }, out, indent=2)
  176. return dex
  177. SHOWDOWN_REPLACEMENTS = [
  178. ("mega-", "mega"), # charizard, mewtwo
  179. ("paldea-", "paldea"), # tauros
  180. ("mr. ", "mr"), # mr mime + mr rime
  181. (" jr.", "jr"), # mime jr
  182. ("'d", "d"), # farfetch'd and sirfetch'd
  183. ("nidoran-m", "nidoranm"),
  184. ("-f", "f"), # gender diff forms
  185. (re.compile(r"-m$"), ""), # gender diff forms
  186. (re.compile(r"^ho-oh$"), "hooh"),
  187. ("burmy-plant", "burmy"),
  188. ("shellos-west", "shellos"),
  189. ("gastrodon-west", "gastrodon"),
  190. ("hippopotasf", "hippopotas-f"),
  191. ("hippowdonf", "hippowdon-f"),
  192. ("rotomf", "rotom-f"), # rotom fan + frost special case
  193. ("porygon-z", "porygonz"),
  194. ("arceusf", "arceus-f"), # arceus fire, flying, fairy, fighting special case
  195. ("unfezantf", "unfezant-f"),
  196. ("-striped", "striped"), # basculin special cases
  197. ("-galar-zen", "-galarzen"), # darmanitan special cases
  198. ("-spring", ""), # deerling, sawsbuck special cases
  199. ("frillishf", "frillish-f"),
  200. ("jellicentf", "jellicent-f"),
  201. ("pyroarf", "pyroar-f"),
  202. ("flabébé", "flabebe"),
  203. ("flabebe-red", "flabebe"),
  204. ("floette-red", "floette"),
  205. ("florges-red", "florges"),
  206. ("vivillonfancy", "vivillon-fancy"),
  207. ("vivillon-meadow", "vivillon"),
  208. ("vivillon-icy-snow", "vivillon-icysnow"),
  209. ("vivillon-high-plains", "vivillon-highplains"),
  210. ("meowsticf", "meowstic-f"),
  211. ("furfrou-la-reine", "furfrou-lareine"),
  212. ("furfrou-natural", "furfrou"),
  213. ("%", ""), # zygarde 10%
  214. ]
  215. def get_showdown_urls(form: Form) -> list[tuple[str, str]]:
  216. name = form.name.lower()
  217. for pat, ins in SHOWDOWN_REPLACEMENTS:
  218. if isinstance(pat, re.Pattern):
  219. name = re.sub(pat, ins, name)
  220. else:
  221. name = name.replace(pat, ins)
  222. return [
  223. (f"https://play.pokemonshowdown.com/sprites/ani/{name}.gif", "gif"),
  224. (f"https://play.pokemonshowdown.com/sprites/ani-back/{name}.gif", "gif"),
  225. (f"https://play.pokemonshowdown.com/sprites/gen5/{name}.png", "png"),
  226. (f"https://play.pokemonshowdown.com/sprites/gen5-back/{name}.png", "png"),
  227. ]
  228. SEREBII_SPECIAL = {
  229. "Castform-Rainy": "r",
  230. "Castform-Snowy": "i",
  231. "Castform-Sunny": "s",
  232. "Deoxys-Attack": "a",
  233. "Deoxys-Defense": "d",
  234. "Deoxys-Speed": "s",
  235. "Burmy-Plant": "p",
  236. "Burmy-Sandy": "s",
  237. "Burmy-Trash": "t",
  238. # wormadam plant is default form
  239. "Wormadam-Sandy": "c",
  240. "Wormadam-Trash": "t",
  241. "Cherrim-Sunshine": "s",
  242. "Shellos-East": "e",
  243. "Gastrodon-East": "e",
  244. "Tauros-Paldea-Blaze": "b",
  245. "Tauros-Paldea-Aqua": "a",
  246. "Rotom-Heat": "h",
  247. "Rotom-Wash": "w",
  248. "Rotom-Frost": "f",
  249. "Rotom-Fan": "s",
  250. "Rotom-Mow": "m",
  251. "Dialga-Origin": "o",
  252. "Palkia-Origin": "o",
  253. "Giratina-Origin": "o",
  254. "Shaymin-Sky": "s",
  255. "Unfezant-F": "f",
  256. "Basculin-Blue-Striped": "b",
  257. "Basculin-White-Striped": "w",
  258. "Darmanitan-Zen": "z",
  259. "Darmanitan-Galar-Zen": "gz",
  260. "Deerling-Summer": "s",
  261. "Deerling-Autumn": "a",
  262. "Deerling-Winter": "w",
  263. "Sawsbuck-Summer": "s",
  264. "Sawsbuck-Autumn": "a",
  265. "Sawsbuck-Winter": "w",
  266. "Frillish-F": "f",
  267. "Jellicent-F": "f",
  268. "Kyurem-Black": "b",
  269. "Kyurem-White": "w",
  270. "Keldeo-Resolute": "r",
  271. "Meloetta-Pirouette": "s",
  272. "Genesect-Douse": "w",
  273. "Genesect-Shock": "e",
  274. "Genesect-Burn": "f",
  275. "Genesect-Chill": "i",
  276. "Greninja-Ash": "a",
  277. "Vivillon-Archipelago": "a",
  278. "Vivillon-Continental": "c",
  279. "Vivillon-Elegant": "e",
  280. "Vivillon-Fancy": "f",
  281. "Vivillon-Garden": "g",
  282. "Vivillon-High-Plains": "h",
  283. "Vivillon-Icy-Snow": "i",
  284. "Vivillon-Jungle": "j",
  285. "Vivillon-Marine": "ma",
  286. "Vivillon-Meadow": "m",
  287. "Vivillon-Modern": "mo",
  288. "Vivillon-Monsoon": "mon",
  289. "Vivillon-Ocean": "o",
  290. "Vivillon-Pokeball": "pb",
  291. "Vivillon-Polar": "p",
  292. "Vivillon-River": "r",
  293. "Vivillon-Sandstorm": "s",
  294. "Vivillon-Savanna": "sa",
  295. "Vivillon-Sun": "su",
  296. "Vivillon-Tundra": "t",
  297. "Pyroar-F": "f",
  298. "Flabébé-Blue": "b",
  299. "Flabébé-Orange": "o",
  300. "Flabébé-White": "w",
  301. "Flabébé-Yellow": "y",
  302. "Flabébé-Red": "r",
  303. "Floette-Blue": "b",
  304. "Floette-Orange": "o",
  305. "Floette-White": "w",
  306. "Floette-Yellow": "y",
  307. "Floette-Red": "r",
  308. "Florges-Blue": "b",
  309. "Florges-Orange": "o",
  310. "Florges-White": "w",
  311. "Florges-Yellow": "y",
  312. "Florges-Red": "r",
  313. "Meowstic-F": "f",
  314. "Furfrou-Dandy": "da",
  315. "Furfrou-Debutante": "de",
  316. "Furfrou-Diamond": "d",
  317. "Furfrou-Heart": "h",
  318. "Furfrou-Kabuki": "k",
  319. "Furfrou-La-Reine": "l",
  320. "Furfrou-Matron": "m",
  321. "Furfrou-Pharaoh": "p",
  322. "Furfrou-Star": "s",
  323. "Aegislash-Blade": "b",
  324. "Zygarde-10%": "10",
  325. "Zygarde-Complete": "c",
  326. "Hoopa-Unbound": "u",
  327. "Pumpkaboo-Small": "s", # kinda dumb but w/e
  328. "Pumpkaboo-Large": "l",
  329. "Pumpkaboo-Super": "h",
  330. "Gourgeist-Small": "s",
  331. "Gourgeist-Large": "l",
  332. "Gourgeist-Super": "h",
  333. }
  334. SEREBII_USE_SPECIES = (
  335. "Shellos-West", "Gastrodon-West",
  336. "Hippopotas-M", "Hippowdon-M",
  337. "Unfezant-M",
  338. "Deerling-Spring", "Sawsbuck-Spring",
  339. "Frillish-M", "Jellicent-M",
  340. "Pyroar-M",
  341. "Furfrou-Natural",
  342. )
  343. SEREBII_IGNORE_MISSING = ("Hippopotas-F", "Hippowdon-F", "Floette-Eternal")
  344. def get_serebii_url(pkmn: Pokemon, form: Form) -> str | None:
  345. if form.name == pkmn.species or form.name in SEREBII_USE_SPECIES:
  346. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}.png"
  347. if form.name in SEREBII_SPECIAL:
  348. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-{SEREBII_SPECIAL[form.name]}.png"
  349. if "gmax" in form.traits:
  350. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-gi.png"
  351. if "mega" in form.traits:
  352. if "Mega-X" in form.name:
  353. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-mx.png"
  354. elif "Mega-Y" in form.name:
  355. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-my.png"
  356. else:
  357. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-m.png"
  358. if "alola" in form.traits:
  359. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-a.png"
  360. if "galar" in form.traits:
  361. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-g.png"
  362. if "hisui" in form.traits:
  363. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-h.png"
  364. if "paldea" in form.traits:
  365. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-p.png"
  366. if pkmn.num == 493: # arceus
  367. return f"https://www.serebii.net/pokemon/art/493-{form.name.split('-')[1].lower()}.png"
  368. if "-Therian" in form.name:
  369. return f"https://www.serebii.net/pokemon/art/{pkmn.num:03d}-s.png"
  370. if form.name not in SEREBII_IGNORE_MISSING:
  371. print(f"No Serebii URL known for {form.name}")
  372. async def download(session: ClientSession, url: str, filename: Path) -> tuple[str, Exception | bool]:
  373. if filename.is_file():
  374. return url, False
  375. try:
  376. async with session.get(url) as res:
  377. res.raise_for_status()
  378. with open(filename, "wb") as out:
  379. out.write(await res.read())
  380. except Exception as ex:
  381. return url, ex
  382. return url, True
  383. async def download_all_for_pokemon(pkmn: Pokemon, image_dir: Path) -> dict[str, dict[str, Exception | bool]]:
  384. results = defaultdict(dict)
  385. async with ClientSession() as session:
  386. for form in pkmn.forms:
  387. urls = []
  388. urls += get_showdown_urls(form)
  389. urls.append((get_serebii_url(pkmn, form), "png"))
  390. # TODO more sources
  391. results[form.name].update(await asyncio.gather(*[
  392. download(session, url, image_dir.joinpath(f"{form.name}-{i}.{ext}"))
  393. for i, (url, ext) in enumerate(urls) if url is not None
  394. ]))
  395. return results
  396. async def download_all(image_dir: Path, pkmn: list[Pokemon]) -> dict[str, dict[str, Exception | bool]]:
  397. image_dir.mkdir(parents=True, exist_ok=True)
  398. log = {}
  399. for p in pkmn:
  400. log.update(await download_all_for_pokemon(p, image_dir))
  401. return log
  402. KNOWN_MISSING = [
  403. "https://play.pokemonshowdown.com/sprites/ani/venusaur-gmax.gif",
  404. "https://play.pokemonshowdown.com/sprites/ani-back/venusaur-gmax.gif",
  405. "https://play.pokemonshowdown.com/sprites/ani/blastoise-gmax.gif",
  406. "https://play.pokemonshowdown.com/sprites/ani-back/blastoise-gmax.gif",
  407. "https://play.pokemonshowdown.com/sprites/ani/growlithe-hisui.gif",
  408. "https://play.pokemonshowdown.com/sprites/ani-back/growlithe-hisui.gif",
  409. "https://play.pokemonshowdown.com/sprites/ani/arcanine-hisui.gif",
  410. "https://play.pokemonshowdown.com/sprites/ani-back/arcanine-hisui.gif",
  411. "https://play.pokemonshowdown.com/sprites/ani/voltorb-hisui.gif",
  412. "https://play.pokemonshowdown.com/sprites/ani-back/voltorb-hisui.gif",
  413. "https://play.pokemonshowdown.com/sprites/ani/electrode-hisui.gif",
  414. "https://play.pokemonshowdown.com/sprites/ani-back/electrode-hisui.gif",
  415. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeacombat.gif",
  416. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeacombat.gif",
  417. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeablaze.gif",
  418. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeablaze.gif",
  419. "https://play.pokemonshowdown.com/sprites/ani/tauros-paldeaaqua.gif",
  420. "https://play.pokemonshowdown.com/sprites/ani-back/tauros-paldeaaqua.gif",
  421. "https://play.pokemonshowdown.com/sprites/ani/wooper-paldea.gif",
  422. "https://play.pokemonshowdown.com/sprites/ani-back/wooper-paldea.gif",
  423. "https://play.pokemonshowdown.com/sprites/ani/qwilfish-hisui.gif",
  424. "https://play.pokemonshowdown.com/sprites/ani-back/qwilfish-hisui.gif",
  425. "https://play.pokemonshowdown.com/sprites/ani/sneasel-hisui.gif",
  426. "https://play.pokemonshowdown.com/sprites/ani-back/sneasel-hisui.gif",
  427. "https://play.pokemonshowdown.com/sprites/ani/dialga-origin.gif",
  428. "https://play.pokemonshowdown.com/sprites/ani-back/dialga-origin.gif",
  429. "https://play.pokemonshowdown.com/sprites/ani/palkia-origin.gif",
  430. "https://play.pokemonshowdown.com/sprites/ani-back/palkia-origin.gif",
  431. "https://play.pokemonshowdown.com/sprites/ani/lilligant-hisui.gif",
  432. "https://play.pokemonshowdown.com/sprites/ani-back/lilligant-hisui.gif",
  433. "https://play.pokemonshowdown.com/sprites/ani/basculin-whitestriped.gif",
  434. "https://play.pokemonshowdown.com/sprites/ani-back/basculin-whitestriped.gif",
  435. "https://play.pokemonshowdown.com/sprites/ani/braviary-hisui.gif",
  436. "https://play.pokemonshowdown.com/sprites/ani-back/braviary-hisui.gif",
  437. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-archipelago.png",
  438. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-archipelago.png",
  439. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-continental.png",
  440. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-continental.png",
  441. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-elegant.png",
  442. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-elegant.png",
  443. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-garden.png",
  444. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-garden.png",
  445. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-highplains.png",
  446. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-highplains.png",
  447. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-icysnow.png",
  448. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-icysnow.png",
  449. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-jungle.png",
  450. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-jungle.png",
  451. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-marine.png",
  452. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-marine.png",
  453. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-modern.png",
  454. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-modern.png",
  455. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-monsoon.png",
  456. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-monsoon.png",
  457. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-ocean.png",
  458. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-ocean.png",
  459. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-polar.png",
  460. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-polar.png",
  461. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-river.png",
  462. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-river.png",
  463. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-sandstorm.png",
  464. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-sandstorm.png",
  465. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-savanna.png",
  466. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-savanna.png",
  467. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-sun.png",
  468. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-sun.png",
  469. "https://play.pokemonshowdown.com/sprites/gen5/vivillon-tundra.png",
  470. "https://play.pokemonshowdown.com/sprites/gen5-back/vivillon-tundra.png",
  471. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-dandy.png",
  472. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-dandy.png",
  473. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-debutante.png",
  474. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-debutante.png",
  475. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-diamond.png",
  476. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-diamond.png",
  477. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-heart.png",
  478. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-heart.png",
  479. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-kabuki.png",
  480. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-kabuki.png",
  481. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-lareine.png",
  482. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-lareine.png",
  483. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-matron.png",
  484. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-matron.png",
  485. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-pharaoh.png",
  486. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-pharaoh.png",
  487. "https://play.pokemonshowdown.com/sprites/gen5/furfrou-star.png",
  488. "https://play.pokemonshowdown.com/sprites/gen5-back/furfrou-star.png",
  489. "https://play.pokemonshowdown.com/sprites/ani/sliggoo-hisui.gif",
  490. "https://play.pokemonshowdown.com/sprites/ani-back/sliggoo-hisui.gif",
  491. "https://play.pokemonshowdown.com/sprites/ani/goodra-hisui.gif",
  492. "https://play.pokemonshowdown.com/sprites/ani-back/goodra-hisui.gif",
  493. "https://play.pokemonshowdown.com/sprites/ani/avalugg-hisui.gif",
  494. "https://play.pokemonshowdown.com/sprites/ani-back/avalugg-hisui.gif",
  495. ]
  496. async def main(
  497. dex_file: Path, image_dir: Path, startIndex: int, endIndex: int,
  498. log_skipped: bool, force_dex: bool, dex_only: bool
  499. ):
  500. dex = await load_pokedex(dex_file, force_dex)
  501. if dex_only:
  502. return
  503. log = await download_all(image_dir, (dex[i] for i in range(startIndex, endIndex + 1)))
  504. new_downloads = 0
  505. for form, result in log.items():
  506. for url, info in result.items():
  507. if isinstance(info, Exception):
  508. if url not in KNOWN_MISSING:
  509. print(f"{form}: FAILED {url} - {info}")
  510. elif not info:
  511. if log_skipped:
  512. print(f"{form}: SKIPPED {url} - {info}")
  513. else:
  514. print(f"{form}: SUCCESS {url}")
  515. new_downloads += 1
  516. print(f"New Downloads: {new_downloads}")
  517. if __name__ == "__main__":
  518. from argparse import ArgumentParser
  519. parser = ArgumentParser(
  520. prog="Image Retriever",
  521. description="Retrieve pokedex and images",
  522. )
  523. parser.add_argument(
  524. "-d", "--pokedex", default="data/pokedex.json", type=Path, help="Pokedex file"
  525. )
  526. parser.add_argument(
  527. "--refresh-dex", action="store_true", help="Update the pokedex"
  528. )
  529. parser.add_argument(
  530. "--pokedex-only", action="store_true", help="Quit before image download"
  531. )
  532. parser.add_argument(
  533. "-o", "--output", default="images", type=Path, help="Image output directory"
  534. )
  535. parser.add_argument(
  536. "--log-skipped", action="store_true", help="Log skipped images"
  537. )
  538. parser.add_argument(
  539. "bounds", type=lambda a: map(int, a.split("-")), default="1-151", nargs="?",
  540. help="Range of dex numbers to download, inclusive"
  541. )
  542. args = parser.parse_args()
  543. start, end = args.bounds
  544. asyncio.run(main(
  545. args.pokedex, args.output, start, end,
  546. args.log_skipped, args.refresh_dex, args.pokedex_only
  547. ))