-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscores.py
More file actions
80 lines (75 loc) · 2.69 KB
/
scores.py
File metadata and controls
80 lines (75 loc) · 2.69 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
import pandas
from datetime import datetime, timedelta
from dateutil.tz import tzlocal
import api
import meta
def Scores(date:str):
"""
TODO
"""
# API Download
apiData = api.MLBAPIHandler("scores "+date)
df = pandas.json_normalize(apiData,['dates','games']).set_index('gamePk').sort_values(by='gameDate')
df['teams.home.team.abbreviation'] = df['teams.home.team.id'].map(meta.TeamInfo("ALL","CODES"))
df['gameTime'] = pandas.to_datetime(df['gameDate'],format='%Y-%m-%dT%H:%M:%SZ',utc=True).dt.tz_convert(tzlocal()).dt.strftime("%H:%M")
# Format game status and score (if available)
df = df.groupby('status.abstractGameCode').apply(_gameState)
rowStr = " ".join([tableDict[key]['str'] for key in tableDict.keys()])
home = df[[tableDict[x]['col'].format('home') for x in ['TEAM','SCORE','CODE']]].values.tolist()
away = df[[tableDict[x]['col'].format('away') for x in ['TEAM','SCORE','CODE']]].values.tolist()
print()
for ii in range(len(home)):
print(rowStr.format(*away[ii]))
print(rowStr.format(*home[ii]))
print()
def ScoresCheck(query:list[str]):
"""TODO"""
if len(query) > 1:
print("Too many dates given. Try again.")
return 1, None
if not query:
date = "today"
else:
date = query[0]
if (date == "today") or (date == "tonight"):
date = str(datetime.now())
elif date == "yesterday":
date = str(datetime.now() - timedelta(days=1))
elif date == "tomorrow":
date = str(datetime.now() + timedelta(days=1))
else:
if len(date) == 5:
date = str(datetime.now().year) + "-" + date
try:
datetime.strptime(date,"%Y-%m-%d")
except:
print("Invalid date given. Please supply the date as YYYY-MM-DD or MM-DD.")
return 2, None
return 0, date
def _gameState(group):
# TODO: fix function
if group['status.abstractGameCode'].iloc[0] == 'P':
group.loc[:,['teams.away.code','teams.home.score','teams.away.score']] = ' '
group['teams.home.code'] = group['gameTime']
elif group['status.abstractGameCode'].iloc[0] == 'F':
c = group['teams.home.score'] > group['teams.away.score']
group.loc[c,'teams.home.code'] = '<'
group.loc[c,'teams.away.code'] = 'F'
group.loc[~c,'teams.home.code'] = 'F'
group.loc[~c,'teams.away.code'] = '<'
return group
# Dictionary defining table fields and formatting
tableDict = {
'TEAM': {
'col': 'teams.{}.team.name',
'str': '{:<22}',
},
'SCORE': {
'col': 'teams.{}.score',
'str': '{:>2}',
},
'CODE': {
'col': 'teams.{}.code',
'str': '{:<2}',
},
}