-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_data.py
252 lines (195 loc) · 8.04 KB
/
api_data.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
import requests
import time
from tqdm import tqdm
from dotenv import load_dotenv
import os
from mysql_connector import *
# loading api_key from .env file
load_dotenv()
api_key = os.getenv('api_key')
# Start = 0 means we fetch count newest matches.
start = '0'
# Count is the amount of mathces we want to fetch per player
count = '20'
connection = connector()
cursor = connection.cursor()
# The following functions' purpose is to get the puu-id of summoners from Master - Challenger rank on the EU West server to get their match data.
def all_master_rank_summoners():
# api tft-league-v1
url_master = 'https://euw1.api.riotgames.com/tft/league/v1/master?queue=RANKED_TFT' + '&api_key=' + api_key
response_master = requests.get(url_master)
player_info_master = response_master.json()
summoner_master_data = player_info_master.get('entries', [])
for entry in summoner_master_data:
# renaming rank to identify master players
entry['rank'] = 'master'
return summoner_master_data
'''
Output:
{
"summonerId": "lgqyzaz7HhAIWKAP5AIJ05IQkC0NmDYbFfRaAjEdv2FPocg",
"summonerName": "I am goodder",
"leaguePoints": 779,
"rank": "master",
"wins": 98,
"losses": 63,
"veteran": false,
"inactive": false,
"freshBlood": true,
"hotStreak": false
}
'''
def all_grandmaster_rank_summoners():
# api tft-league-v1
url_grandmaster = 'https://euw1.api.riotgames.com/tft/league/v1/grandmaster?queue=RANKED_TFT' + '&api_key=' + api_key
response_grandmaster = requests.get(url_grandmaster)
player_info_grandmaster = response_grandmaster.json()
summonder_grandmaster_data = player_info_grandmaster.get('entries', [])
for entry in summonder_grandmaster_data:
# renaming rank to identify grandmaster players
entry['rank'] = 'grandmaster'
return summonder_grandmaster_data
'''
Output:
{
"summonerId": "lgqyzaz7HhAIWKAP5AIJ05IQkC0NmDYbFfRaAjEdv2FPocg",
"summonerName": "I am goodder",
"leaguePoints": 779,
"rank": "grandmaster",
"wins": 98,
"losses": 63,
"veteran": false,
"inactive": false,
"freshBlood": true,
"hotStreak": false
}
'''
def all_challenger_rank_summoner():
# api tft-league.v1
url_challenger = 'https://euw1.api.riotgames.com/tft/league/v1/challenger?queue=RANKED_TFT' + '&api_key=' + api_key
response_challenger = requests.get(url_challenger)
player_info_challenger = response_challenger.json()
summoner_challenger_data = player_info_challenger.get('entries', [])
for entry in summoner_challenger_data:
# renaming rank to identify challenger players
entry['rank'] = 'challenger'
return summoner_challenger_data
'''
Output:
{
"summonerId": "lgqyzaz7HhAIWKAP5AIJ05IQkC0NmDYbFfRaAjEdv2FPocg",
"summonerName": "I am goodder",
"leaguePoints": 779,
"rank": "challenger",
"wins": 98,
"losses": 63,
"veteran": false,
"inactive": false,
"freshBlood": true,
"hotStreak": false
}
'''
def all_summoners():
# Joining all summoner ids from master, grandmaster and challenger rank together
summoner_data = (all_master_rank_summoners() +
all_grandmaster_rank_summoners() +
all_challenger_rank_summoner()
)
ingest_summoner_data(summoner_data, connection, cursor)
return summoner_data
def get_player_info(summoner_id):
# Getting the player_info of a summoner. This contains the puu-id, which is the global id of player unlike summoner id.
# API tft-summoner-v1
url_player_info = f'https://euw1.api.riotgames.com/tft/summoner/v1/summoners/{summoner_id}' + '?api_key=' + api_key
response_player_info = requests.get(url_player_info)
player_info = response_player_info.json()
return player_info
'''
output:
{
"id": "lgqyzaz7HhAIWKAP5AIJ05IQkC0NmDYbFfRaAjEdv2FPocg",
"accountId": "muRjLXzG607QJLQII3F2TzVgBQ-vJ_KGKlrAOO23nDTRp6o",
"puuid": "eQUrCU42YEFk3EvrdACEirvVlhe4zT2kn9o2rBmSl7RLkmXPabL6Fkp4CULXTlSYssh4aGFTEKIukw",
"name": "I am goodder",
"profileIconId": 3398,
"revisionDate": 1700329066000,
"summonerLevel": 450
}
'''
def get_all_player_info(cursor):
# Getting player info for all summoner_ids.
cursor.execute('SELECT summoner_id FROM game_database.summoners')
summoner_ids = cursor.fetchall()
# summoner_ids = [entry['summonerId'] for entry in summoner_data]
player_data = []
for id in tqdm(summoner_ids, desc = 'Fetching player data'):
player_info = get_player_info(id)
player_data.append(player_info)
# The Riot api only allows 100 requests every 2 minutes.
time.sleep(1.2)
# TODO This is just for testing since the for loop takes hours to complete due to the API's rate limit, and amount of ids.
if len(player_data) >= 20:
break
ingest_player_info(player_data, connection, cursor)
return player_data
# The following functions are to fetch the match data of the puu-ids.
def get_match_id(puuid):
# api tft-match-v1
url_match_ids = f'https://europe.api.riotgames.com/tft/match/v1/matches/by-puuid/{puuid}' + '/ids?start=' + start + '&count=' + count + '&api_key=' + api_key
response_match_ids = requests.get(url_match_ids)
match_ids = response_match_ids.json()
return match_ids
'''
output:
[
"EUW1_6681384040",
"EUW1_6681331522",
"EUW1_6681284110",
"EUW1_6668355988",
"EUW1_6668281520",
"EUW1_6668217716",
"EUW1_6668150430"
]
'''
def get_all_match_ids(cursor):
cursor.execute('SELECT puu_id FROM game_database.player_info')
puuids = cursor.fetchall()
# player_data = get_all_player_info()
# puuids = [entry['puuid'] for entry in player_data]
unique_match_ids = set()
for id in tqdm(puuids, desc = 'Fetching match ids'):
match_id = get_match_id(id)
unique_match_ids.update(match_id)
# The Riot api only allows 100 requests every 2 minutes.
time.sleep(1.2)
# TODO This is just for testing since the for loop takes hours to complete due to the API's rate limit, and amount of ids.
if len(unique_match_ids) >= 100:
break
return unique_match_ids
def get_match_data(match_id):
url_match_data = 'https://europe.api.riotgames.com/tft/match/v1/matches/' + match_id + '?api_key=' + api_key
response_match_data = requests.get(url_match_data)
match_data = response_match_data.json()
return match_data
def get_all_match_data():
match_ids = get_all_match_ids(cursor)
match_data = []
for id in tqdm(match_ids, desc = 'Fetching match data'):
fetch_match_data = get_match_data(id)
match_data.append(fetch_match_data)
# The Riot api only allows 100 requests every 2 minutes.
time.sleep(1.2)
# TODO This is just for testing since the for loop takes hours to complete due to the API's rate limit, and amount of ids.
if len(match_data) >= 20:
break
ingest_metadata(match_data, connection, cursor)
ingest_participant_data(match_data, connection, cursor)
return match_data
# The output is things like players in a match, their placement, traits, augments etc.
#create_drop_schema(connection, cursor)
#create_tables(connection, cursor)
#all_summoners()
#get_all_player_info(cursor)
#get_all_match_data()
#cursor.close()
#connection.close()