-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmlb_highlights.py
More file actions
303 lines (276 loc) · 12.7 KB
/
Copy pathmlb_highlights.py
File metadata and controls
303 lines (276 loc) · 12.7 KB
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
import requests
import json
import bs4
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtMultimedia import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QPixmap
import mlbui
import sys
import videowidget
import re
from datetime import *
from dateutil.tz import *
class MainWindow(QMainWindow, mlbui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.single_game_highlights_ordered = []
self.single_game_highlights = {}
date = QDate.currentDate()
self.dateEdit.setDate(date) # sets the dateEdit window to the current date
self.set_team_logos()
self.listWidget.itemDoubleClicked.connect(self.display_single_game_replays) # team double clicked
self.replay_window.itemDoubleClicked.connect(self.replay_clicked) # replay double clicked
self.get_game_button.clicked.connect(self.game_button_clicked) # gets date
def set_team_logos(self):
self.team_logos = {'Angels': 'ana.jpg', 'D-backs': 'ari.jpg', 'Braves': 'atl.jpg',
'Orioles': 'bal.jpg', 'Red Sox': 'bos.jpg', 'Cubs': 'chc.jpg', 'Reds': 'cin.jpg',
'Indians': 'cle.jpg', 'Rockies': 'col.jpg', 'White Sox': 'cws.jpg', 'Tigers': 'det.jpg',
'Astros': 'hou.jpg', 'Royals': 'kc.jpg', 'Dodgers': 'la.jpg', 'Marlins': 'mia.jpg',
'Brewers': 'mil.jpg', 'Twins': 'min.jpg', 'Mets': 'nym.jpg', 'Yankees': 'nyy.jpg',
'Athletics': 'oak.jpg', 'Phillies': 'phi.jpg', 'Pirates': 'pit.jpg', 'Padres': 'sd.jpg',
'Mariners': 'sea.jpg', 'Giants': 'sf.jpg', 'Cardinals': 'stl.jpg', 'Rays': 'tb.jpg',
'Rangers': 'tex.jpg', 'Blue Jays': 'tor.jpg', 'Nationals': 'was.jpg'}
def game_button_clicked(self):
"""
gets the date and goes to the MLB site and gets the games from that date
:return:
"""
self.listWidget.clear()
item = self.dateEdit.date().toPyDate() # returns date in format "mm-dd-yyyy"
date_of_games = str(item).split('-') # splits date up into a list
year = date_of_games[0]
month = date_of_games[1]
day = date_of_games[2]
self.day_url = 'http://gd2.mlb.com/components/game/mlb/year_{}/month_{}/day_{}/grid.json'.format(year, month, day)
self.media_url = 'http://gd2.mlb.com/components/game/mlb/year_{}/month_{}/day_{}/media/highlights.xml'.format(year, month, day)
self.games_of_the_day = self.get_date_json() # gets the games for the chosen date
self.menu_dict = self.build_menu(self.games_of_the_day) # builds a dict of the games and their unique id
free_game = self.find_free_game(self.games_of_the_day) # finds the MLB free game of the day
self.free_game_label.setText(free_game) # set's the label for the window of the free game
self.replay_data = self.get_highlight_xml(self.games_of_the_day) # gets the replay data for all the games
def replay_clicked(self):
"""
gets the replay the user clicks on, and then sends it's to the video object to play
:return:
"""
if self.radioButton.isChecked():
self.play_all_replays()
else:
replay = self.replay_window.currentItem().text() # name of the replay
for k, v in self.single_game_highlights.items():
if k == replay:
url = v
break
self.dialog = videowidget.VideoPlayer() # the videowidget object of the imported file
self.dialog.mediaPlayer.setMedia(QMediaContent(QUrl(url))) # sets the file to play
self.dialog.mediaPlayer.play()
self.dialog.setWindowTitle('MLB Replay - {}'.format(replay)) # adds replay title to window title
self.dialog.show()
#item = self.replay_window.currentItem().text()
#QMessageBox.warning(self, 'Warning!', '%s' % item)
def play_all_replays(self):
"""
creates a QMediaPlaylist object and inserts all the games replays (in order) to the playlist
and then plays it.
:return:
"""
self.dialog = videowidget.VideoPlayer() # this is the video player widget I import
self.playlist = QMediaPlaylist() # the playlist
self.dialog.mediaPlayer.setPlaylist(self.playlist) # mediaPlayer is the QMediaPlayer created in videowidget()
for v in self.single_game_highlights_ordered:
url = QUrl(v)
self.playlist.addMedia(QMediaContent(url))
self.dialog.mediaPlayer.setPlaylist(self.playlist)
self.dialog.nextButton.setEnabled(True)
self.dialog.nextButton.setToolTip('Next Replay')
self.playlist.setCurrentIndex(0)
self.dialog.mediaPlayer.play()
self.dialog.setWindowTitle('MLB Replay - {}'.format(self.replay_window.item(0).text()))
self.dialog.show()
self.playlist.currentIndexChanged.connect(self.playlist_changed) # call this function when the playlist changes.
self.dialog.nextButton.pressed.connect(self.next_button_pressed)
def playlist_changed(self):
num = self.playlist.currentIndex() # gets current playlist index
self.dialog.setWindowTitle('MLB Replay - {}'.format(self.replay_window.item(num).text()))
def next_button_pressed(self):
num = self.playlist.currentIndex() + 1
self.dialog.mediaPlayer.stop()
if num < self.playlist.mediaCount():
self.dialog.setWindowTitle('MLB Replay - {}'.format(self.replay_window.item(num).text()))
self.playlist.setCurrentIndex(num)
self.dialog.mediaPlayer.play()
else:
self.dialog.close()
def display_single_game_replays(self):
"""
builds and displays the selected game's highlights
:param menu_dict: games of the day with unique id
:param replay_data: xml of the replay info
:return: ??
"""
self.single_game_highlights_ordered = []
found = False
self.replay_window.clear()
teams = self.listWidget.currentItem().text()
for game, key in self.menu_dict.items():
if teams == game:
game_id = key
for games in self.replay_data:
if games[0].game_pk == game_id: # find the game
found = True
for replay in games:
self.replay_window.addItem('{}'.format(replay.headline)) # adds Title to window
self.single_game_highlights_ordered.append(replay.replay_url)
self.single_game_highlights[replay.headline] = replay.replay_url # creates dict entry
break
if not found:
self.replay_window.addItem('No Replay Data Found for this game')
#QMessageBox.warning(self, 'Warning!', 'No replay data found for this game')
def show_game(self):
"""
shows the games in the listWidget
:return:
"""
item = self.listWidget.currentItem().text()
self.lineEdit.setText(item)
def build_menu(self, games):
"""
Builds a menu of available games
:param games: list of GameData() objects
:return: dict with k,v - game name (away_team @ home_team), game_pk
"""
game_dict = {}
for game in games:
title = '{} @ {}'.format(game.away_team_name, game.home_team_name)
self.listWidget.addItem(title)
game_pk = game.game_pk
game_dict[title] = game_pk
return game_dict
def build_game_data(self, name, game):
"""
Builds information about the days games. creates a GameData object to store the data
:param game: str
:return: GameData() object
"""
name = GameData()
name.game_pk = game['game_pk']
name.id = game['id']
name.away_team_name = game['away_team_name']
name.home_team_name = game['home_team_name']
name.event_time = game['event_time']
try: name.free = game['game_media']['homebase']['media'][0]['free']
except: name.free = 'NO'
name.calendar_event_id = game['calendar_event_id']
name.media_state = game['media_state']
return name
def find_free_game(self, games):
"""
finds the Free Game of the Day on MLB.TV
:param games: list of GameData() objects
:return: str of the free game
"""
away_label = self.away_team_label
home_label = self.home_team_label
game_time = self.free_game_time_label
away_label.clear()
home_label.clear()
for game in games:
if game.free == 'ALL':
home_pixmap = QPixmap('./logos/' + self.team_logos[game.home_team_name])
away_pixmap = QPixmap('./logos/' + self.team_logos[game.away_team_name])
event_time = game.event_time + ' EST'
away_label.setPixmap(away_pixmap)
home_label.setPixmap(home_pixmap)
game_time.setText(event_time)
return '{} @ {}'.format(game.away_team_name, game.home_team_name)
def build_game_highlights(self, data):
"""
builds video highlights for each game
:param data: XML data
:return: list of ReplayData() objects
"""
game_highlights = []
video = data.find_all('media', type='video')
media = re.compile(r'(http://.*\.mp4)')
for tag in video:
pk = data.attrs['game_pk']
pk = ReplayData()
pk.game_pk = data.attrs['game_pk']
pk.id = tag.attrs['id']
text = tag.text.strip().split('\n')
pk.headline = text[0]
pk.length = text[1]
mo = media.search(tag.text)
pk.replay_url = mo.group()
game_highlights.append(pk)
game_highlights.sort(key=lambda x: x.id) # sorts the list of objects based on the game id
return game_highlights
def get_date_json(self):
"""
loads the date selected and builds a list of the games that day
:return: list of game objects
"""
url = self.day_url
days_games = [] # holds GameData() objects with data for each game.
response = requests.get(url)
response.raise_for_status()
replay = json.loads(response.text)
games = replay['data']['games']['game']
for game in games: # build game data
name = game['home_name_abbrev']
days_games.append(self.build_game_data(name, game))
return days_games
def get_highlight_xml(self, days_games):
"""
Gets the highlights in XML form from MLB and returns a list of objects containing
each games highlists
:param days_games:
:return: list of objects with the highlight data
"""
replay_data = [] # holds ReplayData() objects with data for each replay
response = requests.get(self.media_url)
if response.status_code == 404:
self.replay_window.addItem('No Media found for this date....yet?')
#QMessageBox.warning(self, 'Warning!', '404 - Not Found Error\nNo Media found for this date.')
else:
response.raise_for_status()
page = bs4.BeautifulSoup(response.text, 'lxml')
highlights = page.find_all('highlights')
for game in days_games: # builds replay data for all games with highlights
for data in highlights:
if game.game_pk not in data.attrs['game_pk']: # some games don'y have highlights, deal with that eventually
pass
else:
replay_data.append(self.build_game_highlights(data))
return replay_data
class GameData():
"""
Holds data for each game played on a certain day
"""
game_pk = '' # unique id for each game
id = '' # game id in format "2015/06/18/texmlb-lanmlb-1"
away_team_name = ''
home_team_name = ''
event_time = '' # start time EST
free = '' # is this the "Free game of the day on MLB.TV?
calendar_event_id = '' # another unique id for the game
media_state = '' # game over?
class ReplayData():
"""
Holds data for each replay generated
"""
game_pk = '' # unique id for each game
id = '' # unique id for each replay (larger = later in game?)
length = '' # length of the replay
headline = '' # headline for replay
replay_url = '' # full url for replay
def main():
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
#app.exec_()
if __name__ == '__main__':
main()