-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSpecs.py
133 lines (117 loc) · 3.35 KB
/
getSpecs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from gw2api import GuildWars2Client
import time
import json
import yaml
import pandas as pd
from pandas.core.common import flatten
import pandas.io.formats.style
html_location = "../material/overrides/"
search_term = "specializations"
amount_per_chunk = 100
time_between_chunks = 1
time_to_sleep = 60
gw2 = GuildWars2Client()
specializations = []
print("Pulling data from the GW2 API...")
for i in gw2.specializations.get():
specializations.append(i)
print("Calculating total data pieces...")
cntSpecs = len(specializations)
def get_chunks(size):
chunked_list = [
specializations[i : i + size] for i in range(0, cntSpecs, size)
]
return chunked_list
def get_json(lst):
print("Writing JSON file...")
with open("specializations.json", "w") as outfile:
for item in lst:
print(json.dumps(item), file=outfile)
def get_yaml(lst):
yamlString = yaml.dump(lst, indent=2, sort_keys=False)
yamlFile = open(search_term + ".yaml", "w")
yamlFile.write(yamlString)
yamlFile.close()
def get_html(lst):
html_name = html_location + search_term + ".html"
df = pd.DataFrame(data=lst)
df.drop(
[
"icon",
"background",
"weapon_trait",
"profession_icon_big",
"profession_icon",
],
axis=1,
inplace=True,
)
df.columns = [
"API ID",
"Specialization",
"Profession",
"Elite?",
"Minor Trait IDs",
"Major Trait IDs",
]
df.set_index("API ID")
df["Trait IDs"] = df["Major Trait IDs"] + df["Minor Trait IDs"]
flattened_col = pd.DataFrame(
[
(index, value)
for (index, values) in df["Trait IDs"].iteritems()
for value in values
],
columns=["index", "Trait IDs"],
).set_index("index")
df = df.drop("Trait IDs", axis=1).join(flattened_col)
df.drop(["Minor Trait IDs", "Major Trait IDs"], axis=1, inplace=True)
result = """
{% extends "overrides/main.html" %}
{% block content %}
<section>
<div>
<div>
<div>
<h1>Specializations</h1>
"""
result += df.to_html(
table_id=search_term, escape=False, border=0, index=False
)
result += """
</div>
</div>
</div>
</section>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
<script>
const table = new simpleDatatables.DataTable("#specializations")
</script>
{% endblock %}
"""
with open(html_name, "w") as f:
f.write(result)
# Get api search item in chunks to avoid throttling
objChunks = get_chunks(size=amount_per_chunk)
lstSearchItems = []
missedItems = []
for chunk in objChunks:
i = 0
# print(chunk) #debug
for i in chunk:
spec = gw2.specializations.get(id=i)
if spec == {"text": "too many requests"}:
print("API rate limit hit - sleeping for 60s...")
missedItems.append(i)
time.sleep(time_to_sleep)
else:
lstSearchItems.append(spec)
chunk_n = 0
# print(missedItems) #debug
for i in missedItems:
spec = gw2.specializations.get(id=i)
lstSearchItems.append(spec)
# get_json(lstSearchItems)
get_yaml(lstSearchItems)
#get_html(lstSearchItems)
print("Process complete")