-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlb_scoreboard.py
216 lines (189 loc) · 6.9 KB
/
mlb_scoreboard.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
#Jason Hammett
#MLB command line scoreboard
import datetime
import sys
import urllib.request
import xmltodict
from termcolor import colored, cprint
import colorama
import termcolor
teamColorDict = dict()
teamColorDict['Cardinals'] = ['white', 'on_red']
teamColorDict['Yankees'] = ['white', 'on_grey']
teamColorDict['Rays'] = ['white', 'on_cyan']
teamColorDict['Blue Jays'] = ['white', 'on_blue']
teamColorDict['Tigers'] = ['white', 'on_grey']
teamColorDict['Orioles'] = ['red', 'on_yellow']
teamColorDict['Red Sox'] = ['white', 'on_red']
teamColorDict['Marlins'] = ['white', 'on_cyan']
teamColorDict['Dodgers'] = ['white', 'on_blue']
teamColorDict['Pirates'] = ['grey', 'on_yellow']
teamColorDict['Reds'] = ['white', 'on_red']
teamColorDict['Braves'] = ['grey', 'on_red']
teamColorDict['Mets'] = ['white', 'on_blue']
teamColorDict['Nationals'] = ['white', 'on_red']
teamColorDict['Phillies'] = ['white', 'on_red']
teamColorDict['Twins'] = ['white', 'on_magenta']
teamColorDict['Indians'] = ['grey', 'on_red']
teamColorDict['White Sox'] = ['grey', 'on_white']
teamColorDict['Royals'] = ['white', 'on_magenta']
teamColorDict['Brewers'] = ['white', 'on_magenta']
teamColorDict['Giants'] = ['white', 'on_grey']
teamColorDict['Athletics'] = ['yellow', 'on_green']
teamColorDict['Mariners'] = ['grey', 'on_magenta']
teamColorDict['D-backs'] = ['white', 'on_magenta']
teamColorDict['Angels'] = ['white', 'on_red']
teamColorDict['Rangers'] = ['red', 'on_blue']
teamColorDict['Padres'] = ['white', 'on_magenta']
teamColorDict['Rockies'] = ['grey', 'on_magenta']
teamColorDict['Astros'] = ['white', 'on_red']
teamColorDict['Cubs'] = ['red', 'on_blue']
banner1 = "# ____ ____ _____ ______ ______ ______ ___ _______ ________ ______ ___ _ _______ ______"
banner2 = "# |_ \ / _||_ _| |_ _ \ .' ____ \ .' ___ | .' `.|_ __ \ |_ __ ||_ _ \ .' `. / \ |_ __ \ |_ _ `."
banner3 = "# | \/ | | | | |_) | | (___ \_|/ .' \_|/ .-. \ | |__) | | |_ \_| | |_) | / .-. \ / _ \ | |__) | | | `. \ "
banner4 = "# | |\ /| | | | _ | __'. _.____`. | | | | | | | __ / | _| _ | __'. | | | | / ___ \ | __ / | | | | "
banner5 = "# _| |_\/_| |_ _| |__/ | _| |__) | | \____) |\ `.___.'\\\ `-' /_| | \ \_ _| |__/ | _| |__) |\ `-' /_/ / \ \_ _| | \ \_ _| |_.' / "
banner6 = "# |_____||_____||________||_______/ \______.' `.____ .' `.___.'|____| |___||________||_______/ `.___.'|____| |____||____| |___||______.' "
print(banner1)
print(banner2)
print(banner3)
print(banner4)
print(banner5)
print(banner6)
#print(str(len(sys.argv)))
# Must be either 0 or 3 command line arguments
if ((len(sys.argv) != 1) and (len(sys.argv) != 4)):
print("Incorrect arguments\nEither provide no arguments for today's scores or provide Month Day Year for a specific date")
sys.exit()
if(len(sys.argv) == 4):
#Specific date
month = int(sys.argv[1])
day = int(sys.argv[2])
year = int(sys.argv[3])
else:
#current day
date = datetime.date.today()
month = date.month
year = date.year
day = date.day
#MLB game data
urlStr1 = "http://gd2.mlb.com/components/game/mlb/year_"
urlStr2 = "/month_"
urlStr3 = "/day_"
urlStr4 = "/scoreboard.xml"
#Formatting the url to today
finalUrl = urlStr1 + str(year) + urlStr2 + str(month).zfill(2) + urlStr3 + str(day).zfill(2) + urlStr4
print(finalUrl)
#Query the MLB website
response = urllib.request.urlopen(finalUrl)
responseDict = xmltodict.parse(response)
#Response is now an xml tag based dictionary
try:
scoreboard = responseDict['scoreboard']
except e:
print("No games today.")
numberOfFinishedGames = 0
numberOfInProgressGames = 0
#print(scoreboard)
try:
numberOfFinishedGames = len(scoreboard['go_game'])
except Exception as e:
#print(e)
print("No Completed Games\n")
numberOfFinishedGames = 0
try:
numberOfInProgressGames = len(scoreboard['ig_game'])
except Exception:
print("No In Progress Games\n")
numberOfInProgressGames = 0
'''
'''
def get_games(scoreboard):
gameType = 'go_game'
if (numberOfFinishedGames > 0):
print("Completed Games\n")
print("Team\t\t\tRuns\tHits\tErrors\n")
for game in scoreboard[gameType]:
for team in game['team']:
runs = team['gameteam']['@R']
hits = team['gameteam']['@H']
errors = team['gameteam']['@E']
name = team['@name']
spaces = " " * (10 - len(name))
#cprint(name, teamColorDict[name][0], teamColorDict[name][1], end="")
print(name, end="")
print(spaces + "\t\t" + str(runs) + "\t" + str(hits) + "\t" + str(errors))
print()
gameType = 'ig_game'
if (numberOfInProgressGames > 0):
print("\nIn Progress Games\n")
print("Team\t\t\tRuns\tHits\tErrors")
for game in scoreboard[gameType]:
#print(game.keys())
print()
status = game['game']['@status']
startTime = game['game']['@start_time']
#print(game['game'])
if (status == "PRE_GAME" or status == "IMMEDIATE_PREGAME"):
print("PREGAME\nStart Time = ", end="")
print(startTime)
for team in game['team']:
runs = team['gameteam']['@R']
hits = team['gameteam']['@H']
errors = team['gameteam']['@E']
name = team['@name']
spaces = " " * (10 - len(name))
#cprint(name, teamColorDict[name][0], teamColorDict[name][1], end="")
print(name, end="")
print(spaces + "\t\t" + str(runs) + "\t" + str(hits) + "\t" + str(errors))
elif status == "IN_PROGRESS":
inning = game['inningnum']
outs = game['@outs']
for team in game['team']:
runs = team['gameteam']['@R']
hits = team['gameteam']['@H']
errors = team['gameteam']['@E']
name = team['@name']
spaces = " " * (10 - len(name))
#cprint(name, teamColorDict[name][0], teamColorDict[name][1], end="")
print(name, end="")
print(spaces + "\t\t" + str(runs) + "\t" + str(hits) + "\t" + str(errors))
print(inning['@half'] + str(inning['@inning']) + " " + str(outs) + " outs")
try:
pitcher = game['pitcher']
print("P: " + pitcher['@name'])
except KeyError as k:
pass
try:
batter = game['batter']
print("AB: " + batter['@name'])
except KeyError as k:
pass
try:
on_base = game['on_base']
print("On Base: ")
#print(len(game['on_base']))
try:
for base_runner in on_base:
#print(base_runner)
base_number = base_runner['@base']
player = base_runner['player']
print(str(base_number) + ": " + player['@name'])
except TypeError as t:
base_number = on_base['@base']
player = on_base['player']
print(str(base_number) + ": " + player['@name'])
except KeyError as k:
pass
return (True)
get_games(scoreboard)
'''
print("Number of in progress games: " + str(numberOfInProgressGames))
print("Number of finished games: " + str(numberOfFinishedGames))
print(scoreboard['ig_game'])
print()
for game in scoreboard['ig_game']:
print(game[''])
if (numberOfInProgressGames > 0):
print(get_games(scoreboard, 'ig_game'))
'''