def player_stats(name): # Read the page source that was previously written by webgrab(). If webgrab_switch is toggled # off and the page source doesn't exist, the program will exist and print an error message try: f1 = open("C:\\Python27\\Scripts\\baseball\\Fangraphs\\player_info\\" + name + "_output.txt") except IOError: print "Error: The file C:\\Python27\\Scripts\\baseball\\Fangraphs\\player_info\\%s_output.txt does not exist." %name sys.exit() pageSource = f1.read() f1.close() table = get_tables(pageSource) ftemp = open("C:\\Python27\\Scripts\\baseball\\Fangraphs\\temp_player_info\\" + name + "_table_temp.txt", 'w') for i in table: ftemp.write(str(i)) # See if they are in the middle of the game # (Contents of Table 18 change if there is a game ongoing) live_test = makelist(table[18]) if str(live_test[0][0]).find("Today") >= 0: print "live game in progress\n" player_list = makelist(table[24]) # This table stores basic stats else: print "No live game in progress\n" player_list = makelist(table[23]) # This table stores basic stats return [player_list]
def send_to_sql(): global season db = "../sql/nba_stats.db" temp = dbConnect(db) tables = get_tables(db) with temp: for stats in ['somestats', 'allstats']: tablename = ['nba', 'opponent', stats, '20' + season[-2:]] tablename = ('_').join(tablename) if tablename in list(tables): print "Table already exists" continue else: if stats == 'somestats': df = team_merge(season = '2014-15', playoff = 'Regular+Season', somestats = True) else: df = team_merge(season = '2014-15', playoff = 'Regular+Season', somestats = False) df.to_sql(tablename, temp.con, flavor = 'sqlite') print print 'From stats.nba.com' print 'Finished team stats table for %s' % ('20' + season[-2:]) print tablename print
def month_sql(): """ Sends the dataframe from allteam_months to the SQLite database """ db = "../sql/nba_stats.db" temp = dbConnect(db) tables = get_tables(db) with temp: tablename = ['nba', 'opponent', 'month', '2015'] tablename = ('_').join(tablename) if tablename in list(tables): print "Table already exists" else: df = allteam_months() df.to_sql(tablename, temp.con, flavor = 'sqlite') print print 'From stats.nba.com' print 'Finished team month stats table for %s' % ('2015') print tablename print