-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoepy_core.py
183 lines (148 loc) · 5.63 KB
/
poepy_core.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! python3.6
import json
import time
import timeit
from statistics import mean
from textwrap import dedent, indent
from timeit import default_timer as timer
from typing import Any, Callable, Dict, List, Set
import regex
import requests
import requests_cache
import poetiergen_constants as constants
requests_cache.install_cache("ninja_data", expire_after=172800)
class MeanTimer:
def __init__(self, name=None):
self.name = " '" + name + "'" if name else ""
self.times: List[float] = []
def __enter__(self):
self.start = timeit.default_timer()
def __exit__(self, exc_type, exc_value, traceback):
self.took = (timeit.default_timer() - self.start) * 1000.0
self.times.append(self.took)
print(
"Code block" + self.name + " took: " + str(mean(self.times)) + "(mean) ms"
)
def FileToJson(filepath: str) -> List[dict]:
file_data = ""
with open(filepath, "r", encoding="utf-8") as json_file:
file_data = json_file.read()
return json.loads(file_data).get("lines")
# MORE URL PARAMS: Essences: 'Essence', Currency: 'Currency', Fragments: 'Fragment', Scarabs: 'Scarab', Fossils: 'Fossil', Resonators: 'Resonator', Watchstones: 'Watchstone'
def DownloadJson(
league_param: str, type_param: str, use_cache: bool = True, type_: str = "item"
) -> List[Dict]:
payload = {"league": league_param, "type": type_param}
if use_cache:
r = requests.get(f"https://poe.ninja/api/data/{type_}overview", params=payload)
print(f"Downloaded from: {r.url} - Cache: {r.from_cache}") # type: ignore
else:
with requests_cache.disabled():
r = requests.get(
f"https://poe.ninja/api/data/{type_}overview", params=payload
)
print(f"Downloaded from: {r.url} - Cache: False") # type: ignore
r.encoding = "utf-8"
return r.json().get("lines")
def GetDivinationData(
league: str = None, download: bool = False, use_cache: bool = True
) -> List[dict]:
if download and league is not None:
print("Starting Download...")
div_data = DownloadJson(league, "DivinationCard", use_cache)
print("Download complete.")
else:
div_data = FileToJson(constants.json_div_filepath)
return div_data
def GetFragmentData(
league: str = None, download: bool = False, use_cache: bool = True
) -> List[dict]:
if download and league is not None:
print("Starting Download...")
fragment_data = DownloadJson(league, "Fragment", use_cache, "currency")
print("Download complete.")
else:
fragment_data = FileToJson(constants.json_fragments_filepath)
return fragment_data
def GetBasesData(
league: str = None, download: bool = False, use_cache: bool = True
) -> List[dict]:
if download and league is not None:
print(f"Starting Download...")
bases = DownloadJson(league, "BaseType", use_cache)
print("Download complete.")
else:
bases = FileToJson(constants.json_bases_filepath)
return bases
def GetUniquesData(
league: str = None, download: bool = False, use_cache: bool = True
) -> List[List[dict]]:
uniques_data = []
param_uniques = [
"UniqueArmour",
"UniqueWeapon",
"UniqueFlask",
"UniqueAccessory",
"UniqueJewel",
"UniqueMap",
]
if download and league is not None:
print("Starting Download...")
for param in param_uniques:
uniques_data.append(DownloadJson(league, param, use_cache))
print("Download complete.")
else:
for path in constants.json_unique_filepaths:
uniques_data.append(FileToJson(path))
return uniques_data
def GetWatchstoneData(
league: str = None, download: bool = False, use_cache: bool = True
) -> List[dict]:
if download and league is not None:
print("Starting Download...")
watchstone_data = DownloadJson(league, "Watchstone", use_cache)
print("Download complete.")
else:
watchstone_data = FileToJson(constants.json_watchstones_filepath)
return watchstone_data
def write_to_file(file_path: str, write_string: Any) -> None:
with open(file_path, "w") as target_file:
target_file.write(str(write_string))
def BaseTypeString(base_types: list) -> str:
"""
Return a space-joined string of the input list,
with each list entry pre- and suffixed by \"
"""
return " ".join(f'"{base_type}"' for base_type in base_types).strip()
def quicktime(fun: Callable) -> float:
start = timer()
fun()
end = timer()
print(end - start)
return end - start
def ternary(condition: bool, true_value: Any, false_value: Any = 0) -> Any:
return true_value if condition else false_value
def InvestigatedItem(item):
inv_set = { # type: ignore
# ('Marble Amulet', 86, 'Normal'),
# ('Prismatic Ring', 82, 'Shaper'),
# ('Latticed Ringmail', 84, 'Elder'),
# ('Leatherscale Boots', 82, 'Elder'),
# ('Bone Helmet', 86, 'Elder'),
# ('Opal Ring', 85, 'Elder'),
# ('Eternal Burgonet', 84, 'Elder')
# ('Titanium Spirit Shield', 86, 'Shaper')
# ('Driftwood Sceptre', 82, 'Normal'),
# ('Highland Blade', 82, 'Normal')
# ('Marble Amulet', 'Normal'),
# ('Prismatic Ring', 'Shaper'),
# ('Latticed Ringmail', 'Elder'),
# ('Leatherscale Boots', 'Elder'),
# ('Bone Helmet', 'Elder'),
# ('Opal Ring', 'Elder'),
# ('Eternal Burgonet', 'Elder'),
# ('Titanium Spirit Shield', 'Shaper'),
# ('Driftwood Sceptre', 'Normal'),
# ('Highland Blade', 'Normal')
}
return item in inv_set