Skip to content

Instantly share code, notes, and snippets.

@Keyacom
Last active May 29, 2023 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keyacom/9e972abdba517ac7443f236c54a4c2a7 to your computer and use it in GitHub Desktop.
Save Keyacom/9e972abdba517ac7443f236c54a4c2a7 to your computer and use it in GitHub Desktop.
Downloader for new-style Pokémon HOME icons
from sys import argv, exit
from ast import literal_eval
from pathlib import Path
from time import time
import requests
USAGE = 'USAGE: `python download.py [dirname="."]`'
class HTTP403Forbidden(Exception): pass
def icon_fn(dn: int, fn: int = 0):
return f"icon{dn:0>4}_f{fn:0>2}_s0.png"
def icon_url(dn: int, fn: int = 0):
return f"https://resource.pokemon-home.com/battledata/img/pokei128/{icon_fn(dn, fn)}"
def download(dn: int, fn: int = 0):
url = icon_url(dn, fn)
resp = requests.get(url)
if resp.status_code == 403:
raise HTTP403Forbidden
print(f"Request against {url!r} successful")
return resp.content
def save(dir: str, dn: int, fn: int = 0):
content = download(dn, fn)
# makes sure the file is not created if the req fails
with open(Path(dir, icon_fn(dn, fn)), "wb") as file:
file.write(content)
def save_all(dir: str):
for i in range(1011):
j = 0
while True:
try:
save(dir, i, j)
j += 1
except HTTP403Forbidden:
break
if len(argv) <= 1:
argv.append(".")
try:
_, dirn, *excess = argv
if excess:
print(f"Too many arguments\n{USAGE}")
exit(1)
except ValueError:
print(f"Wrong arguments\n{USAGE}")
exit(1)
try:
new_dirn = literal_eval(dirn)
dirn = new_dirn
except (ValueError, SyntaxError):
pass
start_time = time()
save_all(dirn)
end_time = time()
print(f"Duration: {end_time - start_time} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment