-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathupdate_pokedex.py
More file actions
67 lines (54 loc) · 1.98 KB
/
update_pokedex.py
File metadata and controls
67 lines (54 loc) · 1.98 KB
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
# -*- coding: utf-8 -*-
import requests
import re
import json
# Fetch latest version
data = requests.get(
"https://raw.githubusercontent.com/smogon/pokemon-showdown/master/data/pokedex.ts"
).text
# get rid of beginning Typescript object definitions
data = data.split("= {")
assert len(data) == 2, f"expecting data to have length=2: {[i[:50] for i in data]}"
data = "{" + data[1]
# Get rid of tabs
data = data.replace("\t", " ")
# Remove comments
data = re.sub(r" +//.+", "", data) # end-of-line comments
data = re.sub(r"\/\*[\s\S]*?\*\/", "", data) # multi-line comments
# double newlines are unnecessary
while "\n\n" in data:
data = data.replace("\n\n", "\n")
# get rid of commas on the final attribute of objects. These aren't valid JSON
data = re.sub(r",\n( *)([\}\]])", r"\n\1\2", data)
# add double-quotes to keys that do not have them
data = re.sub(r"([\w\d]+): ", r'"\1": ', data)
# Correct double-quoted text inside double-quoted text
data = re.sub(r': ""(.*)":(.*)",', r': "\1:\2",', data)
# remove semicolon at end of file
data = data.replace("};", "}")
# should be parseable as JSON now
data_json = json.loads(data)
# some custom changes for this project
for k, v in data_json.items():
v["baseStats"] = {
"hp": v["baseStats"]["hp"],
"attack": v["baseStats"]["atk"],
"defense": v["baseStats"]["def"],
"special-attack": v["baseStats"]["spa"],
"special-defense": v["baseStats"]["spd"],
"speed": v["baseStats"]["spe"],
}
v["types"] = [
i.lower() for i in v["types"]
]
v["name"] = v["name"].lower()
# re-create the dictionary in order of pokedex numbers
# put negative numbers at the end
new_dict = {}
sorted_dex = sorted(data_json.items(), key=lambda x: x[1]["num"])
negative_nums = [i for i in sorted_dex if i[1]["num"] <= 0]
sorted_dex = [i for i in sorted_dex if i[1]["num"] > 0]
for k, v in sorted_dex + negative_nums:
new_dict[k] = v
with open("pokedex_new.json", "w") as f:
json.dump(data_json, f, indent=4)