-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.py
237 lines (187 loc) · 7.65 KB
/
Utils.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
import asyncio
import datetime
import json
import os
import shelve
import time
import aiohttp
import requests
from Responder import Responder
from utils import Config
from utils.LogController import LogController
class Utils:
def __init__(self):
self.logger = LogController().get_logger()
async def fetch_one_url(self, session, url, save_path=None):
async with session.get(url) as response:
time.sleep(0.01)
response_text = await response.text()
if save_path is not None:
with open(save_path, "wb") as text_file:
text_file.write(response_text.encode("UTF-8"))
return url, response_text
### dowloads everything from urls, then returns with response
def download_urls(self, urls: list, save_as=None):
if save_as is None:
save_as = {}
loop = asyncio.get_event_loop()
htmls = loop.run_until_complete(self.download_urls_helper(urls, save_as))
return htmls
async def download_urls_helper(self, urls: list, save_as: dict):
tasks = []
async with aiohttp.ClientSession() as session:
for url in urls:
if url in save_as:
save_path = save_as[url]
else:
save_path = None
tasks.append(self.fetch_one_url(session, url, save_path))
htmls = await asyncio.gather(*tasks)
return htmls
# end DOWNLOAD URLS
def get_number_of_pages(self):
with open('auction/0.json', 'rb') as f:
ah_dict = json.load(f)
if ah_dict['success']:
number_of_pages = ah_dict['totalPages']
self.logger.debug(f"{number_of_pages} pages found")
return number_of_pages
else:
self.logger.error("Error in number_of_pages")
async def download_auctions(self, responder: Responder):
global cake_auctions_json_list
global cake_auction_list
await responder.append_header("Updating all auctions")
for filename in os.listdir('auction'):
os.remove('auction/' + filename)
r = requests.get('https://api.hypixel.net/skyblock/auctions?key=' + Config.API_KEY + '&page=0')
with open(r'auction/0.json', 'wb') as f:
f.write(r.content)
number_of_pages = self.get_number_of_pages()
await responder.append_header(f"Downloading {number_of_pages} pages")
if number_of_pages is None:
self.logger.warn("number_of_pages is None for some reason, "
"downloading 2 pages, so the script does not crash")
number_of_pages = 2
urls = []
save_as = {}
for page_number in range(1, number_of_pages):
url = 'https://api.hypixel.net/skyblock/auctions?key=' + Config.API_KEY + '&page=' + str(page_number)
urls.append(url)
save_as[url] = r'auction/' + str(page_number) + '.json'
self.download_urls(urls, save_as)
await responder.append_header("Auctions successfully updated")
def is_player_online(self, mc_name):
try:
print("is_player_online - mc_name: ", mc_name, flush=True)
mc_uuid = self.get_uuid_from_mc_name(mc_name)
except json.decoder.JSONDecodeError:
self.logger.debug(f"is_player_online - name changed, old name: {mc_name}")
return "name_not_found_or_changed"
if mc_uuid == 429:
return "429_api_limit_reached"
if mc_uuid is None:
return "mc_uuid not found"
url = 'https://api.hypixel.net/player?key=' + Config.API_KEY + '&uuid=' + mc_uuid
try:
json_player_stats = requests.get(url=url).json()
except:
self.logger.error(f"INVALID API RESPONSE - it is not known if {mc_name} is online")
return "hypixel_api_is_down"
if json_player_stats['success']:
try:
if json_player_stats['player']['lastLogin'] > json_player_stats['player']['lastLogout']:
return "Online"
else:
return "Offline"
except KeyError as ex:
self.logger.info(f"Can't retrieve online status from player '{mc_name}' because their API settings are disabled")
return "Unknown"
else:
self.logger.error("is_player_online - ERROR: ", json_player_stats)
return False
def get_uuid_from_mc_name(self, name):
"""
get uuid from minecraft name
returns 429 if api limit is reached
returns None if name is not found
return uuid if name is found
"""
url = 'https://api.mojang.com/users/profiles/minecraft/' + name
response = requests.get(url=url)
data = response.json()
if response.status_code == 429:
print("get_uuid_from_mc_name - API LIMIT REACHED", flush=True)
return 429
# print("get_uuid_from_mc_name - status: ", response.status_code, flush=True)
if 'id' in data:
# print("get_uuid_from_mc_name - data: ", data, flush=True)
return data['id']
else:
self.logger.warn(f"Could not retrieve UUID from player name '{name}': {data}")
return None
# api limit 600 per 10 minutes = 1 per sec
def get_mc_name_from_uuid(self, uuid):
with shelve.open('mcnames') as mcnames:
if uuid not in mcnames:
data = requests.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid).json()
name = data["name"]
mcnames[uuid] = name
self.logger.debug(f"Fetched name from mojang - {mcnames[uuid]}")
# time.sleep(0.1)
found_name = mcnames[uuid]
return found_name
# https://stackoverflow.com/questions/3429510/pythonic-way-to-convert-a-list-of-integers-into-a-string-of-comma-separated-rang
def split_message(msg):
"""
Split message to multiple messages if it is too long.
"""
msgs = msg.split('\n')
# merge adjacent if total length is less than 2000
merged_msgs = []
adding_to_index = 0
for msg in msgs:
if len(merged_msgs) == 0:
merged_msgs.append(msg)
continue
if len(merged_msgs[adding_to_index]) + len(msg) < 1980:
merged_msgs[adding_to_index] += '\n' + msg
else:
merged_msgs.append(msg)
adding_to_index += 1
return merged_msgs
def uuid_add_dashes(uuid):
return f"{uuid[0:8]}-{uuid[8:12]}-{uuid[12:16]}-{uuid[16:20]}-{uuid[20:32]}"
def seconds_to_hours_minutes_seconds(seconds: int):
if seconds < 0:
return seconds
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return f'{h:d}:{m:02d}:{s:02d}'
def k_to_mil(num: int):
return round(num / 1000, 3)
def list_to_ranges(zones):
buffer = []
try:
st = ed = zones[0]
for i in zones[1:]:
delta = i - ed
if delta == 1:
ed = i
elif not (delta == 0):
buffer.append((st, ed))
st = ed = i
else:
buffer.append((st, ed))
except IndexError:
pass
return ','.join(
"%d" % st if st == ed else "%d-%d" % (st, ed)
for st, ed in buffer).replace(",", ", ")
def chunks(lst, n): # https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def delete_database():
with shelve.open('mcnames') as mcnames:
mcnames.clear()