-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviego.py
385 lines (318 loc) · 14.5 KB
/
viego.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
import json
import os
import time
from argparse import RawDescriptionHelpFormatter
from email.policy import HTTP
from msilib.schema import Error
from time import process_time_ns
from types import NoneType
from urllib import response
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import requests
from requests.exceptions import HTTPError
class Error(Exception):
"""Base class for other exceptions"""
pass
class BLANKError(Error):
"""Raised when the input value is too small"""
pass
class IDError(Error):
"""Raised when the input value is too large"""
pass
class MATCHESError(Error):
"""Raised when the input value is too large"""
pass
class GETTINGMATCHError(Error):
"""Raised when the input value is too large"""
pass
class LolViego:
# the idea of this module is that , whatever tier or division you add, it will continue all the way from that specific division to diamond I or IV idk yet
x = 0
TIER_MAPPING = {
"GOLD": ["DIAMOND", "PLATINUM", "GOLD"],
"PLATINUM": ["DIAMOND", "PLATINUM"],
"DIAMOND": ["DIAMOND"],
}
DIVISION_MAPPING = {
"IV": ["IV", "III", "II", "I"],
"III": ["III", "II", "I"],
"II": ["II", "I"],
"I": ["I"],
}
CONTINENTS_MAPPING = {
"br1": "americas",
"eun1": "europe",
"euw1": "europe",
"jp1": "asia",
"kr": "asia",
"la1": "americas",
"la2": "americas",
"na1": "americas",
"oc1": "sea",
"ru": "europe",
}
DATA_ESTRUCTURE = {
"championId": [0],
"championName": [0],
"item0": [0],
"item1": [0],
"item2": [0],
"item3": [0],
"item4": [0],
"item5": [0],
"spell1": [0],
"spell2": [0],
"perk0": [0],
"perk1": [0],
"perk2": [0],
"perk3": [0],
"perk4": [0],
"perk5": [0],
"role": [0],
"lane": [0],
"win": [0],
"tier": [0],
"division": [0],
}
def __init__(self, region, division, tier, page):
self.requests = 0
self.division_list = LolViego.DIVISION_MAPPING[division]
self.tier_list = LolViego.TIER_MAPPING[tier]
self.page_list = [x for x in range(page, 10000)]
self.region = region # BR1, EUN1, EUW1, JP1, KR, LA1, LA2, NA1, OC1, RU
self.region_continent = LolViego.CONTINENTS_MAPPING[region]
self.folder_name = self.region + "_data"
self.csv_name = self.region + "_data.csv"
self.recovery_name = self.region + "_recovery.txt"
with open("./api_key.txt") as f: # keep this secret
contents = f.readline()
self.api_key = contents
if not os.path.isdir(self.folder_name):
os.mkdir(self.folder_name)
if not os.path.isfile(self.folder_name + "/" + self.recovery_name):
with open(self.folder_name + "/" + self.recovery_name, "w+") as f:
f.write("first")
else:
self.recovery_name = self.folder_name + "/" + self.recovery_name
if not os.path.isfile(self.folder_name + "/" + self.csv_name):
self.csv_name = self.folder_name + "/" + self.csv_name
self.lol_data = pd.DataFrame(LolViego.DATA_ESTRUCTURE, index=["championId"])
self.lol_data.to_csv(self.csv_name, index=False)
else:
self.csv_name = self.folder_name + "/" + self.csv_name
self.lol_data = pd.read_csv(self.csv_name)
def get_data(
self,
): # this is for getting a list of players by tier, i will start at platinum and beyond
for division in self.division_list:
for tier in self.tier_list:
for page in self.page_list:
# print(tier)
# print(division)
# print(page)
players_entries = self.leaguev4_get_entries(tier, division, page)
if type(players_entries) is NoneType:
print('retrying in 1s')
time.sleep(1)
players_entries = self.leaguev4_get_entries(tier, division, page)
if type(players_entries) is not NoneType:
pass
else:
print('breaking')
continue
self.requests +=1
for player in players_entries:
summoner_id = player["summonerId"]
puuid = self.summonerv4_get_summoner_by_id(summoner_id)
if type(puuid) is NoneType:
print('retrying in 1s')
time.sleep(1)
puuid = self.summonerv4_get_summoner_by_id(summoner_id)
if type(puuid) is not NoneType:
pass
else:
print('breaking')
continue
time.sleep(1)
self.requests +=1
matches = self.matchv5_matchlist(puuid)
if type(matches) is NoneType:
print('retrying in 1s')
time.sleep(1)
matches = self.matchv5_matchlist(puuid)
if type(matches) is not NoneType:
pass
else:
print('breaking')
continue
time.sleep(1)
self.requests +=1
# print(matches)
# print(puuid)
# print(games)
try:
for match in matches:
time.sleep(0.25)
# print(match)
match_info = self.matchv5_match_info(match)
if type(match_info) is NoneType:
print('retrying in 1s')
time.sleep(1)
match_info = self.matchv5_match_info(match)
if type(match_info) is not NoneType:
pass
else:
print('breaking')
continue
self.requests +=1
print(f'this is request n{self.requests}')
for participant in match_info["participants"]:
perks = participant["perks"]
styles = perks["styles"]
perks = []
for style in styles:
selections = style["selections"]
for selection in selections:
perks.append(selection["perk"])
try:
league_data = {
"championId": [participant["championId"]],
"championName": [
participant["championName"]
],
"item0": [participant["item0"]],
"item1": [participant["item1"]],
"item2": [participant["item2"]],
"item3": [participant["item3"]],
"item4": [participant["item4"]],
"item5": [participant["item5"]],
"spell1": [participant["summoner1Id"]],
"spell2": [participant["summoner2Id"]],
"perk0": [perks[0]],
"perk1": [perks[1]],
"perk2": [perks[2]],
"perk3": [perks[3]],
"perk4": [perks[4]],
"perk5": [perks[5]],
"role": [participant["individualPosition"]],
"lane": [participant["lane"]],
"win": [participant["win"]],
"tier": tier,
"division": division,
}
aux_df = pd.DataFrame(
league_data, index=["championId"]
)
self.lol_data = self.lol_data.append(
aux_df, ignore_index=True
)
except Exception:
print("passing because list out of index")
pass
LolViego.x += 1
if LolViego.x == 50:
self.lol_data.to_csv(self.csv_name, index=False)
LolViego.x = 0
print("writted after 50")
print(f"{tier}, {division}, {page}")
pass
except TypeError as non_iterable:
print("[error] matches list was empty")
continue
def leaguev4_get_entries(
self, tier, division, page
): # returns a list with player info from differents soloq rankeds
url = f"https://{self.region}.api.riotgames.com/lol/league/v4/entries/RANKED_SOLO_5x5/{tier}/{division}?page={page}&api_key={self.api_key}"
try:
response = requests.get(url)
if len(response.json()) == 0: # means there was an empty response
raise BLANKError
print(f"[success] entries gotten")
return response.json()
except HTTPError as http_err: # some serious error like no internet, server error, bad url ec
print(f"[error] HTTP ERROR OCCURRED(SERIOUS): {http_err}")
pass
except requests.exceptions.ConnectionError as e:
return
pass
except Exception as e:
return
pass
except BLANKError: # response was blank, which could mean that we have reached last page
print("[error] response was empty, likely last page")
pass
def summonerv4_get_summoner_by_id(self, summoner_id):
url = f"https://{self.region}.api.riotgames.com/lol/summoner/v4/summoners/{summoner_id}?api_key={self.api_key}"
try:
response = requests.get(url)
if response.status_code == 400:
raise IDError
print(f"[success] puuid gotten")
print(response.json())
if response.status_code == 500:
return
return response.json()["puuid"]
except HTTPError as http_err: # some serious error like no internet, server error, bad url ec
print(f"[error] HTTP ERROR OCCURRED(SERIOUS): {http_err}")
pass
except IDError:
print("[error] id not found or other error")
pass
except TypeError:
print('error with the summonerv4 get summoner by id')
except requests.exceptions.ConnectionError as e:
return
pass
except Exception as e:
return
pass
def matchv5_matchlist(
self, puuid, count=40
): # this is matches by puuid amd returns a list of ids with
start_time = int(time.time()) - 604800 * 2 # two weeks from today
# url = f'https://{self.region_continent}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids?queue=420&type=ranked&endTime={end_time}&count={count}&api_key={self.api_key}'
url = f"https://{self.region_continent}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids?startTime={start_time}&endTime={int(time.time())}&queue=420&type=ranked&start=0&count={count}&api_key={self.api_key}"
try:
response = requests.get(url)
if len(response.json()) == 1:
print("[warning] did not found recent games")
return
if response.status_code != 200:
raise MATCHESError
print(f"[success] match list retrived")
return response.json()
except HTTPError as http_err: # some serious error like no internet, server error, bad url ec
print(f"[error] HTTP ERROR OCCURRED(SERIOUS): {http_err}")
pass
except MATCHESError:
print("[error] matches id ")
pass
except requests.exceptions.ConnectionError as e:
return
pass
except Exception as e:
return
pass
def matchv5_match_info(self, match_id):
url = f"https://{self.region_continent}.api.riotgames.com/lol/match/v5/matches/{match_id}?api_key={self.api_key}"
try:
response = requests.get(url)
if response.status_code != 200:
raise GETTINGMATCHError
print(f"[success] match info gotten")
return response.json()["info"]
except GETTINGMATCHError:
print("[error] could not get match info")
except HTTPError as http_err: # some serious error like no internet, server error, bad url ec
print(f"[error] HTTP ERROR OCCURRED(SERIOUS): {http_err}")
pass
except requests.exceptions.ConnectionError as e:
return
pass
except Exception as e:
return
pass
america = LolViego("na1", "I", "DIAMOND", 1)
america.get_data()