-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
121 lines (94 loc) · 3.99 KB
/
api.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
import json
import requests
fd_url = "https://www.football-data.org"
invalid_api_message = ("You must have an API token."
" Get one from {}.".format(fd_url))
class InvalidAPIKey(Exception):
pass
class RestrictedResource(Exception):
pass
class TooManyRequests(Exception):
pass
class ResourceNotFound(Exception):
pass
class BadRequest(Exception):
pass
def check_response(response):
code = response.status_code
if code == 429:
throttling_url = 'https://www.football-data.org/documentation/api#request-throttling'
raise TooManyRequests('You have exceeded request quote. See {}'.format(throttling_url))
if code == 403:
raise RestrictedResource('Resource is restricted.')
if code == 404:
raise ResourceNotFound('You tried to access resource that doesn\'t exist')
if code == 400:
raise BadRequest('Your request was malformed')
class FootballData:
def __init__(self, auth_token=None):
with open('config.json') as f:
self.config = json.load(f)
self.base_url = self.config.get('base_url')
if auth_token:
self.auth_token = auth_token
else:
self.auth_token = self.config.get('auth_token')
if self.auth_token == 'YOUR_AUTH_TOKEN':
raise InvalidAPIKey(invalid_api_message)
self.headers = {'X-Auth-Token': self.auth_token}
def get_competitions(self):
rv = requests.get(self.base_url + 'competitions/', headers=self.headers)
check_response(rv)
return rv.json()
def get_competition(self, id):
rv = requests.get(self.base_url + 'competitions/' + str(id),
headers=self.headers)
check_response(rv)
return rv.json()
def get_competition_teams(self, id, season=None):
rv = requests.get(self.base_url + 'competitions/' + str(id) + '/teams',
params={'season': season},
headers=self.headers)
check_response(rv)
return rv.json()
def get_competition_standings(self, id):
rv = requests.get(self.base_url + 'competitions/' + str(id) + '/standings',
headers=self.headers)
check_response(rv)
return rv.json()['standings'][0]['table']
def get_competition_scorers(self, id):
rv = requests.get(self.base_url + 'competitions/' + str(id) + '/scorers',
headers=self.headers)
check_response(rv)
return rv.json()
def get_competition_matches(self, id, status=None, stage=None,
group=None, date_from=None, date_to=None):
rv = requests.get(self.base_url + 'competitions/' + str(id) + '/matches',
params=dict(status=status,
stage=stage,
group=group,
dateFrom=date_from,
dateTo=date_to),
headers=self.headers)
check_response(rv)
return rv.json()['matches']
def get_match(self, competitions=None, status=None,
stage=None, group=None, date_from=None, date_to=None):
rv = requests.get(self.base_url + 'matches',
params=dict(competitions=competitions,
status=status,
stage=stage,
group=group,
dateFrom=date_from,
dateTo=date_to),
headers=self.headers)
check_response(rv)
return rv.json()
def get_teams(self, competition=None):
if competition:
url = 'competitions/' + str(competition) + '/teams'
else:
url = '/teams'
rv = requests.get(self.base_url + url,
headers=self.headers)
return rv.json()