-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmega_alerts.py
597 lines (537 loc) · 23.4 KB
/
mega_alerts.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/usr/bin/python3
from __future__ import print_function
import time, json, random, os
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from utils.helpers import (
create_oribos_exchange_pet_link,
create_oribos_exchange_item_link,
get_wow_russian_realm_ids,
create_embed,
split_list,
)
from PyQt5.QtCore import QThread, pyqtSignal
import utils.mega_data_setup
class Alerts(QThread):
completed = pyqtSignal(int)
progress = pyqtSignal(str)
def __init__(
self,
path_to_data_files=None,
path_to_desired_items=None,
path_to_desired_pets=None,
path_to_desired_ilvl_items=None,
path_to_desired_ilvl_list=None,
):
super(Alerts, self).__init__()
self.running = True
self.path_to_data_files = path_to_data_files
self.path_to_desired_items = path_to_desired_items
self.path_to_desired_pets = path_to_desired_pets
self.path_to_desired_ilvl_items = path_to_desired_ilvl_items
self.path_to_desired_ilvl_list = path_to_desired_ilvl_list
self.alert_record = []
def run(self):
#### FUNCTIONS ####
def pull_single_realm_data(connected_id):
auctions = mega_data.get_listings_single(connected_id)
clean_auctions = clean_listing_data(auctions, connected_id)
if not clean_auctions or len(clean_auctions) == 0:
return
russian_realms = get_wow_russian_realm_ids()
# construct message
suffix = (
" **(RU)**\n"
if clean_auctions[0]["realmID"] in russian_realms
else "\n"
)
is_russian_realm = (
"**(Russian Realm)**"
if clean_auctions[0]["realmID"] in russian_realms
else ""
)
# add details on each snipe to the message
embed_fields = []
for auction in clean_auctions:
if not self.running:
break
if "itemID" in auction:
id_msg = f"`itemID:` {auction['itemID']}\n"
saddlebag_link_id = auction["itemID"]
if "tertiary_stats" in auction:
item_name = mega_data.DESIRED_ILVL_NAMES[auction["itemID"]]
# old method
# id_msg += f"`Name:` {item_name}\n"
id_msg += f"`ilvl:` {auction['ilvl']}\n"
id_msg += f"`tertiary_stats:` {auction['tertiary_stats']}\n"
id_msg += f"`bonus_ids:` {list(auction['bonus_ids'])}\n"
elif auction["itemID"] in mega_data.ITEM_NAMES:
item_name = mega_data.ITEM_NAMES[auction["itemID"]]
# old method
# id_msg += f"`Name:` {item_name}\n"
else:
item_name = "Unknown Item"
# old method
# id_msg += f"`Name:` {item_name}\n"
embed_name = item_name
else:
id_msg = f"`petID:` {auction['petID']}\n"
saddlebag_link_id = auction["petID"]
if auction["petID"] in mega_data.PET_NAMES:
pet_name = mega_data.PET_NAMES[auction["petID"]]
# old method
# id_msg += f"`Name:` {pet_name}\n"
else:
pet_name = "Unknown Pet"
# old method
# id_msg += f"`Name:` {pet_name}\n"
embed_name = pet_name
message = ""
# old method
# message += f"`region:` {mega_data.REGION}` realmID:` {auction['realmID']} {is_russian_realm}\n"
# message += f"`realmNames`: {auction['realmNames']}{suffix}\n"
message += id_msg
# Add item links, if available
link_label = (
"Wowhead link"
if mega_data.WOWHEAD_LINK and "itemID" in auction
else "Undermine link"
)
link_url = (
f"https://www.wowhead.com/item={auction['itemID']}"
if mega_data.WOWHEAD_LINK and "itemID" in auction
else auction["itemlink"]
)
if not mega_data.NO_LINKS:
message += f"[{link_label}]({link_url})\n"
message += f"[Saddlebag link](https://saddlebagexchange.com/wow/item-data/{saddlebag_link_id})\n"
message += f"[Where to Sell](https://saddlebagexchange.com/wow/export-search?itemId={saddlebag_link_id})\n"
# Add price info, if available
price_type = (
"bid_prices" if "bid_prices" in auction else "buyout_prices"
)
message += f"`{price_type}`: {auction[price_type]}\n"
# send alerts
if auction not in self.alert_record:
# # old method one message per item
# mega_data.send_discord_message(message)
embed_fields.append(
{
"name": embed_name,
"value": message,
"inline": True,
}
)
self.alert_record.append(auction)
else:
print(f"Already sent this alert {auction}")
if len(embed_fields) != 0:
# new embed method one message per realm
desc = f"**region:** {mega_data.REGION}\n"
desc += (
f"**realmID:** {clean_auctions[0]['realmID']} {is_russian_realm}\n"
)
desc += f"**realmNames:** {clean_auctions[0]['realmNames']}{suffix}"
# split it up so message is not too long
for chunk in split_list(embed_fields, 10):
item_embed = create_embed(
f"{mega_data.REGION} SNIPE FOUND!", desc, chunk
)
mega_data.send_discord_embed(item_embed)
def clean_listing_data(auctions, connected_id):
all_ah_buyouts = {}
all_ah_bids = {}
pet_ah_buyouts = {}
pet_ah_bids = {}
ilvl_ah_buyouts = []
if len(auctions) == 0:
print(f"no listings found on {connected_id} of {mega_data.REGION}")
return
def add_price_to_dict(price, item_id, price_dict, is_pet=False):
if is_pet:
if price < mega_data.DESIRED_PETS[item_id] * 10000:
if item_id not in price_dict:
price_dict[item_id] = [price / 10000]
elif price / 10000 not in price_dict[item_id]:
price_dict[item_id].append(price / 10000)
elif price < mega_data.DESIRED_ITEMS[item_id] * 10000:
if item_id not in price_dict:
price_dict[item_id] = [price / 10000]
elif price / 10000 not in price_dict[item_id]:
price_dict[item_id].append(price / 10000)
for item in auctions:
item_id = item["item"]["id"]
# regular items
if item_id in mega_data.DESIRED_ITEMS and item_id != 82800:
price = 10000000 * 10000
if "bid" in item and mega_data.SHOW_BIDPRICES == "true":
price = item["bid"]
add_price_to_dict(price, item_id, all_ah_bids)
if "buyout" in item:
price = item["buyout"]
add_price_to_dict(price, item_id, all_ah_buyouts)
if "unit_price" in item:
price = item["unit_price"]
add_price_to_dict(price, item_id, all_ah_buyouts)
# all caged battle pets have item id 82800
elif item_id == 82800:
if item["item"]["pet_species_id"] in mega_data.DESIRED_PETS:
pet_id = item["item"]["pet_species_id"]
price = 10000000 * 10000
if "bid" in item and mega_data.SHOW_BIDPRICES == "true":
price = item["bid"]
add_price_to_dict(price, pet_id, pet_ah_bids, is_pet=True)
if "buyout" in item:
price = item["buyout"]
add_price_to_dict(
price, pet_id, pet_ah_buyouts, is_pet=True
)
# ilvl snipe items
if (
mega_data.DESIRED_ILVL_ITEMS
and item_id in mega_data.DESIRED_ILVL_ITEMS["item_ids"]
):
ilvl_item_info = check_tertiary_stats_generic(
item,
mega_data.socket_ids,
mega_data.leech_ids,
mega_data.avoidance_ids,
mega_data.speed_ids,
mega_data.ilvl_addition,
mega_data.DESIRED_ILVL_ITEMS,
mega_data.min_ilvl,
)
if ilvl_item_info:
ilvl_ah_buyouts.append(ilvl_item_info)
for desired_ilvl_item in mega_data.DESIRED_ILVL_LIST:
if item_id in desired_ilvl_item["item_ids"]:
ilvl_item_info = check_tertiary_stats_generic(
item,
mega_data.socket_ids,
mega_data.leech_ids,
mega_data.avoidance_ids,
mega_data.speed_ids,
mega_data.ilvl_addition,
desired_ilvl_item,
desired_ilvl_item["ilvl"],
)
if ilvl_item_info:
ilvl_ah_buyouts.append(ilvl_item_info)
if not (
all_ah_buyouts
or all_ah_bids
or pet_ah_buyouts
or pet_ah_bids
or ilvl_ah_buyouts
):
print(
f"no listings found matching desires on {connected_id} of {mega_data.REGION}"
)
return
else:
print(f"Found matches on {connected_id} of {mega_data.REGION}!!!")
return format_alert_messages(
all_ah_buyouts,
all_ah_bids,
connected_id,
pet_ah_buyouts,
pet_ah_bids,
list(ilvl_ah_buyouts),
)
def check_tertiary_stats_generic(
auction,
socket_ids,
leech_ids,
avoidance_ids,
speed_ids,
ilvl_addition,
DESIRED_ILVL_ITEMS,
min_ilvl,
):
if "bonus_lists" not in auction["item"]:
return False
item_bonus_ids = set(auction["item"]["bonus_lists"])
# look for intersection of bonus_ids and any other lists
tertiary_stats = {
"sockets": len(item_bonus_ids & socket_ids) != 0,
"leech": len(item_bonus_ids & leech_ids) != 0,
"avoidance": len(item_bonus_ids & avoidance_ids) != 0,
"speed": len(item_bonus_ids & speed_ids) != 0,
}
desired_tertiary_stats = {
"sockets": DESIRED_ILVL_ITEMS["sockets"],
"leech": DESIRED_ILVL_ITEMS["leech"],
"avoidance": DESIRED_ILVL_ITEMS["avoidance"],
"speed": DESIRED_ILVL_ITEMS["speed"],
}
# if we're looking for sockets, leech, avoidance, or speed, skip if none of those are present
# Check if any of the desired stats are True
if any(desired_tertiary_stats):
# Check if all the desired stats are present in the tertiary_stats
for stat, desired in desired_tertiary_stats.items():
if desired and not tertiary_stats.get(stat, False):
return False
# get ilvl
base_ilvl = DESIRED_ILVL_ITEMS["base_ilvls"][auction["item"]["id"]]
ilvl_addition = [
ilvl_addition[bonus_id]
for bonus_id in item_bonus_ids
if bonus_id in ilvl_addition.keys()
]
if len(ilvl_addition) > 0:
ilvl = base_ilvl + sum(ilvl_addition)
else:
ilvl = base_ilvl
# skip if ilvl is too low
if ilvl < min_ilvl:
return False
# if we get through everything and still haven't skipped, add to matching
buyout = round(auction["buyout"] / 10000, 2)
if buyout > DESIRED_ILVL_ITEMS["buyout"]:
return False
else:
return {
"item_id": auction["item"]["id"],
"buyout": buyout,
"tertiary_stats": tertiary_stats,
"bonus_ids": item_bonus_ids,
"ilvl": ilvl,
}
def format_alert_messages(
all_ah_buyouts,
all_ah_bids,
connected_id,
pet_ah_buyouts,
pet_ah_bids,
ilvl_ah_buyouts,
):
results = []
realm_names = mega_data.get_realm_names(connected_id)
for itemID, auction in all_ah_buyouts.items():
# use instead of item name
itemlink = create_oribos_exchange_item_link(
realm_names[0], itemID, mega_data.REGION
)
results.append(
results_dict(
auction,
itemlink,
connected_id,
realm_names,
itemID,
"itemID",
"buyout",
)
)
for petID, auction in pet_ah_buyouts.items():
# use instead of item name
itemlink = create_oribos_exchange_pet_link(
realm_names[0], petID, mega_data.REGION
)
results.append(
results_dict(
auction,
itemlink,
connected_id,
realm_names,
petID,
"petID",
"buyout",
)
)
for auction in ilvl_ah_buyouts:
itemID = auction["item_id"]
# use instead of item name
itemlink = create_oribos_exchange_item_link(
realm_names[0], itemID, mega_data.REGION
)
results.append(
ilvl_results_dict(
auction,
itemlink,
connected_id,
realm_names,
itemID,
"itemID",
"buyout",
)
)
if mega_data.SHOW_BIDPRICES == "true":
for itemID, auction in all_ah_bids.items():
# use instead of item name
itemlink = create_oribos_exchange_item_link(
realm_names[0], itemID, mega_data.REGION
)
results.append(
results_dict(
auction,
itemlink,
connected_id,
realm_names,
itemID,
"itemID",
"bid",
)
)
for petID, auction in pet_ah_bids.items():
# use instead of item name
itemlink = create_oribos_exchange_pet_link(
realm_names[0], petID, mega_data.REGION
)
results.append(
results_dict(
auction,
itemlink,
connected_id,
realm_names,
petID,
"petID",
"bid",
)
)
# end of the line alerts go out from here
return results
def results_dict(
auction, itemlink, connected_id, realm_names, id, idType, priceType
):
auction.sort()
minPrice = auction[0]
return {
"region": mega_data.REGION,
"realmID": connected_id,
"realmNames": realm_names,
f"{idType}": id,
"itemlink": itemlink,
"minPrice": minPrice,
f"{priceType}_prices": json.dumps(auction),
}
def ilvl_results_dict(
auction, itemlink, connected_id, realm_names, id, idType, priceType
):
tertiary_stats = [
stat for stat, present in auction["tertiary_stats"].items() if present
]
return {
"region": mega_data.REGION,
"realmID": connected_id,
"realmNames": realm_names,
f"{idType}": id,
"itemlink": itemlink,
"minPrice": auction[priceType],
f"{priceType}_prices": auction[priceType],
"tertiary_stats": tertiary_stats,
"bonus_ids": auction["bonus_ids"],
"ilvl": auction["ilvl"],
}
#### MAIN ####
def main():
while self.running:
current_min = int(datetime.now().minute)
# refresh alerts 1 time per hour
if current_min == 1 and mega_data.REFRESH_ALERTS:
print(self.alert_record)
print("\n\nClearing Alert Record\n\n")
self.alert_record = []
matching_realms = [
realm["dataSetID"]
for realm in mega_data.get_upload_time_list()
if realm["lastUploadMinute"] + mega_data.SCAN_TIME_MIN
<= current_min
<= realm["lastUploadMinute"] + mega_data.SCAN_TIME_MAX
]
# mega wants extra alerts
if mega_data.EXTRA_ALERTS:
extra_alert_mins = json.loads(mega_data.EXTRA_ALERTS)
if current_min in extra_alert_mins:
matching_realms = [
realm["dataSetID"]
for realm in mega_data.get_upload_time_list()
]
if matching_realms != []:
self.progress.emit("Sending alerts!")
pool = ThreadPoolExecutor(max_workers=mega_data.THREADS)
for connected_id in matching_realms:
pool.submit(pull_single_realm_data, connected_id)
pool.shutdown(wait=True)
else:
self.progress.emit(
f"The updates will come\non min {mega_data.get_upload_time_minutes()} of each hour."
)
print(
f"Blizzard API data only updates 1 time per hour. The updates will come on minute {mega_data.get_upload_time_minutes()} of each hour. "
+ f"{datetime.now()} is not the update time. "
)
time.sleep(20)
self.progress.emit("Stopped alerts!")
self.completed.emit(1)
def main_single():
# run everything once slow
for connected_id in set(mega_data.WOW_SERVER_NAMES.values()):
pull_single_realm_data(connected_id)
def main_fast():
self.progress.emit("Sending alerts!")
# run everything once fast
pool = ThreadPoolExecutor(max_workers=mega_data.THREADS)
for connected_id in set(mega_data.WOW_SERVER_NAMES.values()):
pool.submit(pull_single_realm_data, connected_id)
pool.shutdown(wait=True)
self.progress.emit("Setting data and\nconfig variables!")
print("Sleep 10 sec on start to avoid spamming the api")
time.sleep(10)
if not self.running:
self.progress.emit("Stopped alerts!")
self.completed.emit(1)
return
#### GLOBALS ####
mega_data = utils.mega_data_setup.MegaData(
self.path_to_data_files,
self.path_to_desired_items,
self.path_to_desired_pets,
self.path_to_desired_ilvl_items,
self.path_to_desired_ilvl_list,
)
if not self.running:
self.progress.emit("Stopped alerts!")
self.completed.emit(1)
return
# show details on run
print(
f"Blizzard API data only updates 1 time per hour.\n"
+ f"The updates for region '{mega_data.REGION}' for '{mega_data.FACTION}' faction AH will come on minute {mega_data.get_upload_time_minutes()} of each hour.\n"
+ f"{datetime.now()} may not the update time. "
+ "But we will run once to get the current data so no one asks me about the waiting time.\n"
+ "After the first run we will trigger once per hour when the new data updates.\n"
+ f"Running {mega_data.THREADS} concurrent api calls\n"
+ f"checking for items {mega_data.DESIRED_ITEMS}\n"
+ f"or pets {mega_data.DESIRED_PETS}\n"
+ f"or or ilvl items from list {mega_data.DESIRED_ILVL_LIST}\n"
)
# start app here
if mega_data.DEBUG:
mega_data.send_discord_message(
"DEBUG MODE: starting mega alerts to run once and then exit operations"
)
# for debugging one realm at a time
main_single()
else:
mega_data.send_discord_message(
"🟢Starting mega alerts and scan all AH data instantly.🟢\n"
+ "🟢These first few messages might be old.🟢\n"
+ "🟢All future messages will release seconds after the new data is available.🟢"
)
time.sleep(1)
if not self.running:
self.progress.emit("Stopped alerts!")
self.completed.emit(1)
return
# im sick of idiots asking me about the waiting time just run once on startup
main_fast()
if not self.running:
self.progress.emit("Stopped alerts!")
self.completed.emit(1)
return
# then run the main loop
main()
if __name__ == "__main__":
fun = Alerts()
fun.run()