forked from Ridepad/uwu-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs_profile_parser.py
307 lines (248 loc) · 8.91 KB
/
logs_profile_parser.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Character info:
# http://armory.warmane.com/character/char_name/server
# BeautifulSoup is like document.querySelector() in js
# Parses basic info like name level guild etc.
# Goes thru each inventory slot, parses id, gems, enchants
#
# Talents:
# http://armory.warmane.com/character/char_name/server/talents"
# Encoding works by calculating rank of each pair of ranks of talents
# idk why i just translated it from lua code
# couldnt figure out glyphs cz it seemed random to me
import json
import time
from datetime import datetime
from pathlib import Path
from threading import Thread
from bs4 import BeautifulSoup
from bs4.element import Tag
import requests
PATH = Path(__file__).parent
CACHE_DIR = PATH.joinpath("cache")
CACHE_DIR.mkdir(exist_ok=True)
CHARACTERS_DIR = CACHE_DIR.joinpath("character")
CHARACTERS_DIR.mkdir(exist_ok=True)
done: dict[str, dict] = {}
threads: dict[str, Thread] = {}
HEADERS = {"User-Agent": "WarmaneCharacterParser/2.0; +uwu-logs.xyz"}
TALENTS_ENCODE_STR = "0zMcmVokRsaqbdrfwihuGINALpTjnyxtgevE"
CLASS_NAME_SPEC = "specialization"
CLASS_NAME_PROF = "profskills"
FORMAT_FUNCTION = {
CLASS_NAME_SPEC: lambda v: v.replace(" ", ""),
CLASS_NAME_PROF: lambda v: v.split(maxsplit=1)[0],
}
DOUBLE_RACES = [
"Night", # Night Elf
"Blood", # Blood Elf
]
DOUBLE_CLASSES = [
"Knight", # Death Knight
]
CLASSES_ORDERED = [
"Druid",
"Hunter",
"Mage",
"Paladin",
"Priest",
"Rogue",
"Shaman",
"Warlock",
"Warrior",
"Death Knight",
]
def player_id(player: dict):
return f"{player['name']}--{player['server']}"
def is_valid_response(response: requests.Response):
return response is not None and "guild-name" in response.text
def requests_get(page_url, headers, timeout=2, attempts=3):
for _ in range(attempts):
try:
page = requests.get(page_url, headers=headers, timeout=timeout, allow_redirects=False)
if page.status_code == 200:
return page
except (requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError):
time.sleep(2)
# LOGGER.error(f"Failed to load page: {page_url}")
return None
def parse_slot(slot: Tag):
if not slot.get("rel"): # Empty slot
return {}
# rel="item=51290&ench=3820&gems=3621:3520:0&transmog=22718"
item_properties_list = slot["rel"][0].split("&")
# item_properties = ["item=51290", "ench=3820", "gems=3621:3520:0", "transmog=22718"]
item_properties = dict(property.split("=") for property in item_properties_list)
# item_properties = {"item": "51290", "ench": "3820", "gems": "3621:3520:0", "transmog": "22718"}
item_properties["gems"] = item_properties.get("gems", "0:0:0").split(":")
# item_properties = {"item": "51290", "ench": "3820", "gems": ["3621","3520","0"], "transmog": "22718"}
return item_properties
def get_gear(profile: BeautifulSoup):
equipment = profile.find(class_="item-model").find_all("a")
return [parse_slot(slot) for slot in equipment]
def get_stats_data(stats: Tag, class_name: str) -> dict[str, str]:
if class_name not in FORMAT_FUNCTION:
return []
text: Tag
data = []
format_value = FORMAT_FUNCTION[class_name]
for tag in stats.find_all(class_=class_name):
for text in tag.find_all(class_="text"):
try:
name, value = text.stripped_strings
data.append([name, format_value(value)])
except ValueError:
pass
return data
def _get_race(level_race_class: list[str]):
race = level_race_class[1]
if race in DOUBLE_RACES:
race = " ".join(level_race_class[1:3])
return race
def _get_class(level_race_class: list[str]):
class_ = level_race_class[-1]
if class_ in DOUBLE_CLASSES:
class_ = " ".join(level_race_class[-2:])
return class_
def get_basic_info(profile: BeautifulSoup):
level_race_class_full = profile.find(class_="level-race-class").text.strip()
level_race_class = level_race_class_full.split(",", 1)[0].split(" ")
if "Level" in level_race_class:
level_race_class.remove("Level")
return {
"level": level_race_class[0],
"race": _get_race(level_race_class),
"class": _get_class(level_race_class),
}
def get_class_prefix(soup: BeautifulSoup):
basic_info = get_basic_info(soup)
class_i = CLASSES_ORDERED.index(basic_info["class"])
return TALENTS_ENCODE_STR[class_i * 3]
def get_talent_rank(talent: Tag):
return int(talent.text.strip()[0])
def convert_to_string(tree: Tag):
talents: list[int] = [
get_talent_rank(talent)
for row in tree.find_all(class_="tier")
for talent in row.find_all(class_="talent")
]
if len(talents) & 1:
talents.append(0)
z = zip(talents[::2], talents[1::2])
g = (TALENTS_ENCODE_STR[r1 * 6 + r2] for r1, r2 in z)
s = "".join(g)
if s[-1] == TALENTS_ENCODE_STR[0]:
return s.rstrip(TALENTS_ENCODE_STR[0]) + "Z"
return s
def convert_spec_to_string(spec: Tag):
trees = spec.find_all(class_="talent-tree")
tree_gen = (convert_to_string(tree) for tree in trees)
return "".join(tree_gen).rstrip("Z")
def get_talents_strings(char_name: str, server: str):
url = f"http://armory.warmane.com/character/{char_name}/{server}/talents"
response = requests_get(url, HEADERS)
if not is_valid_response(response):
return []
soup = BeautifulSoup(response.text, "html.parser")
CLASS_PREFIX = get_class_prefix(soup)
return [
CLASS_PREFIX + convert_spec_to_string(spec)
for spec in soup.find_all(class_="talents-container")
]
def get_profile(char_name: str, server: str):
char_url = f"http://armory.warmane.com/character/{char_name}/{server}"
response = requests_get(char_url, HEADERS)
if not is_valid_response(response):
return {}
soup = BeautifulSoup(response.text, "html.parser")
stats = soup.find(id="character-profile").find(class_="information-right")
talents = get_talents_strings(char_name, server)
profile_dict = get_basic_info(soup)
profile_dict["guild"] = soup.find(class_="guild-name").text
profile_dict["specs"] = get_stats_data(stats, CLASS_NAME_SPEC)
profile_dict["profs"] = get_stats_data(stats, CLASS_NAME_PROF)
profile_dict["talents"] = talents
profile_dict["gear_data"] = get_gear(soup)
return profile_dict
def read_json(p: Path):
try:
return json.loads(p.read_text())
except (FileNotFoundError, json.decoder.JSONDecodeError):
return {}
except Exception:
return {}
def dump_json(p: Path, data: dict):
p.write_text(json.dumps(data))
def is_same_as_last_recorded(player_profile: dict, new_profile: dict):
old_profiles = list(player_profile.values())
if not old_profiles:
return False
last_profile = old_profiles[-1]
if not isinstance(last_profile, dict):
last_profile = player_profile.get(last_profile)
return new_profile == last_profile
def parse_and_save_player(player: dict[str, str]):
server = player["server"]
player_name = player["name"]
server_dir = CHARACTERS_DIR.joinpath(server)
server_dir.mkdir(exist_ok=True)
profile_path = server_dir.joinpath(player_name).with_suffix(".json")
player_profile = read_json(profile_path)
new_profile = get_profile(player_name, server)
if not new_profile:
return player_profile
if is_same_as_last_recorded(player_profile, new_profile):
return player_profile
timestamp_now = int(datetime.now().timestamp())
for timestamp, profile in player_profile.items():
if profile == new_profile:
player_profile[timestamp_now] = timestamp
break
else:
player_profile[timestamp_now] = new_profile
dump_json(profile_path, player_profile)
return player_profile
### Used to assure single instance of parser
def wait_for_thread(t: Thread):
try:
t.start()
except RuntimeError:
pass
t.join()
def parse_and_save_player_wrap(player):
player_profile = parse_and_save_player(player)
id = player_id(player)
done[id] = player_profile
return player_profile
def parse_and_save_wrap(player: dict):
id = player_id(player)
if id in done:
return done[id]
if id in threads:
t = threads[id]
else:
t = Thread(target=parse_and_save_player_wrap, args=(player, ))
threads[id] = t
wait_for_thread(t)
return done[id]
def __test():
d = {
"name": "Nomadra",
"server": "Lordaeron",
}
q = get_profile(d["name"], d["server"])
print(q)
return
def __test2():
chars = [
"Nomadra",
"Safiyah",
# "Meownya",
]
for char_name in chars:
url = f"https://armory.warmane.com/api/character/{char_name}/Lordaeron/"
r = requests.get(url, headers=HEADERS)
print()
print(r.text)
time.sleep(3)
if __name__ == "__main__":
__test()