-
Notifications
You must be signed in to change notification settings - Fork 0
/
espn_api.py
196 lines (163 loc) · 5.45 KB
/
espn_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
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
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from bs4 import BeautifulSoup
URL_STR = "https://www.espn.com/nfl/scoreboard/"
def init_driver():
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
driver = webdriver.Chrome(options=options)
return driver
def load_soup_for_page(url):
driver = init_driver()
driver.get(url)
timeout = 15
try:
wait = WebDriverWait(driver, timeout, .1)
# ec = EC.presence_of_element_located((By.CLASS_NAME, 'sb-team-abbrev'))
# wait.until(ec)
html_source = driver.page_source
except TimeoutException as e:
raise e
finally:
driver.quit()
soup = BeautifulSoup(html_source, 'html.parser')
return soup
def parse_live_game(game):
date_time = game.select("th.date-time")
if len(date_time):
time = date_time[0].get_text().strip()
parts = time.split(" - ")
if len(parts) > 1:
clock = parts[0]
quarter = parts[1]
else:
clock = None
quarter = time
else:
clock = None
quarter = None
away = game.select("tr.away")
if len(away):
away_team = away[0].select("span.sb-team-abbrev")[0].get_text().strip()
away_score = away[0].select("td.total")[0].get_text().strip()
else:
away_team = None
away_score = None
home = game.select("tr.home")
if len(home):
home_team = home[0].select("span.sb-team-abbrev")[0].get_text().strip()
home_score = home[0].select("td.total")[0].get_text().strip()
else:
home_team = None
home_score = None
network_span = game.select("th.network")
if len(network_span):
network = network_span[0].get_text().strip()
else:
network = None
game_dict = {}
game_dict['date'] = None
game_dict['day'] = None
game_dict['time'] = None
game_dict['away'] = away_team
game_dict['away_score'] = away_score
game_dict['home'] = home_team
game_dict['home_score'] = home_score
game_dict['quarter'] = quarter
game_dict['network'] = network
game_dict['clock'] = clock
game_dict['status'] = "ACTIVE"
return game_dict
def parse_upcoming_game(game):
day_span = game.select("span.month-day")
if len(day_span):
day = day_span[0].get_text().strip()
else:
day = None
time_span = game.select("span.time")
if len(time_span):
time = time_span[0].get_text().strip()
else:
time = None
away = game.select("tr.away")
if len(away):
away_team = away[0].select("span.sb-team-abbrev")[0].get_text().strip()
else:
away_team = None
home = game.select("tr.home")
if len(home):
home_team = home[0].select("span.sb-team-abbrev")[0].get_text().strip()
else:
home_team = None
network_span = game.select("th.network")
if len(network_span):
network = network_span[0].get_text().strip()
else:
network = None
date_time = game.select("th.date-time")
if len(date_time):
date = date_time[0]['data-date'].strip()
else:
date = None
game_dict = {}
game_dict['date'] = date
game_dict['day'] = day
game_dict['time'] = time
game_dict['away'] = away_team
game_dict['away_score'] = None
game_dict['home'] = home_team
game_dict['home_score'] = None
game_dict['quarter'] = None
game_dict['network'] = network
game_dict['clock'] = None
game_dict['status'] = "PREGAME"
return game_dict
def parse_completed_game(game):
away = game.select("tr.away")
if len(away):
away_team = away[0].select("span.sb-team-abbrev")[0].get_text().strip()
away_score = away[0].select("td.total")[0].get_text().strip()
else:
away_team = None
away_score = None
home = game.select("tr.home")
if len(home):
home_team = home[0].select("span.sb-team-abbrev")[0].get_text().strip()
home_score = home[0].select("td.total")[0].get_text().strip()
else:
home_team = None
home_score = None
game_dict = {}
game_dict['date'] = None
game_dict['day'] = None
game_dict['time'] = None
game_dict['away'] = away_team
game_dict['away_score'] = away_score
game_dict['home'] = home_team
game_dict['home_score'] = home_score
game_dict['quarter'] = None
game_dict['network'] = None
game_dict['clock'] = None
game_dict['status'] = "FINAL"
return game_dict
def get_game_data():
soup = load_soup_for_page(URL_STR)
games = []
live_games = soup.select("section.sb-score.live")
for live_game in live_games:
game = parse_live_game(live_game)
games.append(game)
upcoming_games = soup.select("section.sb-score.pregame")
for upcoming_game in upcoming_games:
game = parse_upcoming_game(upcoming_game)
games.append(game)
completed_games = soup.select("section.sb-score.final")
for completed_game in completed_games:
game = parse_completed_game(completed_game)
games.append(game)
return games