-
Notifications
You must be signed in to change notification settings - Fork 3
/
sportmonks_calls.py
668 lines (532 loc) · 21.5 KB
/
sportmonks_calls.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#!/usr/bin/python
import requests
import json
import time
from collections import OrderedDict###### LIST OF ENDPOINTS FROM SPORTMONKS #######################################
'''
// Continents
// Countries
// Leagues
// Seasons
// Fixtures
// Livescores
// Commentaries
// Video Highlights
// Head2Head
// TVStations
// Standings
// Teams
// Topscorers
// Venues
// Rounds
// Pre-Match-Odds
// In-Play-Odds
// Players
// Bookmakers
// Team-Squads
'''
#### maybe i need to map all the includes for proper parsing
includes = {
"visitorTeam": ['country_id','founded','id', 'legacy_id','logo_path','name','national_team','twitter','venue_id'],
"localTeam": ['country_id','founded','id', 'legacy_id','logo_path','name','national_team','twitter','venue_id'],
"goals": ['extra_minute','fixture_id','id','minute','player_assist_id','player_assist_name','player_id','player_name','result','team_id','type'],
"stats": ['attacks','corners','fixture_id','fouls','free_kick','goal_kick','offsides','passes','possessiontime','redcards','saves','shots','substitutions','team_id','throw_in','yellowcards'],
"corners": ['comment','extra_minute','fixture_id','id','minute','team_id'],
"bench": ['additional_position','fixture_id','formation_position','number','player_id','player_name','position','posx','posy','stats','team_id'],
"localCoach": [],
"visitorCoach": [],
"group": [],
"groups": [],
"odds": [],
"venue": [],
"events": [],
"referee": [],
"stage": [],
"round": ['end','id','league_id','name','season_id','stage_id','start'],
"season": ['current_round_id','current_stage_id','id','is_current_season','league_id','name'],
"league": [],
"highlights": [],
"tvstations": [],
"comments": [],
"sidelined": [],
"lineup": [],
"other": [],
"cards": [],
"substitutions": [],
"inplay": [],
"flatOdds": [],
"coach": [],
"squad": [],
"transfers": [],
"fifaranking": [],
"uefaranking": [],
"visitorFixtures": [],
"localFixtures": [],
"latest": [],
"upcoming": [],
"visitorResults": [],
"localResults": [],
"fixtures": [],
"fixture": [],
"results": []
}
################################################################################
####debug-global-var############################################################
http = 'https://soccer.sportmonks.com/api/v2.0'
DEBUG_MODE = False
token_path = ''
with open('{0}/settings.json'.format(token_path)) as json_data:
settings = json.load(json_data)
sportmonks_token = settings['sportmonks']['api_token']
################################################################################
#global class to call SporMonks API
class Spms(object):
"""
General sportmonks request class
--------------------------------------
Methods Available:
| make_request()
"""
def __init__(self, token='', include = ''):
"""
parameters :
------------
| token: sportmonks token
| include: string with all includes for the API request
"""
self.token = token
self.include = include
self.api_calls = 0
def make_request(self, endpoint, paginated = False, verbose=DEBUG_MODE):
"""
parameters :
------------
| endpoint: endpoint for the request
| paginated: boolean. View documentation for more info
| verbose: boolean
"""
#if paginated = true, then make sure to loop through all pages
self.payload = {'api_token': self.token,
'Accept': 'application/json',
'page': 1,
'include': self.include}
if not paginated:
request = requests.get(endpoint, params=self.payload)
self.api_calls += 1
[print (self.api_calls) if verbose else None]
if request.status_code != 200:
fixture_results = {'data':'Error'}
return False
else:
fixture_results = request.json()['data']
else:
first_request = requests.get(endpoint, params=self.payload)
meta = first_request.json()
pages = meta['meta']['pagination']['total_pages']
fixture_results = []
for page in range(1,pages,1):
self.payload['page'] = page
[time.sleep(2.4) if int(pages) > 1500 else None]
[print (self.api_calls) if verbose else None]
request = requests.get(endpoint, params=self.payload)
self.api_calls += 1
fixtures = request.json()
for fixture in fixtures['data']:
fixture_results.append(fixture)
self.results = fixture_results
return True
################################################################################
class Livescores(object):
"""
Class to query livescores
-------------------------
Methods Available:
| get_all()
| get_all_today()
"""
endpoint = http + '/livescores'
includes = {
"get_all": "localTeam,visitorTeam,substitutions,goals,cards,other,corners,\
lineup,bench,sidelined,stats,comments,tvstations,highlights,\
league,season,round,stage,referee,events,venue,odds,flatOdds,\
inplay,localCoach,visitorCoach,group",
"get_all_today": "localTeam,visitorTeam,substitutions,goals,cards,other\
,corners,lineup,bench,sidelined,stats,comments,tvstations,highlights,\
league,season,round,stage,referee,events,venue,odds,inplay,\
localCoach,visitorCoach,group"
}
includes_limit = {"get_all": 10, "get_all_today": 10}
def __init__(self, token, include = ''):
self.request = Spms(token = token, include=include)
def get_all(self, include = ''):
http_request = self.endpoint +'/now'
self.request.include = include
self.request.make_request(http_request, paginated = False)
return True
def get_all_today(self, include = ''):
http_request = self.endpoint
self.request.include = include
self.request.make_request(http_request, paginated = True)
return True
class Fixtures(object):
endpoint = http + '/fixtures'
includes = {
"by_range": "localTeam, visitorTeam, substitutions, goals, cards, \
other, corners, lineup, bench, sidelined, stats, comments, tvstations, \
highlights, league, season, round, stage, referee, events, venue, odds, \
flatOdds, inplay, localCoach, visitorCoach,group",
"by_id": "localTeam, visitorTeam, substitutions, goals, cards, other, corners,\
lineup, bench, sidelined, stats, comments, tvstations, highlights, league, \
season, round, stage, referee, events, venue, odds, flatOdds, inplay, \
localCoach, visitorCoach,group",
"by_date": "localTeam, visitorTeam, substitutions, goals, cards, other, corners,\
lineup, bench, sidelined, stats, comments, tvstations, highlights, league, season,\
round, stage, referee, events, venue, odds, flatOdds, inplay, localCoach, \
visitorCoach,group"
}
includes_limit = {"by_range": 3, "by_id": 3, "by_date": 3}
def __init__(self, token, include=''):
self.request = Spms(token = token, include=include)
def by_range(self, date_start, date_end, include=''):
self.request.include = include
http_request = self.endpoint + '/between/{0}/{1}'.format(date_start, date_end)
self.request.make_request(http_request, paginated = True)
return True
def by_id(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
def by_date(self, fixture_date, include=''):
self.request.include = include
http_request = self.endpoint + '/date/{0}'.format(fixture_date)
self.request.make_request(http_request, paginated = True)
return True
class Pre_Match_Odds(object):
endpoint = http + '/odds'
includes = {
"by_fixture": "",
"by_fixture_bookmaker": "",
"by_fixture_market": ""
}
includes_limit = {"by_fixture": 0, "by_fixture_bookmaker": 0, "by_fixture_market": 0}
def __init__(self, token):
self.request = Spms(token = token, include='')
def by_fixture(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/fixture/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
def by_fixture_bookmaker(self, fixture_id, bookmaker_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}/bookmaker/{1}'.format(fixture_id, bookmaker_id)
self.request.make_request(http_request, paginated = False)
return True
def by_fixture_market(self, fixture_id, market_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}/market/{1}'.format(fixture_id, market_id)
self.request.make_request(http_request, paginated = False)
return True
class Continents(object):
endpoint = http + '/continents'
includes = {
"get_all": "countries",
"by_id": "countries"
}
includes_limit = {"get_all": 10, "by_id": 10}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = False)
return True
def by_id(self, continent_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(continent_id)
self.request.make_request(http_request, paginated = False)
return True
class Countries(object):
endpoint = http + '/countries'
includes = {
"get_all": "continent,leagues",
"by_id": "continent,leagues"
}
includes_limit = {"get_all": 2, "by_id": 2}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = True)
return True
def by_id(self, country_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(country_id)
self.request.make_request(http_request, paginated = False)
return True
class Leagues(object):
endpoint = http + '/leagues'
includes = {
"get_all": "countries,seasons,season",
"by_id": "countries,seasons,season"
}
includes_limit = {"get_all": 2, "by_id": 10}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = True)
return True
def by_id(self, league_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(league_id)
self.request.make_request(http_request, paginated = False)
return True
class Seasons(object):
endpoint = http + '/seasons'
includes = {
"get_all": "league,stages,rounds,fixtures,upcoming,results,groups",
"by_id": "league,stages,rounds,fixtures,upcoming,results,groups"
}
includes_limit = {"get_all": 2, "by_id": 10}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = True)
return True
def by_id(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
class Commentaries(object):
endpoint = http + '/commentaries'
includes = {
"by_fixture": ""
}
includes_limit = {"by_fixture": 0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_fixture(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/fixture/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
class Videos(object):
endpoint = http + '/highlights'
includes = {
"get_all":"fixture",
"by_fixture": ""
}
includes_limit = {"get_all":2,"by_fixture": 0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = True)
return True
def by_fixture(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/fixture/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
class Head2Head(object):
endpoint = http + '/head2head'
includes = {
"by_teams":"localTeam,visitorTeam,substitutions,goals,cards,other,lineup,\
bench,stats,comments,tvstations,highlights,league,season,round,stage,referee,\
events,venue"
}
includes_limit = {"by_teams":2}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_teams(self, team1_id, team2_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}/{1}'.format(team1_id, team2_id)
self.request.make_request(http_request, paginated = False)
return True
class TVStations(object):
endpoint = http + '/tvstations'
includes = {
"by_fixture":""
}
includes_limit = {"by_fixture":0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_fixture(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/fixture/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
class Standings(object):
endpoint = http + '/standings'
includes = {
"by_season":"team,league,season,round,stage",
"live_by_season":"team,league,season,round,stage"
}
includes_limit = {"by_season":2,"live_by_season":2}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_season(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/season/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
def live_by_season(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/season/live/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
class Teams(object):
endpoint = http + '/teams'
includes = {
"by_id":"country,squad,coach,transfers,sidelined,stats,venue,fifaranking,\
uefaranking,visitorFixtures,localFixtures,visitorResults,localResults,\
latest,upcoming",
"by_season":"country,squad,coach,transfers,sidelined,stats,venue,fifaranking,\
uefaranking,visitorFixtures,localFixtures,visitorResults,localResults,\
latest,upcoming"
}
includes_limit = {"by_id":3,"by_season":3}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_id(self, team_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(team_id)
self.request.make_request(http_request, paginated = False)
return True
def by_season(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/season/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
class Topscorers(object):
endpoint = http + '/topscorers'
includes = {
"by_season":"goalscorers.player,goalscorers.team"
}
includes_limit = {"by_season":3}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_season(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/season/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
class Venues(object):
endpoint = http + '/venues'
includes = {
"by_id":""
}
includes_limit = {"by_id":0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_id(self, venue_id, include=''):
self.request.include = include
http_request = self.endpoint+'/{0}'.format(venue_id)
self.request.make_request(http_request, paginated = False)
return True
class Rounds(object):
endpoint = http + '/rounds'
includes = {
"by_season":"fixtures,results,season,league",
"by_id":"fixtures,season,league"
}
includes_limit = {"by_season":2,"by_id":2}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_season(self, season_id, include=''):
self.request.include = include
http_request = self.endpoint + '/season/{0}'.format(season_id)
self.request.make_request(http_request, paginated = False)
return True
def by_id(self, round_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(round_id)
self.request.make_request(http_request, paginated = False)
return True
class In_Play_Odds(object):
#coming from bet365
endpoint = http + '/odds/inplay'
includes = {
"by_fixture":""
}
includes_limit = {"by_fixture":0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_fixture(self, fixture_id, include=''):
self.request.include = include
http_request = self.endpoint + '/fixture/{0}'.format(fixture_id)
self.request.make_request(http_request, paginated = False)
return True
class Players(object):
endpoint = http + '/players'
includes = {
"by_id":"position,team,stats,trophies.seasons,sidelined,transfers"
}
includes_limit = {"by_id":2}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_id(self, player_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(player_id)
self.request.make_request(http_request, paginated = False)
return True
class Bookmakers(object):
endpoint = http + '/bookmakers'
includes = {
"get_all":"",
"by_id":""
}
includes_limit = {"get_all":0,"by_id":0}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def get_all(self, include=''):
self.request.include = include
http_request = self.endpoint
self.request.make_request(http_request, paginated = False)
return True
def by_id(self, bookmaker_id, include=''):
self.request.include = include
http_request = self.endpoint + '/{0}'.format(bookmaker_id)
self.request.make_request(http_request, paginated = False)
return True
class Team_Squads(object):
endpoint = http + '/squad'
includes = {
"by_team_season":"player,position"
}
includes_limit = {"by_team_season":3}
def __init__(self, token, include=''):
self.request = Spms(token = token, include = '')
def by_team_season(self, season_id, team_id ,include =''):
self.request.include = include
http_request = self.endpoint + '/season/{0}/team/{1}'.format(season_id, team_id)
self.request.make_request(http_request, paginated = False)
return True
if __name__ == '__main__':
livescores = Livescores(sportmonks_token)
fixtures = Fixtures(sportmonks_token)
pre_match_odds = Pre_Match_Odds(sportmonks_token)
continents = Continents(sportmonks_token)
countries = Countries(sportmonks_token)
leagues = Leagues(sportmonks_token)
seasons = Seasons(sportmonks_token)
commentaries = Commentaries(sportmonks_token)
videos = Videos(sportmonks_token)
head2head = Head2Head(sportmonks_token)
tvstations = TVStations(sportmonks_token)
standings = Standings(sportmonks_token)
teams = Teams(sportmonks_token)
topscorers = Topscorers(sportmonks_token)
venues = Venues(sportmonks_token)
rounds = Rounds(sportmonks_token)
in_play_odds = In_Play_Odds(sportmonks_token)
players = Players(sportmonks_token)
bookmakers = Bookmakers(sportmonks_token)
team_squads = Team_Squads(sportmonks_token)