-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_Winning_Stats_Analyzer.py
More file actions
113 lines (91 loc) · 5.05 KB
/
common_Winning_Stats_Analyzer.py
File metadata and controls
113 lines (91 loc) · 5.05 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
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
# Author
# Brian Schroeder
# Synopsis
# This program gets the winner for each game and outputs the stats they were leading in.
# The Output will be the total number of Games that category was higher for the winning team vs the losing
import requests
import json
from collections import Counter
from prettytable import PrettyTable
import datetime
batting_winningStats = []
pitching_winningStats = []
categories = 'batting', 'pitching'
#Format for Dates: Day,Month,Year
startDate = "01-01-2019"
endDate = "31-12-2019"
# List of Stats that are better when lower
batting_stats_adjustment = "atBatsPerHomeRun", "caughtStealing", "flyOuts", "groundIntoDoublePlay", "groundIntoTriplePlay", "groundOuts", "leftOnBase", "strikeOuts"
pitching_stats_adjustment = "airOuts", "atBats", "balks", "baseOnBalls", "battersFaced", "doubles", "earnedRuns", "era", "hitBatsmen", "hitByPitch", "hits", "homeRuns", "homeRunsPer9", "inheritedRunners", "inheritedRunnersScored", "intentionalWalks", "obp", "rbi", "runs", "runsScoredPer9", "sacBunts", "sacFlies", "stolenBasePercentage", "stolenBases", "triples", "whip", "wildPitches"
def mlb_games(*gamedates):
for gamedate in gamedates:
try:
request = requests.get(f"http://statsapi.mlb.com/api/v1/schedule/games/?sportId=1&date={gamedate}").text
request_json = json.loads(request)
games = (request_json['dates'][0]['games'])
except:
continue
for game in games:
for category in categories:
request = requests.get(f"http://statsapi.mlb.com{(game['link'])}").text
request_json = json.loads(request)
homeStats = (request_json['liveData']['boxscore']['teams']['home']['teamStats'][category])
awayStats = (request_json['liveData']['boxscore']['teams']['away']['teamStats'][category])
try:
if (game['teams']['home']['isWinner']) == True:
for key in homeStats.keys():
if key in pitching_stats_adjustment or batting_stats_adjustment:
if homeStats[key] < awayStats[key]:
if category == 'batting':
batting_winningStats.append(key)
if category == 'pitching':
pitching_winningStats.append(key)
else:
if homeStats[key] > awayStats[key]:
if category == 'batting':
batting_winningStats.append(key)
if category == 'pitching':
pitching_winningStats.append(key)
else:
for key in homeStats.keys():
if key in pitching_stats_adjustment or batting_stats_adjustment:
if awayStats[key] < homeStats[key]:
if category == 'batting':
batting_winningStats.append(key)
if category == 'pitching':
pitching_winningStats.append(key)
else:
if awayStats[key] > homeStats[key]:
if category == 'batting':
batting_winningStats.append(key)
if category == 'pitching':
pitching_winningStats.append(key)
except:
continue
def dateRange(startDate,endDate):
dates = []
start = datetime.datetime.strptime(f"{startDate}", "%d-%m-%Y")
end = datetime.datetime.strptime(f"{endDate}", "%d-%m-%Y")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]
for date in date_generated:
dates.append(date.strftime("%m/%d/%Y"))
return(dates)
dates = dateRange(startDate,endDate)
for date in dates:
mlb_games(date)
common_batting_winning_stats = (dict(Counter(batting_winningStats)))
common_pitching_winning_stats = (dict(Counter(pitching_winningStats)))
batting_stats_table = PrettyTable(['Stat', 'Amount of (Games) Winning Team Won the Category'])
pitching_stats_table = PrettyTable(['Stat', 'Amount of (Games) Winning Team Won the Category'])
for key, val in common_batting_winning_stats.items():
batting_stats_table.add_row([key, val])
for key, val in common_pitching_winning_stats.items():
pitching_stats_table.add_row([key, val])
batting_stats_table.sortby = 'Amount of (Games) Winning Team Won the Category'
pitching_stats_table.sortby = 'Amount of (Games) Winning Team Won the Category'
batting_stats_table.reversesort = True
pitching_stats_table.reversesort = True
print('\n\nCommon Winning Leading Batting Categories\n\n')
print(batting_stats_table)
print('\n\nCommon Winning Leading Pitching Categories\n\n')
print(pitching_stats_table)