-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
executable file
·328 lines (283 loc) · 11.5 KB
/
functions.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
from models import Game, Inning, AtBat, Pitch, Player
import os
import sys
import datetime
import requests
import time
from lxml import etree
from natural import number
from papirus import PapirusTextPos
now = datetime.datetime.now()
fontPath = os.path.dirname(os.path.abspath(__file__)) + '/fonts/benton-sans-regular.ttf'
dingsPath =os.path.dirname(os.path.abspath(__file__)) + '/fonts/FreeSans.ttf'
display = PapirusTextPos(False)
minX = 0
minY = 0
# ---------------------------------------------------------------------
def writeToDisplay(text = '', x=2, y=2, size=14, id='Default', fontPath=fontPath, maxLines=100):
display.AddText(text=text, x=x, y=y, size=size, Id=id, invert=False, fontPath=fontPath, maxLines=maxLines)
#print(datetime.datetime.now().strftime('%Y%m%d %H:%M:%S %Z') + ': ' + text)
#print('')
return
# ---------------------------------------------------------------------
def getCount(howMany, max):
count = ''
if int(howMany) == 4 and max == 4:
count = u"\u25CF \u25CF \u25CF \u25CF"
elif int(howMany) == 3 and max == 4:
count = u"\u25CF \u25CF \u25CF \u25CB"
elif int(howMany) == 2 and max == 4:
count = u"\u25CF \u25CF \u25CB \u25CB"
elif int(howMany) == 1 and max == 4:
count = u"\u25CF \u25CB \u25CB \u25CB"
elif int(howMany) == 0 and max == 4:
count = u"\u25CB \u25CB \u25CB \u25CB"
elif int(howMany) == 3:
count = u"\u25CF \u25CF \u25CF"
elif int(howMany) == 2:
count = u"\u25CF \u25CF \u25CB"
elif int(howMany) == '1':
count = u"\u25CF \u25CB \u25CB"
else:
count = u"\u25CB \u25CB \u25CB"
return count
# ---------------------------------------------------------------------
def getPitchType(pitch):
pitchTypeAbbreviation = pitch.pitchType
return {
'CB' : 'Curveball',
'CH' : 'Changeup',
'CU' : 'Curveball',
'EP' : 'Eephus',
'FA' : 'Fastball',
'FC' : 'Fastball (Cutter)',
'FF' : 'Four-Seam Fastball',
'FO' : 'Pitch Out',
'FS' : 'Fastball',
'FT' : 'Two-Seam Fastball',
'KC' : 'Knuckle-curve',
'KN' : 'Knuckleball',
'PO' : 'Pitch Out',
'SF' : 'Fastball (Split-Fingered)',
'SI' : 'Fastball (Sinker)',
'SL' : 'Slider'
}[pitchTypeAbbreviation] or ''
# ---------------------------------------------------------------------
def getTeamAbbreviation(team):
return {
'Angels' : 'LAA',
'Astros' : 'HOU',
'Athletics' : 'OAK',
'Blue Jays' : 'TOR',
'Braves' : 'ATL',
'Brewers' : 'MIL',
'Cardinals' : 'SLN',
'Cubs' : 'CHN',
'D-backs' : 'ARI',
'Dodgers' : 'LAD',
'Giants' : 'SF',
'Indians' : 'CLE',
'Mariners' : 'SEA',
'Marlins' : 'MIA',
'Mets' : 'NYM',
'Nationals' : 'WAS',
'Orioles' : 'BAL',
'Padres' : 'SD',
'Phillies' : 'PHL',
'Pirates' : 'PIT',
'Rangers' : 'TEX',
'Rays' : 'TB',
'Red Sox' : 'BOS',
'Reds' : 'CIN',
'Rockies' : 'COL',
'Royals' : 'KAN',
'Tigers' : 'DET',
'Twins' : 'MIN',
'White Sox' : 'CWS',
'Yankees' : 'NYY'
}[team] or team
# ---------------------------------------------------------------------
def getAtBatTeam(game, atBat):
if getTeamAbbreviation(game.team) == atBat.batter.team:
output = '[' + game.team + '] vs. ' + game.opponent
elif getTeamAbbreviation(game.opponent) == atBat.batter.team:
output = game.team + ' vs. [' + game.opponent + ']'
else:
output = game.team + ' vs. ' + game.opponent
return output
# ---------------------------------------------------------------------
def displayBoxScore(game, inning):
display.Clear()
if game.status == 'FINAL':
writeToDisplay('FINAL', minX, minY, 14, 'BoxScoreInning', fontPath)
else:
writeToDisplay(inning.half + ' ' + number.ordinal(inning.number), minX, minY, 14, 'BoxScoreInning', fontPath)
writeToDisplay('R', 125, 35, 14, 'RunsHeader', fontPath)
writeToDisplay('H', 150, 35, 14, 'HitsHeader', fontPath)
writeToDisplay('E', 175, 35, 14, 'ErrorsHeader', fontPath)
writeToDisplay(game.team, minX, 55, 14, 'Team', fontPath)
writeToDisplay(game.teamRuns, 125, 55, 14, 'TeamRuns', fontPath)
writeToDisplay(game.teamHits, 150, 55, 14, 'TeamHits', fontPath)
writeToDisplay(game.teamErrors, 175, 55, 14, 'TeamErrors', fontPath)
writeToDisplay(game.opponent, minX, 75, 14, 'Opponent', fontPath)
writeToDisplay(game.opponentRuns, 125, 75, 14, 'OpponentRuns', fontPath)
writeToDisplay(game.opponentHits, 150, 75, 14, 'OpponentHits', fontPath)
writeToDisplay(game.opponentErrors, 175, 75, 14, 'OpponentErrors', fontPath)
display.WriteAll(partialUpdate=True)
return
# ---------------------------------------------------------------------
def displayPitch(game, atBat, pitch):
display.Clear()
writeToDisplay(getAtBatTeam(game, atBat), minX, minY, 14, 'Versus', fontPath)
writeToDisplay('Pitcher: ' + atBat.pitcher.first + ' ' + atBat.pitcher.last, minX, 20, 12, 'Pitcher', fontPath)
writeToDisplay(str(atBat.pitcher.era) + ' ' + 'ERA', 150, 22, 10, 'PitcherStats', fontPath)
writeToDisplay(str(pitch.speed) + ' MPH ' + getPitchType(pitch), minX, 35, 10, 'Pitch', fontPath)
writeToDisplay('At Bat: ' + atBat.batter.first + ' ' + atBat.batter.last, minX, 50, 12, 'Batter', fontPath)
writeToDisplay('#' + str(atBat.batter.number) + ' ' + atBat.batter.position, 150, 52, 10, 'BatterPosition', fontPath)
writeToDisplay(str(atBat.batter.avg) + 'AVG, ' + str(atBat.batter.hr) + ' HR', minX, 65, 10, 'BatterStats', fontPath)
writeToDisplay(pitch.callDescription, minX, 80, 10, 'Call', fontPath)
display.WriteAll(partialUpdate=True)
return
# ---------------------------------------------------------------------
def displayPlay(game, atBat, pitch):
baseDefault = u"\u25C7"
baseHighlight = u"\u25C6"
display.Clear()
writeToDisplay(getAtBatTeam(game, atBat), minX, minY, 14, 'Versus', fontPath)
writeToDisplay(atBat.description, minX, 17, 10, 'Description', maxLines=3)
writeToDisplay('B', minX, 50, 12, 'BallsLabel', fontPath)
writeToDisplay(getCount(atBat.balls, 4), 25, 50, 12, 'BallsCount', dingsPath)
writeToDisplay('S', minX, 65, 12, 'StrikesLabel', fontPath)
writeToDisplay(getCount(atBat.strikes, 3), 25, 65, 12, 'StrikesCount', dingsPath)
writeToDisplay('O', minX, 80, 12, 'OutsLabel', fontPath)
writeToDisplay(getCount(atBat.outs, 3), 25, 80, 12, 'OutsCount', dingsPath)
if atBat.onFirst is not None:
writeToDisplay(baseHighlight, 170, 70, 20, 'FirstBase', dingsPath)
else:
writeToDisplay(baseDefault, 170, 70, 20, 'FirstBase', dingsPath)
if atBat.onSecond is not None:
writeToDisplay(baseHighlight, 150, 55, 20, 'SecondBase', dingsPath)
else:
writeToDisplay(baseDefault, 150, 55, 20, 'SecondBase', dingsPath)
if atBat.onThird is not None:
writeToDisplay(baseHighlight, 130, 70, 20, 'ThirdBase', dingsPath)
else:
writeToDisplay(baseDefault, 130, 70, 20, 'ThirdBase', dingsPath)
display.WriteAll(partialUpdate=True)
return
# ---------------------------------------------------------------------
def displayInGame(game, inning, atBat, pitch, interval):
if game.status == 'FINAL':
displayBoxScore(game, inning)
time.sleep(interval)
elif game.status == 'PREGAME':
displayStatus('Game starting soon!', interval)
else:
displayBoxScore(game, inning)
time.sleep(interval/3)
displayPitch(game, atBat, pitch)
time.sleep(interval/3)
displayPlay(game, atBat, pitch)
time.sleep(interval/3)
return
# ---------------------------------------------------------------------
def displayStatus(message, wait):
display.Clear()
writeToDisplay(message, minX, minY, 14, 'Status', fontPath)
display.WriteAll()
time.sleep(wait)
return
# --------------------------------------------------------------------
def getPlayer(players, playerID):
playerRoot = players.xpath("/game/team/player[@id='" + playerID + "']")[0]
player = Player()
player.id = playerID
player.first = playerRoot.get('first')
player.last = playerRoot.get('last')
player.team = playerRoot.get('team_abbrev')
player.number = playerRoot.get('num')
player.handed = playerRoot.get('rl')
player.bats = playerRoot.get('bats')
player.position = playerRoot.get('position')
player.status = playerRoot.get('status')
average = playerRoot.get('avg')
if average.isdigit() == False:
average = 0
player.avg = '{0:.3f}'.format(float(average))
player.hr = playerRoot.get('hr')
player.rbi = playerRoot.get('rbi')
if player.position == 'P':
player.wins = playerRoot.get('wins')
player.losses = playerRoot.get('losses')
player.era = float(playerRoot.get('era'))
return player
# ---------------------------------------------------------------------
def getLatestEvent(game, eventsUrl, players, scoreboardUrl, interval=60):
try:
inning = Inning()
atBat = AtBat()
pitch = Pitch()
scoreboardRequest = requests.get(scoreboardUrl)
scoreboardDocumentBytes = bytes(bytearray(scoreboardRequest.text, encoding='utf-8'))
scoreboardDocumentXML = etree.XML(scoreboardDocumentBytes)
gameRoot = scoreboardDocumentXML.xpath("/scoreboard/*/game[@id='" + game.id + "']")[0]
gameStatus = gameRoot.get('status')
if gameStatus == 'FINAL' or gameStatus == 'GAME_OVER':
game.status = 'FINAL'
elif gameStatus == 'PRE_GAME':
game.status == 'PREGAME'
outs = gameRoot.getparent().get("outs") or 0 # MLB stores items in multiple places?
parentIter = gameRoot.getparent().iter()
for child in parentIter:
if child.tag == 'team' and child.get('name') == game.team:
game.teamRuns = child.getchildren()[0].get('R')
game.teamHits = child.getchildren()[0].get('H')
game.teamErrors = child.getchildren()[0].get('E')
if child.tag == 'team' and child.get('name') != game.team:
game.opponent = child.get('name')
game.opponentRuns = child.getchildren()[0].get('R')
game.opponentHits = child.getchildren()[0].get('H')
game.opponentErrors = child.getchildren()[0].get('E')
gameEventsRequest = requests.get(eventsUrl)
# print(gameEventsRequest.text)
gameEventsDocumentBytes = bytes(bytearray(gameEventsRequest.text, encoding='utf-8'))
gameEventsDocumentXML = etree.XML(gameEventsDocumentBytes)
# Get current/most recent inning
inningRoot = gameEventsDocumentXML.xpath('/game/inning[last()]')[0]
inning.number = inningRoot.get('num') or 0
inning.half = (inningRoot.xpath("/game/inning[last()]/*[text() and string-length()>0][last()]")[0]).tag or ''
atBatRoot = gameEventsDocumentXML.xpath("/game/inning[last()]/*[text() and string-length()>0][last()]/atbat[last()]")[0]
atBatPlayerRoot = gameEventsDocumentXML.xpath("/game/atBat")[0]
atBat.number = atBatRoot.get('num') or 0
atBat.balls = atBatRoot.get('b') or 0
atBat.strikes = atBatRoot.get('s') or 0
atBat.outs = atBatRoot.get('o') or outs
atBat.pitcher = getPlayer(players, atBatRoot.get('pitcher'))
atBat.batter = getPlayer(players, atBatRoot.get('batter'))
if atBatRoot.get('b1'):
atBat.onFirst = getPlayer(players, atBatRoot.get('b1'))
if atBatRoot.get('b2'):
atBat.onSecond = getPlayer(players, atBatRoot.get('b2'))
if atBatRoot.get('b3'):
atBat.onThird = getPlayer(players, atBatRoot.get('b3'))
atBat.description = atBatRoot.get('des') or ''
atBat.guid = atBatRoot.get('play_guid') or ''
trackAtBatID = atBat.number
# Get current/most recent pitch of the inning
pitchRoot = gameEventsDocumentXML.xpath("/game/inning[last()]/*[text() and string-length()>0][last()]/atbat[last()]/pitch[last()]")[0]
pitch.id = pitchRoot.get('sv_id') or ''
pitch.call = pitchRoot.get('type') or ''
pitch.callDescription = pitchRoot.get('des') or ''
pitch.speed = pitchRoot.get('start_speed') or 0
pitch.pitchType = pitchRoot.get('pitch_type') or ''
trackPitchID = pitch.id
displayInGame(game, inning, atBat, pitch, interval)
except Exception as ex:
#print(ex)
pass
if game.status != 'FINAL':
return True
else:
time.sleep(3600)
return False
# ---------------------------------------------------------------------