forked from pittcsc/PittAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sports.py
150 lines (123 loc) · 5.59 KB
/
sports.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
"""
The Pitt API, to access workable data of the University of Pittsburgh
Copyright (C) 2015 Ritwik Gupta
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import requests
FOOTBALL_URL = "http:https://site.api.espn.com/apis/site/v2/sports/football/college-football/teams/pitt"
MENS_BASKETBALL_URL = "http:https://site.api.espn.com/apis/site/v2/sports/basketball/mens-college-basketball/teams/pittsburgh"
def get_mens_basketball_record() -> str:
"""returns the current record of the men's basketball team"""
basketball_data = _get_mens_basketball_data()
try:
record_summary = basketball_data["team"]["record"]["items"][0]["summary"]
except KeyError:
record_summary = "There's no record right now."
return record_summary
def get_next_mens_basketball_game() -> dict:
"""returns a dict containing details of the next scheduled men's basketball game."""
basketball_data = _get_mens_basketball_data()
next_game = None
try:
next_game = basketball_data["team"]["nextEvent"][0]
opponent = None
homeaway = None
status = None
if next_game["competitions"][0]["status"]["type"]["name"] == "STATUS_FINAL":
status = "GAME_COMPLETE"
elif next_game["competitions"][0]["status"]["type"]["name"] == "STATUS_IN_PROGRESS":
status = "IN_PROGRESS"
if next_game["competitions"][0]["competitors"][0]["id"] == 221:
opponent = next_game["competitions"][0]["competitors"][0]
homeaway = next_game["competitions"][0]["competitors"][0]["homeAway"]
else:
opponent = next_game["competitions"][0]["competitors"][1]
homeaway = next_game["competitions"][0]["competitors"][1]["homeAway"]
return {
"timestamp" : next_game["date"],
"opponent" : {
"id" : opponent["team"]["id"],
"school" : opponent["team"]["nickname"],
"name" : opponent["team"]["displayName"]
},
"home_away" : homeaway,
"location" : {
"full_name" : next_game["competitions"][0]["venue"]["fullName"],
"address" : next_game["competitions"][0]["venue"]["address"]
},
"status" : status
}
except IndexError:
# IndexError occurs when a next game on the schedule is not present
return {
"status" : "NO_GAME_SCHEDULED"
}
def get_mens_basketball_standings() -> str:
"""returns a string describing the placement of the men's basketball team. eg: '14th in ACC' """
basketball_data = _get_mens_basketball_data()
return_value = basketball_data["team"]["standingSummary"]
return return_value
def get_football_record() -> str:
"""returns the current record of the men's football team"""
football_data = _get_football_data()
try:
record_summary = football_data["team"]["record"]["items"][0]["summary"]
except KeyError:
record_summary = "There's no record right now."
return record_summary
def get_next_football_game() -> dict:
football_data = _get_football_data()
next_game = None
try:
next_game = football_data["team"]["nextEvent"][0]
opponent = None
homeaway = None
status = None
if next_game["competitions"][0]["status"]["type"]["name"] == "STATUS_FINAL":
status = "GAME_COMPLETE"
elif next_game["competitions"][0]["status"]["type"]["name"] == "STATUS_IN_PROGRESS":
status = "IN_PROGRESS"
if next_game["competitions"][0]["competitors"][0]["id"] == 221:
opponent = next_game["competitions"][0]["competitors"][1]
homeaway = next_game["competitions"][0]["competitors"][0]["homeAway"]
else:
opponent = next_game["competitions"][0]["competitors"][0]
homeaway = next_game["competitions"][0]["competitors"][1]["homeAway"]
return {
"timestamp" : next_game["date"],
"opponent" : {
"id" : opponent["team"]["id"],
"school" : opponent["team"]["nickname"],
"name" : opponent["team"]["displayName"]
},
"home_away" : homeaway,
"location" : {
"full_name" : next_game["competitions"][0]["venue"]["fullName"],
"address" : next_game["competitions"][0]["venue"]["address"]
},
"status" : status
}
except IndexError:
# IndexError occurs when a next game on the schedule is not present
return {
"status" : "NO_GAME_SCHEDULED"
}
def get_football_standings() -> str:
"""returns a string describing the placement of the football team. eg: '14th in ACC' """
football_data = _get_football_data()
return_value = football_data["team"]["standingSummary"]
return return_value
def _get_mens_basketball_data() -> dict:
return requests.get(MENS_BASKETBALL_URL).json()
def _get_football_data() -> dict:
return requests.get(FOOTBALL_URL).json()