-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.py
321 lines (261 loc) · 10.3 KB
/
base.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
"""
Base API for SportMonks.
API information: https://www.sportmonks.com/products/soccer
"""
import os
import logging
from datetime import datetime
import time
from typing import Dict, Optional, Union, List, Any
import numpy as np
import pandas as pd
import requests
import pytz
from errors import (
BadRequest,
UnathourizedRequest,
APIPermissionError,
APINotFound,
TooManyRequests,
ServerErrors,
APIKeyMissing,
NotJSONNormalizable
)
import helper
log = helper.setup_logger(__name__, "SM_API.log")
class BaseAPI(object):
"""Base API for SportMonks"""
def __init__(self, api_key: str = None, timeout: Optional[int] = None,
tz: Optional[str] = None):
"""
Args:
api_key:
api_key for SportMonks subscription.
timeout:
number of seconds to wait before a response from API.
tz:
timezone
"""
self.url = "https://soccer.sportmonks.com/api/v2.0/"
self.api_key = api_key
self.timeout = timeout
if tz:
self.tz = tz
else:
self.tz = datetime.now().astimezone().tzinfo
self.get_key()
self.initial_params = {"api_token": self.api_key, "tz": self.tz}
self.meta_info()
if tz:
self.tz = tz
else:
self.tz = datetime.now().astimezone().tzinfo
def get_key(self):
"""
If no api_key is specified, then look in environment variables
for the key called "SPORTMONKS_KEY".
"""
if self.api_key is None:
if os.environ.get("SPORTMONKS_KEY"):
self.api_key = os.environ.get("SPORTMONKS_KEY")
else:
raise APIKeyMissing("Make an environment variable named 'SPORTMONKS_KEY' \
to store your api key")
def meta_info(self):
"""Returns meta info from your SportMonks plan."""
r = requests.get(self.create_api_url(endpoint="continents"),
params=self.initial_params)
if r.status_code == 200:
r = r.json()
log.info("r: %s", r)
plan = r.get("meta").get("plan")
if plan:
self.plan_name = plan.get("name")
self.plan_price = "\u20ac" + plan.get("price")
limit, mins = plan.get("request_limit").split(",")
self.request_limit = f"{limit} requests per {mins} minutes."
else:
log.info("Could not retrieve plan info: %s", r.status_code)
return None
@property
def headers(self):
"""Headers"""
return {"Content-Type": "application/json",
"Accept": "application/json",
"Accept-Encoding": "deflate, gzip",
"Date": f"{datetime.now()}"}
def __unnest_includes(self, dictionary: dict):
"""
Changes the SportMonks API response to get rid of the
superfluous "data" that is included inside an includes
response.
{...,
"season": {
"data": {
"id": 16216,
"name": "2019/2020",
"league_id": 27,
"is_current_season": true,
"current_round_id": null,
"current_stage_id": 77444688
}
}
}
Prefer the following:
{...,
"season": {
"id": 16216,
"name": "2019/2020",
"league_id": 27,
"is_current_season": true,
"current_round_id": null,
"current_stage_id": 77444688
}
}
"""
unnested = dict()
for key in dictionary:
if isinstance(dictionary[key], dict) and list(dictionary[key].keys()) == ["data"]:
data = dictionary[key]["data"]
if isinstance(data, list):
for i, v in enumerate(data):
if isinstance(v, dict):
data[i] = self.__unnest_includes(v)
elif isinstance(data, dict):
data = self.__unnest_includes(data)
unnested[key] = data
else:
unnested[key] = dictionary[key]
return unnested
def create_api_url(self, endpoint: Union[str, int, List[Union[str, int]]]):
"""
Creates API URL for different endpoints.
Excludes paramaters which are passed in to request.get().
"""
endpoint = [endpoint] if isinstance(endpoint, (str, int)) else endpoint
return self.url + "/".join(list(map(str, endpoint)))
@staticmethod
def process_includes(includes: Union[str, List[str]]):
"""
includes must be in the form of a string,
with the include separated by a comma.
"""
includes = [includes] if isinstance(includes, str) else includes
return ",".join(includes)
@staticmethod
def process_params(params: dict):
"""
Processes the paramaters ready to be put in to the query string.
"""
for key in params.keys():
if isinstance(params[key], list):
params[key] = ",".join(list(map(str, params[key])))
return params
def make_request(self, endpoint: Union[str, List[str]],
includes: Optional[List[str]] = None,
params: Optional[dict] = None,
filters: Optional[dict] = None):
"""Make a GET reqeust to SportMonks API"""
if params:
params = self.process_params(params)
params.update(self.initial_params)
else:
params = self.initial_params
if filters:
filters = self.process_params(filters)
params.update(filters)
if includes:
includes = self.process_includes(includes)
params["include"] = includes
if "page" not in params:
params["page"] = 1
log.info("Params: %s", params)
url = self.create_api_url(endpoint=endpoint)
try:
r = requests.get(url, params=params, headers=self.headers,
timeout=self.timeout)
log.info("URL: %s", r.url)
except requests.exceptions.Timeout as e:
log.info("Response has timed out: %s", e)
raise SystemExit(e)
# recursion here?
try:
response = r.json()
log.info("r: %s", response)
except ValueError as e:
log.info("Could not decode response in to JSON: %s", e)
raise SystemExit(e)
log.info("status code: %s", r.status_code)
if "error" in response:
error_message = response["error"].get("message")
log.error("Error: %s", error_message)
if r.status_code == 400:
raise BadRequest(f"Bad Request Error, reason: {error_message}")
elif r.status_code == 401:
raise UnathourizedRequest(f"Invalid API Key, reason: {error_message}")
elif r.status_code == 403:
raise APIPermissionError(f"Permission error, reason: {error_message}")
elif r.status_code == 404:
raise APINotFound(f"There is no content, reason: {error_message}")
elif r.status_code == 429:
raise TooManyRequests(f"You have reached your request limit, reason: \
{error_message}")
elif r.status_code in [500, 502, 503, 504]:
raise ServerErrors(f"Server errors, reason: {error_message}")
data = response.get("data")
if not data:
log.error("No data was included!")
raise SystemExit("No data available. No fixtures in that time-frame.")
if ("meta" in response) and ("pagination" in response.get("meta")):
total_pages = response["meta"]["pagination"].get("total_pages")
log.info("Response is paginated; %s pages", total_pages)
for page in range(2, total_pages + 1):
params["page"] = page
r = requests.get(url, params=params, headers=self.headers,
timeout=self.timeout)
log.info("URL: %s", r.url)
next_page_data = r.json().get("data")
if next_page_data:
data += next_page_data
if isinstance(data, dict):
data = self.__unnest_includes(data)
elif isinstance(data, list):
data = [self.__unnest_includes(d) for d in data]
else:
raise TypeError(f"Did not expect response of type: {type(data)}")
return data
@classmethod
def __is_normalizable(cls, response: dict):
"""
Is the response JSON normalizable?
json_normalize only handels nested dictionaries,
if the values in the dicts are lists then they won't be unnested.
"""
for key in response:
if isinstance(response[key], list):
log.debug("This key has a list value: %s", key)
return False
elif isinstance(response[key], dict):
cls.__is_normalizable(response[key])
return True
def _to_df(self, response: Union[dict, List[dict]],
cols: Optional[Union[str, List[str]]] = None):
"""Transforms JSON API reponse to a pandas DataFrame"""
if isinstance(response, dict):
if not self.__is_normalizable(response):
raise NotJSONNormalizable("Response is not JSON-normalizable.")
elif isinstance(response, list):
if not all(self.__is_normalizable(fixt) for fixt in response):
raise NotJSONNormalizable("Response is not JSON-normalizable.")
df = pd.json_normalize(response)
if cols:
try:
df = df[cols]
except KeyError as e:
log.info("No key, value pair for column: %s", e)
missing_keys = set(cols).difference(df.columns)
log.info("Missing keys: %s", missing_keys)
for key in missing_keys:
df[key] = np.NaN
df = df[cols]
return df