-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
341 lines (301 loc) · 13.1 KB
/
player.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import requests
from datetime import datetime
from bnet import BnetBroker
from concurrent.futures import ThreadPoolExecutor
from dateutil.parser import parse
CLASS_DATA = {
'Death Knight': {
'color': '#C41F3B',
'specs': {
'Blood': 'https://wow.zamimg.com/images/wow/icons/medium/spell_deathknight_bloodpresence.jpg',
'Frost': 'https://wow.zamimg.com/images/wow/icons/medium/spell_deathknight_frostpresence.jpg',
'Unholy': 'https://wow.zamimg.com/images/wow/icons/medium/spell_deathknight_unholypresence.jpg',
},
},
'Demon Hunter': {
'color': '#A330C9',
'specs': {
'Havoc': 'https://wow.zamimg.com/images/wow/icons/medium/ability_demonhunter_specdps.jpg',
'Vengeance': 'https://wow.zamimg.com/images/wow/icons/medium/ability_demonhunter_spectank.jpg',
},
},
'Druid': {
'color': '#FF7D0A',
'specs': {
'Balance': 'https://wow.zamimg.com/images/wow/icons/medium/spell_nature_starfall.jpg',
'Feral': 'https://wow.zamimg.com/images/wow/icons/medium/ability_druid_catform.jpg',
'Guardian': 'https://wow.zamimg.com/images/wow/icons/medium/ability_racial_bearform.jpg',
'Restoration': 'https://wow.zamimg.com/images/wow/icons/medium/spell_nature_healingtouch.jpg',
},
},
'Evoker': {
'color': '#33937F',
'specs': {
'Augmentation': 'https://wow.zamimg.com/images/wow/icons/medium/classicon_evoker_augmentation.jpg',
'Devastation': 'https://wow.zamimg.com/images/wow/icons/medium/classicon_evoker_devastation.jpg',
'Preservation': 'https://wow.zamimg.com/images/wow/icons/medium/classicon_evoker_preservation.jpg',
},
},
'Hunter': {
'color': '#ABD473',
'specs': {
'Beast Mastery': 'https://wow.zamimg.com/images/wow/icons/medium/ability_hunter_bestialdiscipline.jpg',
'Marksmanship': 'https://wow.zamimg.com/images/wow/icons/medium/ability_hunter_focusedaim.jpg',
'Survival': 'https://wow.zamimg.com/images/wow/icons/medium/ability_hunter_camouflage.jpg',
},
},
'Mage': {
'color': '#69CCF0',
'specs': {
'Arcane': 'https://wow.zamimg.com/images/wow/icons/medium/spell_holy_magicalsentry.jpg',
'Fire': 'https://wow.zamimg.com/images/wow/icons/medium/spell_fire_firebolt02.jpg',
'Frost': 'https://wow.zamimg.com/images/wow/icons/medium/spell_frost_frostbolt02.jpg',
},
},
'Monk': {
'color': '#00FF96',
'specs': {
'Brewmaster': 'https://wow.zamimg.com/images/wow/icons/medium/spell_monk_brewmaster_spec.jpg',
'Mistweaver': 'https://wow.zamimg.com/images/wow/icons/medium/spell_monk_mistweaver_spec.jpg',
'Windwalker': 'https://wow.zamimg.com/images/wow/icons/medium/spell_monk_windwalker_spec.jpg',
},
},
'Paladin': {
'color': '#F58CBA',
'specs': {
'Holy': 'https://wow.zamimg.com/images/wow/icons/medium/spell_holy_holybolt.jpg',
'Protection': 'https://wow.zamimg.com/images/wow/icons/medium/ability_paladin_shieldofthetemplar.jpg',
'Retribution': 'https://wow.zamimg.com/images/wow/icons/medium/spell_holy_auraoflight.jpg',
},
},
'Priest': {
'color': '#FFFFFF',
'specs': {
'Discipline': 'https://wow.zamimg.com/images/wow/icons/medium/spell_holy_powerwordshield.jpg',
'Holy': 'https://wow.zamimg.com/images/wow/icons/medium/spell_holy_guardianspirit.jpg',
'Shadow': 'https://wow.zamimg.com/images/wow/icons/medium/spell_shadow_shadowwordpain.jpg',
},
},
'Rogue': {
'color': '#FFF569',
'specs': {
'Assassination': 'https://wow.zamimg.com/images/wow/icons/medium/ability_rogue_deadlybrew.jpg',
'Outlaw': 'https://wow.zamimg.com/images/wow/icons/medium/inv_sword_30.jpg',
'Subtlety': 'https://wow.zamimg.com/images/wow/icons/medium/ability_stealth.jpg',
},
},
'Shaman': {
'color': '#0070DE',
'specs': {
'Elemental': 'https://wow.zamimg.com/images/wow/icons/medium/spell_nature_lightning.jpg',
'Enhancement': 'https://wow.zamimg.com/images/wow/icons/medium/spell_shaman_improvedstormstrike.jpg',
'Restoration': 'https://wow.zamimg.com/images/wow/icons/medium/spell_nature_magicimmunity.jpg',
},
},
'Warlock': {
'color': '#9482C9',
'specs': {
'Affliction': 'https://wow.zamimg.com/images/wow/icons/medium/spell_shadow_deathcoil.jpg',
'Demonology': 'https://wow.zamimg.com/images/wow/icons/medium/spell_shadow_metamorphosis.jpg',
'Destruction': 'https://wow.zamimg.com/images/wow/icons/medium/spell_shadow_rainoffire.jpg',
},
},
'Warrior': {
'color': '#C79C6E',
'specs': {
'Arms': 'https://wow.zamimg.com/images/wow/icons/medium/ability_warrior_savageblow.jpg',
'Fury': 'https://wow.zamimg.com/images/wow/icons/medium/ability_warrior_innerrage.jpg',
'Protection': 'https://wow.zamimg.com/images/wow/icons/medium/ability_warrior_defensivestance.jpg',
},
},
}
class Player:
"""Class contains all relevant informations about a player (wow-character)"""
def __init__(self, response, alt=False, hidden=False):
self._response = response
self._data = response.json()
self._is_alt = alt
self._is_hidden = hidden
self._class = self._data['class']
# Data which is only available via bnet api
self._realm_slug = None
self._professions = None
self._equipped_items = None
self.set_up_bnet_data()
self.embellishments()
@property
def name(self):
return self._data['name']
@property
def realm(self):
return self._data['realm']
@property
def realm_slug(self):
return self._realm_slug
@property
def spec(self):
return self._data['active_spec_name']
@property
def ilvl(self):
return self._data['gear']['item_level_equipped']
@property
def score(self):
return self._data['mythic_plus_scores_by_season'][0]['scores']['all']
@property
def achievement_points(self):
return self._data['achievement_points']
@property
def class_color(self):
return CLASS_DATA[self._class]['color']
@property
def professions(self):
return self._professions
def profession_string(self, profession_number):
if profession_number not in [1, 2] or self.professions is None:
return "-"
if profession_number - 1 not in range(len(self.professions)):
return "-"
profession = self.professions[profession_number - 1]
profession_string = profession['profession']['name']
tiers = profession['tiers']
for t in tiers:
if 'Khaz' in t['tier']['name']:
skill_level = f"{t['skill_points']}/{t['max_skill_points']}"
profession_string += f" ({skill_level})"
return profession_string
def mythic_plus_best_runs(self):
return self._data['mythic_plus_best_runs']
def mythic_plus_alternate_runs(self):
return self._data['mythic_plus_alternate_runs']
def weekly_runs(self, weekly):
# rio only returns the 10 highest keys, even if you did more runs per week
runs = self._data[weekly]
# rio sorts them, but resort them just in case
return sorted(runs, key=lambda r: r['mythic_level'], reverse=True)
def last_crawled_at(self):
return parse(self._data['last_crawled_at'])
def days_since_last_update(self):
last_crawled = self.last_crawled_at()
tday = datetime.now(tz=last_crawled.tzinfo)
days = (tday - last_crawled).days
if days > 10: # because rio sometimes doesn't update the data for players
search = ('mythic_plus_recent_runs', 'mythic_plus_best_runs')
if sum([len(self._data[s]) for s in search if s in self._data]) != 0:
latest_date = max([parse(a['completed_at']) for x in search for a in self._data[x]])
days = (tday - latest_date).days
return days
def relevant_scores(self):
scores = dict()
score_dps = self._data['mythic_plus_scores_by_season'][0]['scores']['dps']
if score_dps > 0:
scores['dps'] = score_dps
score_healer = self._data['mythic_plus_scores_by_season'][0]['scores']['healer']
if score_healer > 0:
scores['healer'] = score_healer
score_tank = self._data['mythic_plus_scores_by_season'][0]['scores']['tank']
if score_tank > 0:
scores['tank'] = score_tank
return scores
def get_tier_items(self):
tset_pieces = ['head', 'shoulder', 'chest', 'hands', 'legs']
tset_equiped = [0, 0, 0, 0, 0]
for i, x in enumerate(tset_pieces):
if 'tier' in self._data['gear']['items'][x]:
tset_equiped[i] = int(self._data['gear']['items'][x]['tier'])
if sum(tset_equiped) == 0:
return ""
else:
tool_tip_per_tier = []
different_tiers = set(tset_equiped)
different_tiers.discard(0)
for tier in different_tiers:
count = tset_equiped.count(tier)
pieces = []
for piece, t in zip(tset_pieces, tset_equiped):
if t == tier:
pieces.append(piece)
tool_tip_per_tier.append(f'T{tier}: {count}/5 ({", ".join(pieces)})')
tool_tip = '\n'.join(tool_tip_per_tier)
return tool_tip
def profile_url(self):
return self._data['profile_url']
def thumbnail_url(self):
return self._data['thumbnail_url']
def talents_url(self):
wowhead_talents_url = 'https://www.wowhead.com/talent-calc/blizzard/'
if 'talentLoadout' in self._data:
if 'loadout_text' in self._data['talentLoadout']:
wowhead_talents_url += self._data['talentLoadout']['loadout_text']
return wowhead_talents_url
def spec_icon(self):
class_ = self._class
spec = self.spec
if class_ in CLASS_DATA:
class_specs = CLASS_DATA[class_]['specs']
if spec in class_specs:
return class_specs[spec]
return ""
else:
return ""
def embellishments(self):
embellishments_list = []
if self._equipped_items:
for item in self._equipped_items:
if 'limit_category' in item:
if 'Embellished' in item['limit_category']:
embellishments_list.append({'slot': item['slot']['name'], 'ilvl': item['level']['value']})
return embellishments_list
def set_up_bnet_data(self):
bnet_broker = BnetBroker()
if not bnet_broker.is_operational():
return
realm_index_endpoint = f'/data/wow/realm/index'
r = bnet_broker.pull(realm_index_endpoint, namespace='dynamic-eu')
if not r:
return
for realm in r['realms']:
if realm['name'] == self.realm:
self._realm_slug = realm['slug']
name_lc = self.name.lower()
professions_endpoint = f'/profile/wow/character/{self.realm_slug}/{name_lc}/professions'
r = bnet_broker.pull(professions_endpoint, namespace='profile-eu')
if r:
if 'primaries' in r:
self._professions = r['primaries']
equipment_endpoint = f'/profile/wow/character/{self.realm_slug}/{name_lc}/equipment'
r = bnet_broker.pull(equipment_endpoint, namespace='profile-eu')
if r:
if 'equipped_items' in r:
self._equipped_items = r['equipped_items']
@staticmethod
def embellishment_list_to_tooltip(embellishment_list):
temp = [f"{e['slot']} ({e['ilvl']})" for e in embellishment_list]
tt = ', '.join(temp)
return tt
@staticmethod
def create_players(player_list, responses):
if len(player_list) != len(responses):
print("ERROR: player_list and responses don't have the same length!")
return []
players = []
with ThreadPoolExecutor(max_workers=20) as executor:
futures = []
for p, r in zip(player_list, responses):
future = executor.submit(Player._process_player, p, r)
futures.append(future)
players = [future.result() for future in futures if future.result()]
return players
@staticmethod
def _process_player(p, r):
if not r.ok:
print(f"WARNING: Couldn't get a valid response for player {p['name']}-{p['realm']}:")
print(f"status-code: {r.status_code}")
print(f"Reason: {r.reason}")
print(f"URL: {r.url}")
try:
print(f"Response: {r.json()}")
except requests.exceptions.JSONDecodeError as e:
print(f"Response: {r.text}")
return None
player = Player(r, alt=p['is_alt'], hidden=p.get('is_hidden', False))
return player