-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mega-alerts.py
449 lines (399 loc) · 15.3 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
#!/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,
)
import utils.mega_data_setup
print("Sleep 10 sec on start to avoid spamming the api")
time.sleep(10)
#### GLOBALS ####
alert_record = []
mega_data = utils.mega_data_setup.MegaData()
#### 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:
return
for auction in clean_auctions:
if "itemID" in auction:
id_msg = f"`itemID:` {auction['itemID']}\n"
if "tertiary_stats" in auction:
item_name = mega_data.DESIRED_ILVL_NAMES[auction["itemID"]]
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"]]
id_msg += f"`Name:` {item_name}\n"
else:
id_msg = f"`petID:` {auction['petID']}\n"
if auction["petID"] in mega_data.PET_NAMES:
pet_name = mega_data.PET_NAMES[auction["petID"]]
id_msg += f"`Name:` {pet_name}\n"
if os.getenv("IMPORTANT_EMOJI"):
if len(os.getenv("IMPORTANT_EMOJI")) == 1:
message = os.getenv("IMPORTANT_EMOJI") * 20 + "\n"
else:
message = "🔥🔥🔥🔥🔥🟢🟢🟢🟢🟢🟢🟢🔥🔥🔥🔥🔥\n"
else:
message = "==================================\n"
message += (
f"`region:` {mega_data.REGION} "
+ f"`realmID:` {auction['realmID']} "
+ id_msg
+ f"`realmNames`: {auction['realmNames']}\n"
)
if mega_data.WOWHEAD_LINK and "itemID" in auction:
item_id = auction["itemID"]
message += f"[Wowhead link](https://www.wowhead.com/item={item_id})\n"
else:
message += f"[Undermine link]({auction['itemlink']})\n"
if "bid_prices" in auction:
message += f"`bid_prices`: {auction['bid_prices']}\n"
else:
message += f"`buyout_prices`: {auction['buyout_prices']}\n"
if os.getenv("IMPORTANT_EMOJI"):
if len(os.getenv("IMPORTANT_EMOJI")) == 1:
message += os.getenv("IMPORTANT_EMOJI") * 20 + "\n"
else:
message += "🔥🔥🔥🔥🔥🟢🟢🟢🟢🟢🟢🟢🔥🔥🔥🔥🔥\n"
else:
message += "==================================\n"
if auction not in alert_record:
mega_data.send_discord_message(message)
alert_record.append(auction)
def clean_listing_data(auctions, connected_id):
all_ah_buyouts = {}
all_ah_bids = {}
pet_ah_buyouts = {}
pet_ah_bids = {}
ilvl_ah_buyouts = []
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)
# 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 items {mega_data.DESIRED_ITEMS} "
f"or pets {mega_data.DESIRED_PETS} "
f"or items to snipe by ilvl and stats "
f"on {connected_id} "
f"{mega_data.REGION}"
)
return
else:
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():
global alert_record
while True:
current_min = int(datetime.now().minute)
# refresh alerts 1 time per hour
if current_min == 1 and mega_data.REFRESH_ALERTS:
print("\n\nClearing Alert Record\n\n")
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 != []:
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:
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. "
+ f"Waiting to run {mega_data.THREADS} concurrent api calls: "
+ f"checking for items {mega_data.DESIRED_ITEMS} "
+ f"or pets {mega_data.DESIRED_PETS} "
+ f"or items to snipe by ilvl and stats "
)
time.sleep(20)
def main_single():
# run everything once slow
for realm in mega_data.get_upload_time_list():
pull_single_realm_data(realm["dataSetID"])
def main_fast():
# run everything once fast
pool = ThreadPoolExecutor(max_workers=mega_data.THREADS)
for realm in mega_data.get_upload_time_list():
pool.submit(pull_single_realm_data, realm["dataSetID"])
pool.shutdown(wait=True)
# start app here
if os.getenv("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()
# # for debugging all realms at once in threads
# main_fast()
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.🟢"
)
print(
f"Blizzard API data only updates 1 time per hour. "
+ f"The updates will come on minute {mega_data.get_upload_time_minutes()} of each hour. "
+ 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. "
+ "After the first run we will trigger once per hour when the new data updates. "
)
time.sleep(1)
# im sick of idiots asking me about the waiting time just run once on startup
main_fast()
# then run the main loop
main()