/
summary.py
1366 lines (1115 loc) · 48.4 KB
/
summary.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from bs4 import BeautifulSoup
from datetime import date, datetime
from dateutil.relativedelta import relativedelta
from decimal import Decimal
from io import BytesIO
import json
from lxml import html, etree
import re
import requests
from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound
import sys
from time import sleep
from utils import get_logger, utc_to_local, replace_accents
from managers import get_or_create_player
from models import Game, Team, Player, BatterGame, PitcherGame, FantasyAverage
log = get_logger(__name__)
def seek(game_date, from_file=False, force_overwrite=False):
# 2018 season started 3-29
log.info("")
log.info("----<>----<>----<>----<>----<>----<>----<>----<>----<>----<>----<>----<>----<>----<>----")
if not game_date:
game_date = timezone.now().date()
log.info('<[ {} ]>'.format(game_date))
# get games scoreboard for the day
games, scoreboard = get_games(game_date, from_file)
# iterate and process each game
for game_id, game in games.items():
# 2019 Japan games
if game_date == date(2019,3,20) and game_id != 'mlb.g.390320111' \
or game_date == date(2019, 3, 21) and game_id != 'mlb.g.390321111':
log.debug('Skipping {} {}'.format(game_date, game_id))
continue
# temp placeholder
yahoo_statuses = ['pregame', 'rain-delay', 'delay', 'postponed', 'cancelled', 'suspended',
'in_progress', 'final']
status = game['status_type']
if game_id not in scoreboard:
# log.info('Game {} not in scoreboard, skipping'.format(game_id))
continue
if status in ['suspended', 'in_progress', 'final']:
#log.info(game_type)
log.info('Processing {} ({})'.format(game_id, game['status_type']))
cur_game, game_data, stats = get_box(
suffix=game['navigation_links']['boxscore']['url'],
game_id=game_id,
game=game,
game_date=game_date,
from_file=from_file)
cur_game = parse_game_data(
cur_game=cur_game,
game_date=game_date,
game_id=game_id,
game=game,
status=status,
game_data=game_data,
from_file=from_file)
bstats = parse_batting_notes(
away_batting=game_data['section_notes']['g.awayBatting']['0'],
home_batting=game_data['section_notes']['g.homeBatting']['0'],
pitching_notes=game_data['section_notes']['g.pitching']['0'],
lineups=game_data['lineups'],
game_id=game_id)
pstats = parse_pitching_notes(
pitching_notes=game_data['section_notes']['g.pitching']['0'],
lineups=game_data['lineups'],
game_id=game_id
)
parse_players(
game=cur_game,
stats=stats['playerStats'],
lineups=game_data['lineups'],
pstats=pstats,
bstats=bstats,
game_id=game_id,
cur_game=cur_game
)
# else:
# log.debug('Skipping {} {}'.format(game_id, game['status_type']))
def get_games(game_date, from_file):
# get the scoreboard for a given date
base = 'https://sports.yahoo.com/mlb/scoreboard/?confId=&schedState=2&dateRange='
url = base + '{}-{}-{}'.format(game_date.year, f'{game_date.month:02}', f'{game_date.day:02}')
tries = 0
while tries < 5:
if from_file:
fh = open("../Pages/2018-03-29.html")
page = fh.read()
fh.close()
else:
page = get_page(url)
try:
stores = get_stores(source=page)
except Exception:
log.warning("Bad request...")
tries += 1
if tries == 5:
break
sleep(1)
continue
games = stores['GamesStore']['games']
scoreboard = stores['ScoreboardStore']['games'] # array of game 'valid' IDs
return games, scoreboard
raise Exception('Max retries hit')
def get_box(suffix, game_id, game, game_date, from_file=False):
tries = 0
while tries < 5:
if from_file:
#filename = "380329130.html"
filename = "380329128.html"
log.debug('filename ' + filename)
fh = open("../Pages/" + filename)
page = fh.read()
fh.close()
else:
base = 'https://sports.yahoo.com'
url = base + suffix
page = get_page(url)
try:
stores = get_stores(source=page)
except Exception:
log.warning("Bad request...")
tries += 1
if tries == 5:
break
sleep(1)
continue
stats = stores['StatsStore']
game_data = stores['GamesStore']['games'][game_id]
cur_game = Game()
# Thu, 29 Mar 2018 20:10:00 +0000
cur_game.year = game_date.year
cur_game.starttime = datetime.strptime(game['start_time'], '%a, %d %b %Y %H:%M:%S %z')
# convert timestamp to PxT so date is accurate
cur_game.start = utc_to_local(cur_game.starttime).date()
cur_game.away_team = session.query(Team).filter(Team.yahoo_id==int(game['away_team_id'].replace('mlb.t.', ''))).one()
cur_game.home_team = session.query(Team).filter(Team.yahoo_id==int(game['home_team_id'].replace('mlb.t.', ''))).one()
# create any missing players
check_players(
players=stores['PlayersStore']['players'],
away_team=cur_game.away_team,
home_team=cur_game.home_team,
lineups=game_data['lineups'],
game_date=cur_game.start
)
return cur_game, game_data, stats
raise Exception('Max retries hit')
def parse_game_data(cur_game, game_date, game_id, game, status, game_data, from_file=False):
cur_game.set_status(status, session)
cur_game.extract_yahoo_id(game_id) # maybe overkill
for old_game in session.query(Game).filter(Game.yahoo_id==cur_game.yahoo_id):
log.info('Found dup game {}, deleting...'.format(old_game.yahoo_id))
for batter_game in session.query(BatterGame).filter(BatterGame.game==old_game):
session.delete(batter_game)
for pitcher_game in session.query(PitcherGame).filter(PitcherGame.game==old_game):
session.delete(pitcher_game)
session.delete(old_game)
session.commit()
# TODO: fix this
# capture progress for live games
# if status == 'live':
# cur_game.progress = game.find(name='span').text
# TODO: fix this?
#if status != 'Upcoming':
cur_game.away_runs = game['away_team_stats'][0]['runs']
cur_game.away_hits = game['away_team_stats'][1]['hits']
cur_game.away_errors = game['away_team_stats'][2]['errors']
# TODO: fix this?
#if status != 'Upcoming':
cur_game.home_runs = game['home_team_stats'][0]['runs']
cur_game.home_hits = game['home_team_stats'][1]['hits']
cur_game.home_errors = game['home_team_stats'][2]['errors']
log.info("{} {} @ {} {}".format(
cur_game.away_team.full_name(), cur_game.away_runs,
cur_game.home_team.full_name(), cur_game.home_runs))
# inning-level runs
inning_data = {}
max_inning = None
for period in game_data['game_periods']:
inning = int(period['period_id'])
inning_data[inning] = [period['away_points'], period['home_points']]
max_inning = inning
cur_game.inning_data = inning_data
cur_game.innings = max_inning
session.add(cur_game)
session.commit()
return cur_game
def check_players(players, away_team, home_team, lineups, game_date):
for player, data in players.items():
if player in lineups["away_lineup"]["all"].keys():
team = away_team
elif player in lineups["home_lineup"]["all"].keys():
team = home_team
else:
#log.debug('Player {} not found in lineups'.format(player))
#raise Exception('Player {} not found in lineups'.format(player))
continue
#log.debug('Checking player {}'.format(player))
yahoo_id = int(player.replace('mlb.p.', ''))
get_or_create_player(session=session, yahoo_id=yahoo_id, team=team, data=data, game_date=game_date)
return
def get_page(url):
log.info(url)
headers = {
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/72.0.3626.121 Safari/537.36'
}
tries = 0
max_tries = 5
while tries < max_tries:
page = requests.get(url, headers=headers)
if page.status_code == 200:
return page.content
else:
log.warning('{} trying to get {}'.format(page.status_code, url))
tries += 1
sleep(1)
raise Exception('{} failures trying to get {}'.format(max_tries, url))
def get_stores(source):
soup = BeautifulSoup(source, 'html.parser')
scripts = soup.find_all("script")
for script in scripts:
if 'root.App.main' in script.text:
for line in script.text.split('\n'):
if 'root.App.main' in line:
# trim data for json-izing
line = line.replace('root.App.main =', '')
line = line[:-1]
react_js = json.loads(line)
return react_js['context']['dispatcher']['stores']
# ta daaaaaaaa, our entity IDs
# st = fin['context']['dispatcher']['stores']['ScoreboardStore']['games']
raise Exception("Couldn't find react js in source.")
def parse_pitching_notes(pitching_notes, lineups, game_id):
pitching_order = parse_lineup(
lineup=lineups['away_lineup_order']['P'] + lineups['home_lineup_order']['P'],
ltype='pitching lineups',
game_id=game_id
)
# Pitches-strikes
pstats = {}
index = 0
start_index = -1
for dct in pitching_notes:
if dct['header'] == 'Pitches-strikes':
start_index = index
break
index += 1
if start_index == -1:
raise Exception('Pitches-strikes not found in pitching_notes')
pitching_notes[start_index]['text'] = replace_accents(pitching_notes[start_index]['text'])
raw = pitching_notes[start_index]['text'].split(',')
pitchers_count = len(raw)
for item in raw:
m = re.match('(.*) (\d+)-(\d+).*', item)
if not m:
raise Exception('Pitches-strikes regex failed on {}'.format(item))
pname = m.group(1).lstrip()
pitcher = get_player_from_order(
order=pitching_order,
pname=pname,
stat_name='Pitches-strikes',
game_id=game_id
)
pstats[pitcher.id] = {
'pitches': m.group(2),
'strikes': m.group(3),
'ground': 0,
'fly': 0,
'batters': 0,
}
# Ground balls-fly balls
seen = []
start_index += 1
if pitching_notes[start_index]['header'] != 'Ground balls-fly balls':
raise Exception('Ground balls-fly balls not found')
raw = pitching_notes[start_index]['text'].split(',')
if pitchers_count != len(raw):
log.warning('Expecting {} pitchers in Ground balls-fly balls, found {}'.format(pitchers_count, len(raw)))
for item in raw:
m = re.match('(.*) (\d+)-(\d+).*', item)
if not m:
raise Exception('Ground balls-fly balls regex failed on {}'.format(item))
pname = m.group(1).lstrip()
pitcher = get_player_from_order(
order=pitching_order,
pname=pname,
stat_name='Pitches-strikes',
game_id=game_id
)
pstats[pitcher.id]['ground'] = m.group(2)
pstats[pitcher.id]['fly'] = m.group(3)
# Batters faced
start_index += 1
if pitching_notes[start_index]['header'] != 'Batters faced':
raise Exception('Batters faced not found')
raw = pitching_notes[start_index]['text'].split(',')
# if pitchers_count != len(raw):
# log.warning('Expecting {} pitchers in Batters faced, found {}'.format(pitchers_count, len(raw)))
for item in raw:
# it appears yahoo puts no number if the batters faced is just 1
batters = 1
m = re.match('(.*) (\d+)', item)
if m:
batters = m.group(2)
pname = m.group(1).lstrip()
# else:
# pname = item.lstrip()
# log.warning('Batters faced regex failed on {}'.format(item))
pitcher = get_player_from_order(
order=pitching_order,
pname=pname,
stat_name='batters',
game_id=game_id
)
pstats[pitcher.id]['batters'] = batters
return pstats
def get_player_from_order(order, pname, stat_name, game_id):
pname = replace_accents(pname)
# clean up yahoo's mess
if game_id in ['mlb.g.380405123', 'mlb.g.380330106', 'mlb.g.380401306', 'mlb.g.380401206'] \
and pname == 'F Rivero':
pname = 'F Vazquez'
# 8/23/18 O P??rez
elif game_id == 'mlb.g.380823102' and pname.startswith('O P') and pname.endswith('ez'):
pname = 'O Perez'
# 8/23/18 J Garc??a
elif game_id == 'mlb.g.380823128' and pname.startswith('J Garc') and pname.endswith('a'):
pname = 'J Garcia'
pnames = [pname]
if pname == 'M Wright Jr':
pnames.append('M Wright')
elif pname == 'V Nuno':
pnames.append('V Nuno III')
elif pname == 'D Poncedeleon':
pnames.append('D Ponce de Leon')
for pname in pnames:
try:
return order[pname]
except KeyError:
pass
raise Exception('{} could not find {} in {}'.format(stat_name, pnames, order.keys()))
def parse_batting_notes(away_batting, home_batting, pitching_notes, lineups, game_id):
away_batting_order = parse_lineup(lineup=lineups['away_lineup_order']['B'], ltype='away batter', game_id=game_id)
home_batting_order = parse_lineup(lineup=lineups['home_lineup_order']['B'], ltype='home batter', game_id=game_id)
bstats = {}
for i in range(0, 2):
if i == 0:
batting_order = away_batting_order
ltype = 'away'
source = away_batting
else:
batting_order = home_batting_order
ltype = 'home'
source = home_batting
# 2B, 3B, GIDP
stypes = ['2B', '3B', 'GIDP', 'SF']
for j in range(0, 4):
stype = stypes[j]
index = 0
start_index = -1
for dct in source:
if dct['header'] == stype:
start_index = index
break
index += 1
if start_index == -1:
#log.info('No {} for {} team'.format(stype, ltype))
continue
#log.debug('found {} at {} - {}'.format(stype, start_index, source[start_index]['text']))
source[start_index]['text'] = replace_accents(source[start_index]['text'])
if stype in ['GIDP', 'SF']:
raw = source[start_index]['text'].split(',')
else:
raw = source[start_index]['text'].split('),')
#log.debug(raw)
for item in raw:
increment = 1
# "header": "2B",
# "text": "K Bryant (1), W Contreras (1), J Heyward (1), T La Stella (1)"
if stype in ['2B', '3B']:
# there's a following digit if > 1, e.g. X Bogaerts 2 (2)
m = re.match('(.*) (\d+) \(\d+.*', item)
if m:
bname = m.group(1).lstrip()
increment = int(m.group(2))
else:
m = re.match('(.*) \(\d+.*', item)
if m:
bname = m.group(1).lstrip()
else:
raise Exception('{} regex failed on {} [{}]'.format(stype, item, raw))
# "header": "GIDP",
# "text": "J Bour, M Rojas"
else:
m = re.match('(.*) (\d+)', item)
# there's a following digit if > 1, e.g. X Bogaerts 2
if m:
bname = m.group(1).lstrip()
increment = int(m.group(2))
else:
bname = item.lstrip()
# 8/23/18 Y D??az, E N????ez
if game_id == 'mlb.g.380823102':
if bname.startswith('Y D') and bname.endswith('az'):
bname = 'Y Diaz'
elif bname.startswith('E N') and bname.endswith('ez'):
bname = 'E Nunez'
# 8/23/18 E Su??rez, J B??ez
elif game_id == 'mlb.g.380823116':
if bname.startswith('E Su') and bname.endswith('ez'):
bname = 'E Suarez'
elif bname.startswith('J B') and bname.endswith('ez'):
bname = 'J Baez'
# 8/23/18 A Garc??a
elif game_id == 'mlb.g.380823106':
if bname.startswith('A Garc') and bname.endswith('a'):
bname = 'A Garcia'
elif game_id == 'mlb.g.380912124' and bname == 'D Poncedeleon':
bname = 'D Ponce de Leon'
if bname not in batting_order.keys():
raise Exception('Could not find {} {} in batting_order {}'.
format(stype, bname, batting_order.keys()))
batter = batting_order[bname]
#log.debug('{} {}'.format(stype, batter.name))
if batter.id not in bstats.keys():
bstats[batter.id] = {
'2B': 0,
'3B': 0,
'SF': 0,
'HBP': 0,
'GIDP': 0,
}
bstats[batter.id][stype] += increment
# HBP
away_pitching_order = parse_lineup(lineup=lineups['away_lineup_order']['P'], ltype='away pitcher', game_id=game_id)
home_pitching_order = parse_lineup(lineup=lineups['home_lineup_order']['P'], ltype='home pitcher', game_id=game_id)
index = 0
start_index = -1
for dct in pitching_notes:
if dct['header'] == 'HBP':
start_index = index
break
index += 1
if start_index > -1:
pitching_notes[start_index]['text'] = replace_accents(pitching_notes[start_index]['text'])
raw = pitching_notes[start_index]['text'].split(',')
#log.debug(raw)
for item in raw:
# {
# "header": "HBP",
# "text": "D Dietrich (by M Montgomery), J B\u00e1ez (by J Ure\u00f1a), A Rizzo (by J Ure\u00f1a), A Russell (by J Ure\u00f1a)"
# },
increment = 1
m = re.match('(.*) (\d+) \(by (.*)\)', item)
if m:
bname = m.group(1).lstrip()
increment = int(m.group(2).lstrip())
pname = m.group(3).lstrip()
else:
m = re.match('(.*) \(by (.*)\)', item)
if m:
bname = m.group(1).lstrip()
pname = m.group(2).lstrip()
else:
raise Exception('{} regex failed on {}'.format('HBP', item))
# 8/23/18 R Acu??a Jr
if game_id == 'mlb.g.380823128':
if bname[:5] == 'R Acu' and bname[-2:] == 'Jr':
bname = 'R Acuna Jr'
elif game_id in['mlb.g.380403118', 'mlb.g.380419106', 'mlb.g.380527130', 'mlb.g.380624115', 'mlb.g.380803113', \
'mlb.g.380902107', 'mlb.g.380915101']:
if pname == 'M Wright Jr':
pname = 'M Wright'
elif game_id in['mlb.g.380821119']:
if pname == 'D Poncedeleon':
pname = 'D Ponce de Leon'
elif game_id in['mlb.g.380905114', 'mlb.g.380926130']:
if pname == 'V Nuno':
pname = 'V Nuno III'
if bname in away_batting_order.keys() and pname in home_pitching_order.keys():
batter = away_batting_order[bname]
elif bname in home_batting_order.keys() and pname in away_pitching_order.keys():
batter = home_batting_order[bname]
else:
log.info('AB {}'.format(away_batting_order.keys()))
log.info('HP {}'.format(home_pitching_order.keys()))
log.info('HB {}'.format(home_batting_order.keys()))
log.info('AP {}'.format(away_pitching_order.keys()))
raise Exception('Could not find {} in batting orders and {} in pitching orders'.format(bname, pname))
if batter.id not in bstats.keys():
bstats[batter.id] = {
'2B': 0,
'3B': 0,
'SF': 0,
'HBP': 0,
'GIDP': 0,
}
bstats[batter.id]['HBP'] += increment
else:
#log.info('No {} found'.format('HBP'))
pass
return bstats
def parse_lineup(lineup, ltype, game_id):
player_order = {}
for plyr in lineup:
yahoo_id = int(plyr.replace('mlb.p.', ''))
player = session.query(Player).filter(Player.yahoo_id==yahoo_id).one()
pname = player.pname
if pname in player_order.keys():
# yahoo slop
if pname == 'C Joseph' \
and game_id in ['mlb.g.380619120', 'mlb.g.380911101', 'mlb.g.380914101', 'mlb.g.380918101',
'mlb.g.380924102', 'mlb.g.380926202']:
continue
elif pname == 'D Smith' \
and game_id in ['mlb.g.380623121', 'mlb.g.380624121', 'mlb.g.380629128', 'mlb.g.380819122',
'mlb.g.380822121', 'mlb.g.380907121', 'mlb.g.380909121', 'mlb.g.380913221',
'mlb.g.380917122', 'mlb.g.380918122', 'mlb.g.380923120', 'mlb.g.380925121',
'mlb.g.380927121', 'mlb.g.380929121']:
continue
elif pname == 'T Williams' and game_id in ['mlb.g.380922123']:
continue
# doubled up in the lineup
elif pname == 'W Difo' and game_id in ['mlb.g.380701122']:
continue
elif pname == 'M Reynolds' and game_id in ['mlb.g.380727128', 'mlb.g.380728128']:
continue
# 8/21/18 doubled up in the lineup
elif pname == 'K Herrera' and game_id in ['mlb.g.380821120']:
continue
# 9/8/18 doubled up in the lineup
elif pname == 'G Holland' and game_id in ['mlb.g.380908320']:
continue
# 9/13/18 doubled up in the lineup
elif pname == 'Y Lopez' and game_id in ['mlb.g.380913127']:
continue
log.debug('LINEUP {}'.format(lineup))
log.debug('PLAYER ORDER {}'.format(player_order.keys()))
raise Exception("Duplicate {} {}".format(ltype, pname))
player_order[pname] = player
if player.alt_name():
player_order[player.alt_name()] = player
return player_order
def parse_players(game, stats, lineups, pstats, bstats, game_id, cur_game):
# "playerStats": {
# "mlb.p.7790": {
# "mlb.stat_variation.2": {
# "mlb.stat_type.4": "0",
# AB R H RBI HR SB BB K LOB AVG
for player, data in stats.items():
if 'mlb.stat_variation.2' not in data.keys():
#log.debug('Player {} in stats, but does not appear to have played.'.format(player))
continue
# 4/2/18 Sisco is missing
elif game_id == 'mlb.g.380402118' and player == 'mlb.p.10155':
lineups['home_lineup']['all'][player] = 1
# 4/17/18 Tepera is missing
elif game_id == 'mlb.g.380417314' and player == 'mlb.p.9959':
lineups['away_lineup']['P'][player] = {'order': '3'}
lineups['home_lineup']['all'][player] = 1
pstats[541] = {'pitches': 26, 'strikes': 19, 'ground': 2, 'fly': 0, 'batters': 5}
# 6/18/18 ghost players
elif game_id == 'mlb.g.380618120' \
and player in ['mlb.p.9100', 'mlb.p.8179', 'mlb.p.10293', 'mlb.p.8641', 'mlb.p.9642', 'mlb.p.9642',
'mlb.p.9224', 'mlb.p.8289', 'mlb.p.9603', 'mlb.p.9768', 'mlb.p.7746', 'mlb.p.10624',
'mlb.p.7071', 'mlb.p.9097', 'mlb.p.9251']:
continue
# 6/19/18 C Joseph
elif game_id == 'mlb.g.380619120':
bstats[154] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 6/19/18 D Smith
elif game_id == 'mlb.g.380623121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 22, 'strikes': 9, 'ground': 2, 'fly': 0, 'batters': 5}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 6/24/18 D Smith
elif game_id == 'mlb.g.380624121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 21, 'strikes': 13, 'ground': 2, 'fly': 0, 'batters': 5}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 6/29/18 D Smith
elif game_id == 'mlb.g.380629128':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 12, 'strikes': 5, 'ground': 2, 'fly': 1, 'batters': 4}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 7/27/18 M Reynolds
elif game_id == 'mlb.g.380727128':
# Mark 8030
bstats[1021] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Matt 9913
bstats[783] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 7/28/18 M Reynolds
elif game_id == 'mlb.g.380728128':
# Mark 8030
bstats[1021] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Matt 9913
bstats[783] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 8/19/18 D Smith
elif game_id == 'mlb.g.380819122':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 8, 'strikes': 6, 'ground': 2, 'fly': 0, 'batters': 3}
# Dominic
bstats[1005] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 8/22/18 D Smith
elif game_id == 'mlb.g.380822121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 11, 'strikes': 6, 'ground': 0, 'fly': 1, 'batters': 4}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/7/18 D Smith
elif game_id == 'mlb.g.380907121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 22, 'strikes': 14, 'ground': 2, 'fly': 2, 'batters': 5}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/8/18 ghost players - weird because M Reynolds has stats for 9/8 (dbl-header)
elif game_id == 'mlb.g.380908320' and player in ['mlb.p.8030']:
continue
# 9/9/18 D Smith
elif game_id == 'mlb.g.380909121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 21, 'strikes': 15, 'ground': 2, 'fly': 4, 'batters': 7}
# Dominic
bstats[1005] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/11/18 C Joseph
elif game_id == 'mlb.g.380619120':
# Caleb
bstats[154] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Corban
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/13/18 D Smith
elif game_id == 'mlb.g.380913221':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 16, 'strikes': 9, 'ground': 2, 'fly': 0, 'batters': 5}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/14/18 C Joseph
elif game_id == 'mlb.g.380914101':
# Caleb
bstats[154] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Corban
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/17/18 D Smith
elif game_id == 'mlb.g.380917122':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 8, 'strikes': 4, 'ground': 1, 'fly': 1, 'batters': 2}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/18/18 D Smith
elif game_id == 'mlb.g.380918122':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 16, 'strikes': 12, 'ground': 3, 'fly': 1, 'batters': 5}
# Dominic
bstats[1005] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/18/18 C Joseph
elif game_id == 'mlb.g.380918101':
# Caleb
bstats[154] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Corban
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/22/18 T Williams
elif game_id == 'mlb.g.380922123':
# Taylor
bstats[801] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[801] = {'pitches': 15, 'strikes': 10, 'ground': 2, 'fly': 0, 'batters': 3}
# Trevor
bstats[699] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[699] = {'pitches': 95, 'strikes': 65, 'ground': 6, 'fly': 4, 'batters': 22}
# 9/23/18 D Smith
elif game_id == 'mlb.g.380923120':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 25, 'strikes': 20, 'ground': 3, 'fly': 1, 'batters': 8}
# Dominic
bstats[1005] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/24/18 C Joseph
elif game_id == 'mlb.g.380924102':
# Caleb
bstats[154] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Corban
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/25/18 D Smith
elif game_id == 'mlb.g.380925121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 13, 'strikes': 7, 'ground': 0, 'fly': 0, 'batters': 3}
# Dominic
bstats[1005] = {'2B': 1, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/26/18 C Joseph
elif game_id == 'mlb.g.380926202':
# Caleb
bstats[154] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# Corban
bstats[1137] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/27/18 D Smith
elif game_id == 'mlb.g.380927121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 6, 'strikes': 5, 'ground': 1, 'fly': 1, 'batters': 3}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
# 9/29/18 D Smith
elif game_id == 'mlb.g.380929121':
# Drew
bstats[1153] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
pstats[1153] = {'pitches': 11, 'strikes': 7, 'ground': 0, 'fly': 2, 'batters': 3}
# Dominic
bstats[1005] = {'2B': 0, '3B': 0, 'HBP': 0, 'GIDP': 0, 'SF': 0}
if player in lineups['home_lineup']['P'].keys():
spot = int(lineups['home_lineup']['P'][player]['order'])
elif player in lineups['away_lineup']['P'].keys():
spot = int(lineups['away_lineup']['P'][player]['order'])
else:
spot = -1
if player in lineups['away_lineup']['all'].keys():
team = cur_game.away_team
elif player in lineups['home_lineup']['all'].keys():
team = cur_game.home_team
else:
log.error(lineups['away_lineup']['all'].keys())
log.error(lineups['home_lineup']['all'].keys())
log.error(stats.keys())
log.error(player)
log.error(data)
raise Exception('Player not found in lineups {}'.format(player))
yahoo_id = int(player.replace('mlb.p.', ''))
player = session.query(Player).filter(Player.yahoo_id==yahoo_id).one()
# TODO: clean up, enum or dict?
batting = {
'AB': 'mlb.stat_type.2',
'R': 'mlb.stat_type.3',
'H': 'mlb.stat_type.4',
'HR': 'mlb.stat_type.7',
'RBI': 'mlb.stat_type.8',
'SB': 'mlb.stat_type.12',
'BB': 'mlb.stat_type.14',
'K': 'mlb.stat_type.17',
'AVG': 'mlb.stat_type.23',
'LOB': 'mlb.stat_type.53'
}
pitching = {
'W': 'mlb.stat_type.101',
'L': 'mlb.stat_type.102',
'SV': 'mlb.stat_type.107',
'H': 'mlb.stat_type.111',
'R': 'mlb.stat_type.113',
'ER': 'mlb.stat_type.114',
'HR': 'mlb.stat_type.115',
'BB': 'mlb.stat_type.118',
'K': 'mlb.stat_type.121',
'HLD': 'mlb.stat_type.136',
'IP': 'mlb.stat_type.139',
'ERA': 'mlb.stat_type.140',
'WHIP': 'mlb.stat_type.141',
'BS': 'mlb.stat_type.147',
}
# TODO: batting order, sub/PH
data = data['mlb.stat_variation.2']
if batting['AB'] in data.keys():
if player.id in bstats.keys():
doubles = bstats[player.id]['2B']
triples = bstats[player.id]['3B']
hbp = bstats[player.id]['HBP']
sac_fly = bstats[player.id]['SF']
gidp = bstats[player.id]['GIDP']
else:
doubles = 0
triples = 0
hbp = 0
sac_fly = 0
gidp = 0