-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_manager.py
331 lines (247 loc) · 10.4 KB
/
data_manager.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
import base64
import time
from ruamel.yaml import YAML
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.Util import Padding
import keyring
import keyring.backends.Windows
import requests
import league
import network
import utils
PREFERENCES_PATH = 'resources\\user_preferences.yaml'
HIGHLIGHTS_PATH = 'highlights.yaml'
CHAMPIONS_PATH = 'resources\\champions.json'
PLATFORM = 'lol-highlights-enhancer'
keyring.set_keyring(keyring.backends.Windows.WinVaultKeyring())
class DataStore():
connection_details = None
__internal_data = {
'preferences': {'file_name': PREFERENCES_PATH, 'data': None},
'highlights': {'file_name': HIGHLIGHTS_PATH, 'data': None},
'champions': {'file_name': CHAMPIONS_PATH, 'data': None}}
@classmethod
def __save(cls, data, type, to_file=False):
file_name = cls.__internal_data[type]['file_name']
if to_file:
with open(file_name, 'w') as file:
yaml = YAML(typ='safe')
yaml.dump(data, file)
cls.__internal_data[type]['data'] = data
@classmethod
def __get(cls, type):
file_name = cls.__internal_data[type]['file_name']
data = cls.__internal_data[type]['data']
if data is None:
with open(file_name) as file:
yaml = YAML(typ='safe')
cls.__internal_data[type]['data'] = yaml.load(file)
return cls.__internal_data[type]['data']
@classmethod
def get_preferences(cls):
return cls.__get('preferences')
@classmethod
def get_highlights(cls):
return cls.__get('highlights')
@classmethod
def get_champions(cls):
return cls.__get('champions')
@classmethod
def save_preferences(cls, data, to_file=False):
cls.__save(data, 'preferences', to_file)
@classmethod
def save_highlights(cls, data, to_file=False):
cls.__save(data, 'highlights', to_file)
@classmethod
def save_champions(cls, data, to_file=False):
cls.__save(data, 'champions', to_file)
@classmethod
def __get_latest_token(cls):
client_id, client_secret = cls.get_gfycat_secrets()
body = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret
}
base_url = 'https://api.gfycat.com/v1'
url = f'{base_url}/oauth/token'
response = requests.post(url, json=body)
return response.json()
@classmethod
def __is_expired(cls, expiry):
result = False
current_time = time.time()
if current_time >= float(expiry):
result = True
return result
@classmethod
def get_gfycat_token(cls):
token = keyring.get_password(PLATFORM, 'gfycat_token')
expiry = keyring.get_password(PLATFORM, 'expiry')
if token is None or cls.__is_expired(expiry):
token_details = cls.__get_latest_token()
token = token_details['access_token']
expiry = time.time() + token_details['expires_in']
keyring.set_password(PLATFORM, 'gfycat_token', token)
keyring.set_password(PLATFORM, 'expiry', expiry)
return token
@classmethod
def get_gfycat_secrets(cls):
key = keyring.get_password(PLATFORM, 'puuid')
client_id = keyring.get_password(PLATFORM, 'client_id')
client_secret = keyring.get_password(PLATFORM, 'client_secret')
return (cls.decrypt(key, client_id), cls.decrypt(key, client_secret))
@classmethod
def get_streamable_secrets(cls):
key = keyring.get_password(PLATFORM, 'puuid')
auth = keyring.get_password(PLATFORM, 'Authorization')
return cls.decrypt(key, auth)
@classmethod
def setup(cls):
cls.setup_preferences()
cls.setup_highlights()
cls.setup_champions()
cls.setup_client_secrets()
@classmethod
def setup_client_secrets(cls):
preferences = cls.get_preferences()
user_name = preferences['user_name']
region = preferences['region']
url = f'https://slick.co.ke/v1/summoner/{region}/{user_name}'
summoner_details = requests.get(url).json()
keyring.set_password(PLATFORM, 'puuid', summoner_details['puuid'])
gfycat = summoner_details['secrets']['gfycat']
keyring.set_password(PLATFORM, 'client_id', gfycat['client_id'])
keyring.set_password(PLATFORM, 'client_secret',
gfycat['client_secret'])
streamable = summoner_details['secrets']['streamable']
keyring.set_password(PLATFORM, 'Authorization',
streamable['Authorization'])
@classmethod
def setup_preferences(cls):
current_summoner = network.get('/lol-summoner/v1/current-summoner')
user_name = current_summoner['displayName']
highlights_folder = network.get(
'/lol-highlights/v1/highlights-folder-path')
preferences = cls.get_preferences()
preferences['first_time'] = False
preferences['user_name'] = user_name
preferences['current-highlights-folder'] = highlights_folder
preferences['lol-highlights-folder'].append(highlights_folder)
region = network.get(
'/lol-platform-config/v1/namespaces/LoginDataPacket/platformId')
region = 'na1' if region == 'NA' else region.lower()
preferences['region'] = region
normalised_region = network.get(
'/lol-platform-config/v1/namespaces/LoginDataPacket/'
'competitiveRegion')
preferences['normalised_region'] = normalised_region.lower()
cls.save_preferences(preferences, to_file=True)
@classmethod
def setup_highlights(cls):
highlights = network.post('/lol-highlights/v1/highlights')
highlights_dict = {}
for highlight in highlights:
name = highlight['name']
result = utils.get_match_details(name)
if result is not None:
highlight['region'] = result['region'].lower()
highlight['match_id'] = result['match_id']
patch_major = result['patch_major']
patch_minor = result['patch_minor']
highlight['patch_version'] = f'{patch_major}.{patch_minor}'
else:
highlight['region'] = None
highlight['match_id'] = None
highlight['patch_version'] = None
highlight['gfycat'] = None
highlight['streamable'] = None
highlights_dict[name] = highlight
cls.save_highlights(highlights_dict, to_file=True)
@classmethod
def refresh_highlights(cls):
highlights = network.post('/lol-highlights/v1/highlights')
old_highlights = cls.get_highlights()
for highlight in highlights:
name = highlight['name']
if name not in old_highlights:
result = utils.get_match_details(name)
if result is not None:
highlight['region'] = result['region'].lower()
highlight['match_id'] = result['match_id']
patch_major = result['patch_major']
patch_minor = result['patch_minor']
highlight['patch_version'] = f'{patch_major}.{patch_minor}'
else:
highlight['region'] = None
highlight['match_id'] = None
highlight['patch_version'] = None
highlight['gfycat'] = None
highlight['streamable'] = None
old_highlights[name] = highlight
cls.save_highlights(old_highlights, to_file=True)
@classmethod
def setup_champions(cls):
preferences = cls.get_preferences()
region = preferences['normalised_region']
url = f'https://ddragon.leagueoflegends.com/realms/{region}.json'
latest_version = requests.get(url).json()['n']['champion']
base_url = 'https://ddragon.leagueoflegends.com/cdn/'
url = f'{base_url}{latest_version}/data/en_US/champion.json'
champions = requests.get(url).json()
cls.save_champions(champions, to_file=True)
@classmethod
def get_connection_details(cls):
if cls.connection_details is None:
connection_details = league.get_connection_details()
return connection_details
@classmethod
def decrypt(cls, key, data):
bytes_input = base64.b64decode(data.encode())
block_size = 16
nonce = bytes_input[:block_size]
tag = bytes_input[block_size:block_size * 2]
ciphertext = bytes_input[block_size * 2:]
key = SHA256.new(key.encode()).digest()
cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
padded_data = cipher.decrypt_and_verify(ciphertext, tag)
data = Padding.unpad(padded_data, block_size).decode()
return data
@classmethod
def get_highlight_names(cls):
return list(cls.get_highlights().keys())
@classmethod
def get_highlight(cls, name):
return cls.get_highlights().get(name)
@classmethod
def save_highlight(cls, highlight_name, data):
highlight_dict = {}
highlight_dict[highlight_name] = data
cls.save_partial_highlights(highlight_dict, to_file=True)
@classmethod
def get_champion_by_id(cls, id):
champions = cls.get_champions()
for champion, champion_data in champions['data'].items():
if int(champion_data['key']) == id:
return champion_data['name']
@classmethod
def save_partial_highlights(cls, highlights, to_file=False):
base_highlights = cls.get_highlights()
for highlight_name, data in highlights.items():
base_highlights[highlight_name] = data
cls.save_highlights(base_highlights, to_file)
@classmethod
def get_preference(cls, name):
return cls.get_preferences().get(name)
@classmethod
def save_preference(cls, preference_name, data):
preference_dict = {}
preference_dict[preference_name] = data
cls.save_partial_preferences(preference_dict, to_file=True)
@classmethod
def save_partial_preferences(cls, preferences, to_file=False):
base_preferences = cls.get_preferences()
for preference, data in preferences.items():
base_preferences[preference] = data
cls.save_preferences(base_preferences, to_file)