-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
266 lines (227 loc) · 9.44 KB
/
dataset.py
File metadata and controls
266 lines (227 loc) · 9.44 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
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
import requests
import pandas as pd
import numpy.random as rd
import pickle
from ChampionStat import getChampion
import numpy as np
"""
Request from API
params:
URL: link to request
PARAMS: dictonary with request header
"""
def __FetchAPI(URL, PARAMS = {}):
try:
return requests.get(url=URL, params=PARAMS).json()
except:
print("Bad request")
"""
Request from Match
params:
Id: id for match
API_key: key to acess data information ate riotgames api
"""
def __FetchMatch(matchId, API_KEY):
URL = "https://br1.api.riotgames.com/lol/match/v4/matches/" + str(matchId)
PARAMS = {'api_key': API_KEY}
return __FetchAPI(URL, PARAMS)
def __FetchMatchList(accountId, API_KEY):
URL = "https://br1.api.riotgames.com/lol/match/v4/matchlists/by-account/" + str(accountId) + "?queue=440&queue=420"
PARAMS = {'api_key': API_KEY}
return __FetchAPI(URL, PARAMS)
"""
Request a chapion ID Hash from ddragon LoL api
"""
def ChampionIdHash():
URL = "http://ddragon.leagueoflegends.com/cdn/9.23.1/data/en_US/champion.json"
champData = __FetchAPI(URL)
champData = champData["data"]
champHash = {}
for champion in champData:
champHash[int(champData[champion]["key"])] = champion
return champHash
"""
convert champion id to champion name
params:
id: champion id
champHash: championHash (given by ddragon api)
"""
def ChampId2Name(id, champHash):
if id < 0:
return None
else:
return champHash[id]
"""
Get data from game mode, bans, picks and lanes from a given matchId
params:
matchId: id for match
API_KEY: the acess Key for riot games API
champHas: the Hash identifier for champions
"""
def FetchMatchData(matchId, API_KEY, champHash):
data = __FetchMatch(matchId, API_KEY)
try:
if data["queueId"] == 420:
gameMode = "Ranked-5v5-Solo"
elif data["queueId"] == 440:
gameMode = "Ranked-5v5-Flex"
else:
print("Not a Ranked queue.")
return None
game_result = data["teams"][0]["win"] == "Win"
row = { "matchId": data["gameId"],
"gameMode": gameMode,
"ban1": ChampId2Name(data["teams"][0]["bans"][0]["championId"], champHash),
"ban2": ChampId2Name(data["teams"][0]["bans"][1]["championId"], champHash),
"ban3": ChampId2Name(data["teams"][0]["bans"][2]["championId"], champHash),
"ban4": ChampId2Name(data["teams"][0]["bans"][3]["championId"], champHash),
"ban5": ChampId2Name(data["teams"][0]["bans"][4]["championId"], champHash),
"ban6": ChampId2Name(data["teams"][1]["bans"][0]["championId"], champHash),
"ban7": ChampId2Name(data["teams"][1]["bans"][1]["championId"], champHash),
"ban8": ChampId2Name(data["teams"][1]["bans"][2]["championId"], champHash),
"ban9": ChampId2Name(data["teams"][1]["bans"][3]["championId"], champHash),
"ban10": ChampId2Name(data["teams"][1]["bans"][4]["championId"], champHash),
"champ1": ChampId2Name(data["participants"][0]["championId"], champHash),
"lane1": data["participants"][0]["timeline"]["lane"],
"Name1": data["participantIdentities"][0]["player"]["summonerName"],
"summonerId1": data["participantIdentities"][0]["player"]["summonerId"],
"accountId1": data["participantIdentities"][0]["player"]["accountId"],
"champ2": ChampId2Name(data["participants"][1]["championId"], champHash),
"lane2": data["participants"][1]["timeline"]["lane"],
"Name2": data["participantIdentities"][1]["player"]["summonerName"],
"summonerId2": data["participantIdentities"][1]["player"]["summonerId"],
"accountId2": data["participantIdentities"][1]["player"]["accountId"],
"champ3": ChampId2Name(data["participants"][2]["championId"], champHash),
"lane3": data["participants"][2]["timeline"]["lane"],
"Name3": data["participantIdentities"][2]["player"]["summonerName"],
"summonerId3": data["participantIdentities"][2]["player"]["summonerId"],
"accountId3": data["participantIdentities"][2]["player"]["accountId"],
"champ4": ChampId2Name(data["participants"][3]["championId"], champHash),
"lane4": data["participants"][3]["timeline"]["lane"],
"Name4": data["participantIdentities"][3]["player"]["summonerName"],
"summonerId4": data["participantIdentities"][3]["player"]["summonerId"],
"accountId4": data["participantIdentities"][3]["player"]["accountId"],
"champ5": ChampId2Name(data["participants"][4]["championId"], champHash),
"lane5": data["participants"][4]["timeline"]["lane"],
"Name5": data["participantIdentities"][4]["player"]["summonerName"],
"summonerId5": data["participantIdentities"][4]["player"]["summonerId"],
"accountId5": data["participantIdentities"][4]["player"]["accountId"],
"champ6": ChampId2Name(data["participants"][5]["championId"], champHash),
"lane6": data["participants"][5]["timeline"]["lane"],
"Name6": data["participantIdentities"][5]["player"]["summonerName"],
"summonerId6": data["participantIdentities"][5]["player"]["summonerId"],
"accountId6": data["participantIdentities"][5]["player"]["accountId"],
"champ7": ChampId2Name(data["participants"][6]["championId"], champHash),
"lane7": data["participants"][6]["timeline"]["lane"],
"Name7": data["participantIdentities"][6]["player"]["summonerName"],
"summonerId7": data["participantIdentities"][6]["player"]["summonerId"],
"accountId7": data["participantIdentities"][6]["player"]["accountId"],
"champ8": ChampId2Name(data["participants"][7]["championId"], champHash),
"lane8": data["participants"][7]["timeline"]["lane"],
"Name8": data["participantIdentities"][7]["player"]["summonerName"],
"summonerId8": data["participantIdentities"][7]["player"]["summonerId"],
"accountId8": data["participantIdentities"][7]["player"]["accountId"],
"champ9": ChampId2Name(data["participants"][8]["championId"], champHash),
"lane9": data["participants"][8]["timeline"]["lane"],
"Name9": data["participantIdentities"][8]["player"]["summonerName"],
"summonerId9": data["participantIdentities"][8]["player"]["summonerId"],
"accountId9": data["participantIdentities"][8]["player"]["accountId"],
"champ10": ChampId2Name(data["participants"][9]["championId"], champHash),
"lane10": data["participants"][9]["timeline"]["lane"],
"Name10": data["participantIdentities"][9]["player"]["summonerName"],
"summonerId10": data["participantIdentities"][9]["player"]["summonerId"],
"accountId10": data["participantIdentities"][9]["player"]["accountId"],
"team1win": game_result}
return row
except KeyError:
print("Error: " + str(data["status"]["status_code"]) + ". Reason: " + data["status"]["message"])
except :
print("Bad Request")
"""
Brute Force Crawler for match information
params:
initialId: id in which the search will begain
API_KEY
N: number of id to search from
"""
def MatchCrawler(initialId, API_KEY, N, M):
print("Retriving Champion ID data...")
champDict = ChampionIdHash()
dataList = []
print("Retriving matches data:")
matchId = initialId
for j in range(0, M):
for i in range(0, N):
print("Retriving matcheId: %d, (%d*%d)" % (matchId, i, j))
row = FetchMatchData(matchId, API_KEY, champDict)
if row != None:
dataList.append(row)
#random last 3 games
nextParticipantId = rd.randint(1, 11)
readable = False
while not readable:
nextAccountId = row["accountId"+ str(nextParticipantId)]
matchlists = __FetchMatchList(nextAccountId, API_KEY)
try:
nextGameId = rd.randint(0, min(10, len(matchlists["matches"])))
print("random values for player: %d and match: %d" % (nextParticipantId, nextGameId))
matchId = matchlists["matches"][nextGameId]["gameId"]
readable = True
except KeyError:
nextParticipantId = nextParticipantId % 10 + 1
else:
matchId = rd.randint(1796377995, 1796387995)
df = pd.DataFrame(dataList)
df.to_csv("data/match-list.csv", mode='a', header=False)
dataList = []
def championName(champ_name):
switcher={
"Nunu": "Nunu & Willump",
"MissFortune" : "Miss Fortune",
"XinZhao" : "Xin Zhao",
"Velkoz" : "Vel'Koz",
"LeeSin" : "Lee Sin",
'AurelionSol': 'Aurelion Sol',
'Chogath' : 'Cho\'Gath',
'DrMundo' : 'Dr. Mundo',
'JarvanIV' : 'Jarvan IV',
'Kaisa' : 'Kai\'Sa',
'Khazix' : 'Kha\'Zix',
'KogMaw' : 'Kog\'Maw',
'Leblanc' : 'LeBlanc',
'MasterYi' : 'Master Yi',
'RekSai' : 'Rek\'Sai',
'TahmKench' : 'Tahm Kench',
'TwistedFate' : 'Twisted Fate'
}
return switcher.get(champ_name, champ_name)
def DataCleaning(path):
dataset = pd.read_csv(path)
dataset.drop_duplicates(subset = "matchId", inplace = True)
f = open('./data/champions-stats.Pickle', 'rb')
champions_stats = pickle.load(f)
toDrop = []
for row_index,row in dataset.iterrows():
for i in range(1,11):
champ_name = championName(row["champ" + str(i)])
if(champ_name == None):
toDrop.append(row_index)
break;
try:
role = getChampion(champ_name, champions_stats).role
if role == "Bottom" or role == "Support":
if row["lane" + str(i)] != "BOTTOM":
toDrop.append(row_index)
break;
elif role.lower() != row["lane" + str(i)].lower():
toDrop.append(row_index)
break;
except :
print("%s has not enough games for statistics", champ_name)
toDrop.append(row_index)
break;
dataset.drop(toDrop, inplace = True)
dataset.drop(labels = 'Unnamed: 0', axis = 1, inplace = True)
dataset.to_csv("data/match-list-clean.csv")
#MatchCrawler(1796379999, API_KEY = "RGAPI-6b1ea488-6b90-467e-a4da-519d5a3aab2d",N = 100, M=10000)
#DataCleaning("./data/match-list.csv")