Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import MySQLdb
- import pandas as pd
- from multiprocessing import Process, freeze_support
- from urllib.error import HTTPError
- from tqdm import tqdm
- import time
- db = MySQLdb.connect('tcbinstance.cwudbgjghls4.us-east-1.rds.amazonaws.com', 'artvandelay', 'gumby1234', db='mlb')
- c = db.cursor()
- c.execute("SELECT max(game_date) FROM mlb.pitchdata;")
- test = c.fetchall()
- latest_date = test[0][0]
- teams_a = ['LAA', 'HOU', 'OAK', 'TOR', 'ATL', 'MIL', 'STL','CHC']
- teams_b = ['ARI', 'LAD', 'SF', 'CLE', 'SEA', 'MIA', 'NYM', 'WSH']
- teams_c = ['BAL', 'SD', 'PHI', 'PIT', 'TEX','TB', 'BOS', 'CIN']
- teams_d = ['COL', 'KC', 'DET', 'MIN', 'CWS', 'NYY']
- teams = [teams_a, teams_b, teams_c, teams_d]
- def upload(file):
- for team in file:
- successful = False
- backoff_time = 30
- while not successful:
- try:
- year = 2018
- link = 'https://baseballsavant.mlb.com/statcast_search/csv?all=true&hfPT=&hfAB=&hfBBT=&hfPR=&hfZ=&stadium=&hfBBL=&hfNewZones=&hfGT=&hfC=&hfSea=' + str(
- year) + '%7C&hfSit=&player_type=pitcher&hfOuts=&opponent=&pitcher_throws=&batter_stands=&hfSA=&game_date_gt=&game_date_lt=&team=' + team + '&position=&hfRO=&home_road=&hfFlag=&metric_1=&hfInn=&min_pitches=0&min_results=0&group_by=name-event&sort_col=pitches&player_event_sort=api_p_release_speed&sort_order=desc&min_abs=0&type=details&'
- data = pd.read_csv(link, low_memory=False)
- data['game_date'] = pd.to_datetime(data['game_date'])
- data = data[(data['game_date'] > latest_date)]
- print(data)
- successful = True
- except (HTTPError) as e:
- # If there is an error backoff exponentially until there is no longer an error
- for i in tqdm(range(1, backoff_time), desc="Backing off " + str(backoff_time) + " seconds",
- leave=False):
- time.sleep(1)
- backoff_time = min(backoff_time * 2, 60 * 60)
- if __name__ == '__main__':
- freeze_support()
- for group in teams:
- proc = Process(target=upload, args=(group,))
- proc.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement