-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRiot_match_collector.py
170 lines (128 loc) · 5.74 KB
/
Riot_match_collector.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
import requests
import time
import random
import json
#from google.colab import files
def get_leagues(platform, api):
"""
Returns a list of 200 summonerNames from master, grandmaster and challenger leagues in the given platform.
Inputs:
- platform: One of these values ['BR1', 'eun1', 'EUW1', 'JP1', 'KR', 'LA1', 'LA2', 'NA1', 'OC1', 'RU', 'TR1']
- api: Your api key.
Output:
- summonerName: List of summoner Names in the given platform.
"""
leagues = ['challengerleagues', 'grandmasterleagues', 'masterleagues']
url_challenger = 'https://' + platform + '.api.riotgames.com/lol/league/v4/' + leagues[
0] + '/by-queue/RANKED_SOLO_5x5?api_key=' + api
response = requests.get(url_challenger)
lis = response.json()['entries']
summonerName_c = [sub['summonerName'] for sub in lis[0:67]]
url_grandmaster = 'https://' + platform + '.api.riotgames.com/lol/league/v4/' + leagues[
1] + '/by-queue/RANKED_SOLO_5x5?api_key=' + api
response = requests.get(url_grandmaster)
lis = response.json()['entries']
summonerName_g = [sub['summonerName'] for sub in lis[0:67]]
url_master = 'https://' + platform + '.api.riotgames.com/lol/league/v4/' + leagues[
2] + '/by-queue/RANKED_SOLO_5x5?api_key=' + api
response = requests.get(url_master)
lis = response.json()['entries']
summonerName_m = [sub['summonerName'] for sub in lis[0:66]]
summonerName = summonerName_c + summonerName_g + summonerName_m
return summonerName
def get_puuid(platform, api, summonerNames):
"""
Returns a list of the puuids of each summoner of the given list of summoner names in a given platform.
Inputs:
- platform: One of these values ['BR1', 'eun1', 'EUW1', 'JP1', 'KR', 'LA1', 'LA2', 'NA1', 'OC1', 'RU', 'TR1']
- api: Your api key.
- summonerNames: A list of summoner names in a given platform.
Output:
- puuid_list: A list of puuids of the summoners.
"""
puuid_list = []
for name in summonerNames:
url = 'https://' + platform.lower() + '.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + name + '?api_key=' + api
response = requests.get(url).json()
# If any error happens, skip that summoner
if response.get('status'):
continue
puuid = response['puuid']
puuid_list.append(puuid)
print(len(puuid_list))
return puuid_list
def get_matchId(region, api, puuids):
"""
Returns a list of match ids using the given list of puuids of summoners in a specific region.
Inputs:
- region: 'americas', 'asia' or 'europe'.
- api: Your api key.
- puuids: A list of puuids of summoners.
Output:
- matchId_list: A list of matches played by these summoners. 10 matches are retrieved for each summoner.
"""
matchId_list = []
for puuid in puuids:
# Get the ids of 10 matches played by that summoner with the puuid
url = 'https://' + region + '.api.riotgames.com/lol/match/v5/matches/by-puuid/' + puuid + '/ids?start=0&count=10&api_key=' + api
response = requests.get(url).json()
# If rate limit is exceeded
if isinstance(response, dict):
print(response['status']['message'])
# sleep for 2 minutes
time.sleep(120)
# then request again
url = 'https://' + region + '.api.riotgames.com/lol/match/v5/matches/by-puuid/' + puuid + '/ids?start=0&count=10&api_key=' + api
response = requests.get(url).json()
matchId_list.extend(response)
return matchId_list
def get_matches(region, api, matchIds):
"""
Get 1,000 random matches from the given matchIds list.
Inputs:
- region: 'americas', 'asia' or 'europe'.
- api: Your api key.
- matchIds: A list of matches played by summoners.
Output:
- matches: A dictionary containing 1000 matches.
"""
matches = []
i = 0
random_indices = random.sample(range(0, len(matchIds) - 1), 1000)
for ind in random_indices:
time.sleep(0.01)
url = 'https://' + region + '.api.riotgames.com/lol/match/v5/matches/' + matchIds[ind] + '?api_key=' + api
response = requests.get(url).json()
# If rate limit is exceeded
while response.get('status'):
print(response['status']['message'])
time.sleep(40)
url = 'https://' + region + '.api.riotgames.com/lol/match/v5/matches/' + matchIds[ind] + '?api_key=' + api
response = requests.get(url).json()
i += 1
print("Step = {}".format(i))
matches.append(response)
return matches
# This function wraps all of the above functions
def getMatchesFromPlatform(region, platform, api):
"""
Get 1,000 random matches from the given platform, saves it in a file and dowlnoads it.
Inputs:
- region: 'americas', 'asia' or 'europe'.
- platform: One of these values ['BR1', 'eun1', 'EUW1', 'JP1', 'KR', 'LA1', 'LA2', 'NA1', 'OC1', 'RU', 'TR1']
- api: Your api key.
Output:
- matches: A dictionary containing 1000 matches.
"""
summonerNames = get_leagues(platform, api)
puuids = get_puuid(platform, api, summonerNames)
match_ids = get_matchId(region, api, puuids)
matches = get_matches(region, api, match_ids)
with open('matches', 'w') as f:
json.dump(matches, f)
#files.download("matches")
if __name__ == '__main__':
api = 'YOUR API KEY'
platform = ['BR1', 'eun1', 'EUW1', 'JP1', 'KR', 'LA1', 'LA2', 'NA1', 'OC1', 'RU', 'TR1']
region = ['americas', 'asia', 'europe']
matches = getMatchesFromPlatform(region[0], platform[0], api)