-
Notifications
You must be signed in to change notification settings - Fork 1
/
what2watch.py
349 lines (302 loc) · 10.2 KB
/
what2watch.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import json
import glob
import random
import os
from datetime import datetime, timedelta
from sportsipy.mlb.boxscore import Boxscores, Boxscore
import argparse
import itertools
TAG_DEFS = {
'pitchers-duel': 'Both of the teams scored ≤2 runs.',
'high-scoring': 'Either the combined run count was over 15, or at least one side was over 9.',
'big-inning': 'More than 5 runs in a single inning',
'comeback': 'After trailing by ≥4 runs, a team comes back to within 1 run or surpasses.',
'extra-innings': '>9 innings.',
'extra-extra-innings': '≥13 innings.',
'flip-flop': 'The lead changes more than 3 times.'
}
TAG_COLORS = {
k: f'#{c[0]}{c[0]}{c[1]}{c[1]}{c[2]}{c[2]}'
for k, c
in zip(sorted(TAG_DEFS.keys()), itertools.permutations('FCAF', 3))
}
HASHTAGS = {
"ARI": "Diamondbacks",
"ATL": "Braves",
"BAL": "Orioles",
"BOS": "RedSox",
"CHC": "Cubs",
"CHW": "WhiteSox",
"CIN": "Reds",
"CLE": "Guardians",
"COL": "Rockies",
"DET": "Tigers",
"HOU": "Astros",
"KCR": "Royals",
"LAA": "Angels",
"LAD": "Dodgers",
"MIA": "Marlins",
"MIL": "Brewers",
"MIN": "Twins",
"NYM": "Mets",
"NYY": "Yankees",
"OAK": "Athletics",
"PHI": "Phillies",
"PIT": "Pirates",
"SDP": "Padres",
"SEA": "Mariners",
"SFG": "Giants",
"STL": "Cardinals",
"TBR": "Rays",
"TEX": "Rangers",
"TOR": "BlueJays",
"WSN": "Nationals",
}
def getBoxscore(then):
if not os.path.exists('data'):
os.makedirs('data')
fn = f"data/{then.isoformat()}.json"
if os.path.exists(fn):
with open(fn, "r") as handle:
return json.load(handle)
games_then = Boxscores(then)
data = []
for key in games_then.games.keys():
print(key)
for game in games_then.games[key]:
# print(game['home_abbr'], game['away_abbr'], game['boxscore'])
box = Boxscore(game["boxscore"])
# print(box, dir(box))
data.append(
{
"home_abbr": game["home_abbr"],
"away_abbr": game["away_abbr"],
"home_name": game["home_name"],
"away_name": game["away_name"],
"home_hashtag": HASHTAGS[game["home_abbr"]],
"away_hashtag": HASHTAGS[game["away_abbr"]],
"time": box.time,
"box": box.summary,
}
)
with open(fn, "w") as handle:
json.dump(data, handle)
return data
def fN(d):
return [0 if x is None else x for x in d]
def weave(home, away):
cur_home = 0
cur_away = 0
for i, (h, a) in enumerate(zip(home, away)):
cur_away = sum(away[0 : i + 1])
yield ("top", "away", i + 1, cur_home, cur_away, getleader(cur_home, cur_away))
cur_home = sum(home[0 : i + 1])
yield ("bot", "home", i + 1, cur_home, cur_away, getleader(cur_home, cur_away))
def getleader(h, a):
if h > a:
return "home"
elif a > h:
return "away"
else:
return "none"
def tags(box):
home = fN(box["home"])
away = fN(box["away"])
sh = sum(home)
ah = sum(away)
t = []
# Boring game
if sh <= 2 and ah <= 2:
t.append("pitchers-duel")
# High scoring (together over 15) OR (one side ≥ 9)
if sh + ah > 15 or sh > 9 or ah > 9:
t.append("high-scoring")
# Big inning (more than 5 in one inning)
if any(x >= 5 for x in home) or any(x >= 5 for x in away):
t.append("big-inning")
# comeback (one team is behind by ≥4 and then comes within -1 point or beats)
comeback_condition = None
comeback = False
for tb, howy, inning, cur_home, cur_away, leader in weave(home, away):
# print(tb, inning, comeback_condition, cur_away - cur_home)
if comeback_condition is None:
if abs(cur_home - cur_away) >= 4:
comeback_condition = leader
else:
# If the leader has changed, or the leader hasn't, and the
# difference has shrunk to 1 or 0 (i.e. trailing by 1, or tied.)
if leader != comeback_condition or abs(cur_home - cur_away) <= 1:
comeback = True
if comeback:
t.append("comeback")
# Flip-flop (lead changes more than 3 times)
leads = []
for tb, howy, inning, cur_home, cur_away, leader in weave(home, away):
# If our side is winning
if cur_home > cur_away:
# And the last inning we weren't winning (i.e. remove runs of the
# same value, to make analysis simpler.)
if len(leads) == 0 or leads[-1] != "home":
leads.append("home")
elif cur_away > cur_home:
if len(leads) == 0 or leads[-1] != "away":
leads.append("away")
if len(leads) > 3:
t.append("flip-flop")
# Extra innings
if max(len(home), len(away)) >= 13:
t.append('extra-extra-innings')
elif max(len(home), len(away)) > 9:
t.append('extra-innings')
return t
def color4tag(tag):
return TAG_COLORS[tag]
def renderPlain(data):
out = ""
for d in sorted(data, key=lambda x: -len(x['tags'])):
where = f"{d['away_name']:>22s} @ {d['home_name']:<22s}"
out += f"{where} {', '.join(d['tags'])}\n"
return out
def renderToot(data, standalone, date):
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape()
)
template = env.get_template("mlb.toot")
games = sorted(data, key=lambda x: -len(x['tags']))
kw = dict(games=games, tag_defs=TAG_DEFS,
standalone=standalone, date=date)
return template.render(**kw)
def renderHtml(data, standalone, date):
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("templates"),
autoescape=select_autoescape()
)
template = env.get_template("mlb.html")
games = sorted(data, key=lambda x: -len(x['tags']))
with open('.git/refs/heads/main', 'r') as handle:
git_commit = handle.read().strip()[0:12]
kw = dict(games=games, tag_defs=TAG_DEFS,
color=color4tag, git_commit=git_commit,
standalone=standalone, date=date)
return template.render(**kw)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="What to watch: MLB")
parser.add_argument(
"--date",
help="Must be ISO formatted date, e.g. 2021-12-31, defaults to yesterday",
)
parser.add_argument(
"--html",
action='store_true',
help='Return output as HTML that could be sent in an email'
)
parser.add_argument(
"--toot",
action='store_true',
help='Send toot.'
)
parser.add_argument(
"--sesv2",
action='store_true',
help='Return output in sesv2 json'
)
parser.add_argument(
"--github-pages",
action='store_true',
help='Make the HTML report standalone (head/body/etc.) and store it in docs'
)
parser.add_argument(
"--json",
action='store_true',
help='Return output in JSON'
)
args = parser.parse_args()
if args.date:
then = datetime.fromisoformat(args.date).date()
else:
# yesterday by default
then = datetime.today().date() - timedelta(days=1)
print(then)
data = getBoxscore(then)
for d in data:
d['tags'] = tags(d['box'])
del d['box']
if 'home_hashtag' not in d:
d.update({
"home_hashtag": HASHTAGS[d["home_abbr"]],
"away_hashtag": HASHTAGS[d["away_abbr"]],
})
if args.json:
print(json.dumps(data))
elif args.toot:
from mastodon import Mastodon
server = 'https://galaxians.garden'
token = os.environ['FEDI_ACCESS_TOKEN']
mastodon = Mastodon(
access_token = token,
api_base_url = server
)
tooter = mastodon.toot(renderToot(data, True, then))
print("tooted: ", tooter['uri'])
elif args.html:
if args.github_pages:
with open(f'docs/{then.isoformat()}.html', 'w') as handle:
handle.write(renderHtml(data, True, then))
with open(f'docs/index.md', 'w') as handle:
handle.write("# MLB: What to Watch\n\n")
for f in sorted(glob.glob("docs/*.html")):
handle.write(f"- [{f[5:-5]}]({f[5:]})\n")
else:
print(renderHtml(data, False, then))
elif args.sesv2:
# import boto3
# from botocore.exceptions import ClientError
SENDER = "Galaxians Sports <[email protected]>"
AWS_REGION = "eu-central-1"
SUBJECT = "MLB: What to Watch"
CHARSET = "UTF-8"
# client = boto3.client('ses',region_name=AWS_REGION)
# # Try to send the email.
# try:
# #Provide the contents of the email.
# response = client.send_email(
z = dict(
ListManagementOptions={
'ContactListName': 'HelkiaContactList',
'TopicName': 'MLB-W2W'
},
Destination={
'ToAddresses': ["[email protected]"]
},
Content={
'Simple': {
'Body': {
'Html': {
'Charset': CHARSET,
'Data': renderHtml(data, False, then),
},
'Text': {
'Charset': CHARSET,
'Data': renderPlain(data),
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
}
},
FromEmailAddress=SENDER,
)
print(json.dumps(z))
# Display an error if something goes wrong.
# except ClientError as e:
# print(e.response['Error']['Message'])
# else:
# print("Email sent! Message ID:"),
# print(response['MessageId'])
else:
print(renderPlain(data))