-
Notifications
You must be signed in to change notification settings - Fork 8
/
cache_map.py
executable file
·74 lines (57 loc) · 2.62 KB
/
cache_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3
"""OBSPokemonHUD - Pre-Cache and Check Map Files
This pre-caches and tests the given map file by going through every entry and
downloading it locally. If an error shows up in the console, then either the
file in the map is wrong or it's missing from the server. (Like shiny starter
Eevee).
This is not for every user to run, it's mostly for testing purposes for when
you are creating new map files.
"""
import json
import os
import requests
import sys
# Double check that a map is given. Otherwise exit
if len(sys.argv) != 2:
print("No map file given. Exitting...")
sys.exit()
with open(sys.argv[1], 'r') as file:
sprite_map = json.load(file)
for pokemon in sprite_map['sprites']:
for form in sprite_map['sprites'][pokemon]:
normal_link = sprite_map['urls']['normal'] + sprite_map['sprites'][pokemon][form]
shiny_link = sprite_map['urls']['shiny'] + sprite_map['sprites'][pokemon][form]
# Standard
# Get the file name from the image link
filename = normal_link.split("/")[-1]
# Check that the file doesn't exist yet
if not os.path.isfile("cache/" + sprite_map['cache_location'] + "/" + filename):
# Do a request for the file
response = requests.get(normal_link)
# Check the image file's response code to see if it exists on their
# end
if response.status_code == 404:
print("File not found: " + filename)
elif response.status_code != 200:
print("(" + str(response.status_code) + ")" + filename)
# If it does, save it.
else:
with open("cache/" + sprite_map['cache_location'] + "/" + filename, "wb") as file:
file.write(response.content)
# Shiny
# Get the file name from the image link
filename = shiny_link.split("/")[-1]
# Check that the file doesn't exist yet
if not os.path.isfile("cache/" + sprite_map['cache_location'] + "/shiny/" + filename):
# Do a request for the file
response = requests.get(shiny_link)
# Check the image file's response code to see if it exists on their
# end
if response.status_code == 404:
print("File not found: shiny/" + filename)
elif response.status_code != 200:
print("(" + str(response.status_code) + ") shiny/" + filename)
# If it does, save it.
else:
with open("cache/" + sprite_map['cache_location'] + "/shiny/" + filename, "wb") as file:
file.write(response.content)