-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbazaarapi.py
More file actions
93 lines (55 loc) · 2.23 KB
/
bazaarapi.py
File metadata and controls
93 lines (55 loc) · 2.23 KB
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
import requests
import json
import time
from math import floor
DEFAULT_ENDPOINT = "https://api.hypixel.net/v2/skyblock/bazaar"
class BazaarItem():
def __init__(self, quantity:int, price:float):
self.quantity:int = quantity
self.price:float = price
def __str__(self):
return f"< {self.quantity}\t@\t{self.price}/u >"
class BazaarItemPair():
def __init__(self, buy_order:BazaarItem, sell_offer:BazaarItem):
self.buy_order:BazaarItem = buy_order
self.sell_offer:BazaarItem = sell_offer
def __str__(self):
return f"Buy order :\t{str(self.buy_order)}\nSell offer :\t{str(self.sell_offer)}"
class BazaarAPI():
def __init__(self):
self.endpoint:str = DEFAULT_ENDPOINT
self.content:dict[str, dict] = None
self.last_update:float = 0
self.min_delay:float = 1
self.watchlist = []
def update_watchlist(self, watchlist:list[str]):
self.watchlist = watchlist.copy()
def loadf(self, filename:str):
with open(filename, 'r') as f:
self.content = json.loads(f.read())
def update(self):
if(self.min_delay - (time.time() - self.last_update)) > 0 : time.sleep(self.min_delay - (time.time() - self.last_update))
r = requests.get(self.endpoint).content
self.content = json.loads(r)
self.last_update = time.time()
def search_id(self, query:str) -> list[str]:
f = []
for key in self.content["products"].keys():
if query.lower() in str(key).lower():
f.append(key)
return f
#could add deeper search with SequenceMatcher
def get_item(self, item_name:str) -> dict[str, list]:
if not (item_name in self.content["products"].keys()):
print(f"ERROR : key {item_name} not found.")
return {"product_id":None, "sell_summary":[], "buy_summary":[]}
return self.content["products"][item_name]
def get_top_orders(self, item_name:str):
bo = self.get_item(item_name)["sell_summary"][0]
so = self.get_item(item_name)["buy_summary"][0]
return BazaarItemPair(BazaarItem(bo["amount"], bo["pricePerUnit"]), BazaarItem(so["amount"], so["pricePerUnit"]))
def get_summary(self):
print(f"\n\n\n=== {floor(time.time())} ===\n")
for product in self.watchlist:
print(f"\n>>> {' '.join([x.capitalize() for x in product.lower().split('_')])} <<<\n")
print(self.get_top_orders(product))