-
Notifications
You must be signed in to change notification settings - Fork 0
/
msf_handlers.py
94 lines (66 loc) · 2.66 KB
/
msf_handlers.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
import os
import requests
import base64
def get_team_games(api_key, season, team_id):
response = requests.get(
url='https://api.mysportsfeeds.com/v2.1/pull/nba/{}/games.json?team={}'.format(season, team_id),
headers={
"Authorization": "Basic " + base64.b64encode(
'{}:{}'.format(api_key, 'MYSPORTSFEEDS').encode('utf-8')).decode('ascii')
}
)
return response.json()['games']
def get_game(api_key, season, game_id):
response = requests.get(
url='https://api.mysportsfeeds.com/v2.1/pull/nba/{}/games/{}/boxscore.json'.format(season, game_id),
headers={
"Authorization": "Basic " + base64.b64encode(
'{}:{}'.format(api_key, 'MYSPORTSFEEDS').encode('utf-8')).decode('ascii')
}
)
return response.json()
def get_standings(api_key, season):
response = requests.get(
url='https://api.mysportsfeeds.com/v2.1/pull/nba/{}/standings.json?stats=none'.format(season),
headers={
"Authorization": "Basic " + base64.b64encode(
'{}:{}'.format(api_key, 'MYSPORTSFEEDS').encode('utf-8')).decode('ascii')
}
)
return response.json()
def sort_teams(teams):
east_teams = []
west_teams = []
for team in teams['teams']:
if team['conferenceRank']['conferenceName'] == 'Eastern':
east_teams.append(team)
else:
west_teams.append(team)
east_sorted = sorted(east_teams, key=lambda team_entry: team_entry['conferenceRank']['rank'])
west_sorted = sorted(west_teams, key=lambda team_entry: team_entry['conferenceRank']['rank'])
return {
"east": east_sorted,
"west": west_sorted
}
def get_teams_season_stats(api_key, season):
response = requests.get(
url='https://api.mysportsfeeds.com/v2.1/pull/nba/{}/team_stats_totals.json'.format(season),
headers={
"Authorization": "Basic " + base64.b64encode(
'{}:{}'.format(api_key, 'MYSPORTSFEEDS').encode('utf-8')).decode('ascii')
}
)
return response.json()
def get_team_games_handler(event, context):
api_key = os.getenv('api_key')
return get_team_games(api_key, event['season'], event['teamid'])
def get_game_handler(event, context):
api_key = os.getenv('api_key')
return get_game(api_key, event['season'], event['gameid'])
def standings_handler(event, context):
api_key = os.getenv('api_key')
team_data = get_standings(api_key, event['season'])
return sort_teams(team_data)
def get_team_season_stats_handler(event, context):
api_key = os.getenv('api_key')
return get_teams_season_stats(api_key, event['season'])