-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChampionsLeagueSquads.py
57 lines (42 loc) · 1.71 KB
/
ChampionsLeagueSquads.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
import http.client
import json
import time
import csv
connection = http.client.HTTPConnection('api.football-data.org')
headers = {'X-Auth-Token': 'fb98f620df7a4def92738e2f7bb95d80'}
connection.request('GET', '/v2/competitions/2001/matches', None, headers)
response = json.loads(connection.getresponse().read().decode())
matches = response['matches']
team_ids = []
for match in matches:
if match['stage'] == 'LEAGUE_STAGE':
match_day = match['matchday']
date = match['utcDate']
home_team = match['homeTeam']['name']
away_team = match['awayTeam']['name']
print(f'{match_day},{date},{home_team},{away_team}')
home_team_id = match['homeTeam']['id']
away_team_id = match['awayTeam']['id']
if home_team_id not in team_ids:
team_ids.append(home_team_id)
if away_team_id not in team_ids:
team_ids.append(away_team_id)
with open('/Users/hagrubma/UCLSquads.csv', 'w') as squads_file:
squads_file_writer = csv.writer(squads_file)
request_counter = 1
for team_id in team_ids:
if request_counter % 10 == 0:
time.sleep(60)
connection.request('GET', '/v2/teams/' + str(team_id), None, headers)
team_response = json.loads(connection.getresponse().read().decode())
request_counter += 1
team_name = team_response['name']
team_players = team_response['squad']
if len(team_players) == 0:
print('No squad for ' + team_name)
continue
for team_player in team_players:
player_name = team_player['name']
squads_file_writer.writerow([team_name, player_name])
print('Found squad for ' + team_name)
print('Done')