@@ -0,0 +1,2140 @@
+import pandas as pd%0Aimport requests%0Aimport datetime%0Aimport warnings%0Aimport io%0A%0Adef validate_datestring(date_text):%0A    try:%0A        datetime.datetime.strptime(date_text, '%25Y-%25m-%25d')%0A    except ValueError:%0A        raise ValueError(%22Incorrect data format, should be YYYY-MM-DD%22)%0A%0Adef sanitize_input(start_dt, end_dt, batter_id):%0A%09# this id should be a string to place inside a url%0A%09batter_id = str(batter_id)%0A%0A%09# if no dates are supplied, assume they want yesterday's data%0A%09# send a warning in case they wanted to specify%0A%09if start_dt is None and end_dt is None:%0A%09%09today = datetime.datetime.today()%0A%09%09start_dt = (today - datetime.timedelta(1)).strftime(%22%25Y-%25m-%25d%22)%0A%09%09end_dt = today.strftime(%22%25Y-%25m-%25d%22)%0A%0A%09%09print(%22Warning: no date range supplied. Returning yesterday's Statcast data. For a different date range, try get_statcast(start_dt, end_dt).%22)%0A%0A%09#if only one date is supplied, assume they only want that day's stats%0A%09#query in this case is from date 1 to date 1%0A%09if start_dt is None:%0A%09%09start_dt = end_dt%0A%09if end_dt is None:%0A%09%09end_dt = start_dt%0A%0A%09# now that both dates are not None, make sure they are valid date strings%0A%09validate_datestring(start_dt)%0A%09validate_datestring(end_dt)%0A%09return start_dt, end_dt, batter_id%0A%0Adef statcast_batter(start_dt=None, end_dt=None, batter_id=None):%0A%09%22%22%22 %0A%09Pulls statcast data from Baseball Savant.%0A%09%22%22%22%0A%09start_dt, end_dt, batter_id = sanitize_input(start_dt, end_dt, batter_id)%0A%0A%09# inputs are valid if either both or zero dates are supplied. Not valid of only one given.%0A%09if start_dt and end_dt:%0A%09%09url = 'https://baseballsavant.mlb.com/statcast_search/csv?all=true&hfPT=&hfAB=&hfBBT=&hfPR=&hfZ=&stadium=&hfBBL=&hfNewZones=&hfGT=R%257CPO%257CS%257C=&hfSea=&hfSit=&player_type=batter&hfOuts=&opponent=&pitcher_throws=&batter_stands=&hfSA=&game_date_gt=%7B%7D&game_date_lt=%7B%7D&player_lookup%255B%255D=%7B%7D&team=&position=&hfRO=&home_road=&hfFlag=&metric_1=&hfInn=&min_pitches=0&min_results=0&group_by=name&sort_col=pitches&player_event_sort=h_launch_speed&sort_order=desc&min_abs=0&type=details&'.format(start_dt,end_dt,batter_id)%0A%09%09s=requests.get(url).content%0A%09%09df = pd.read_csv(io.StringIO(s.decode('utf-8')))%0A%09%09return df%0A

create statcast batter function for getting a single batter's statcast data