from ohmysportsfeedspy import MySportsFeeds ''' Data_query = MySportsFeeds('1.2',verbose=True) Data_query.authenticate('YOUR_API_KEY', 'YOUR_ACCOUNT_PASSWORD') Output = Data_query.msf_get_data(league='nba',season='2016-2017-regular',feed='player_gamelogs',format='json',player='stephen-curry',force='true') print(Output) ''' Data_query = MySportsFeeds(version='2.0',verbose=True) Data_query.authenticate('3532c47b-606d-4b5a-b0a9-a736c2', 'MYSPORTSFEEDS') ''' Output = Data_query.msf_get_data(league='nba',season='2016-2017-regular',feed='seasonal_player_gamelogs',format='json',player='stephen-curry') ''' # Output = Data_query.msf_get_data(league='nba',season='2017-2018-regular',feed='seasonal_games',format='json') # Output = Data_query.msf_get_data(league='nba',season='2018-2019-regular',feed='game_boxscore',date='20181115',game='20181115-ATL-DEN',format='json') # Output = Data_query.msf_get_data(league='nba',season='2017-2018-regular',feed='seasonal_team_gamelogs',team='OKL',format='json') Output = Data_query.msf_get_data(league='nba',season='2017-2018-regular',feed='seasonal_team_stats',format='json') print(Output)
from ohmysportsfeedspy import MySportsFeeds import json import pprint Data_query = MySportsFeeds('1.2', verbose=True) Data_query.authenticate('7af68037-2f3f-4bf5-bbd5-173ad2', 'Dk30RQHT') Output = Data_query.msf_get_data(league='nfl', season='upcoming', feed='player_injuries', format='json') print(json.dumps(Output, indent=4))
msf.authenticate(f.decrypt(token), "MYSPORTSFEEDS") #output = msf.msf_get_data(league='mlb',season='2016-playoff',feed='seasonal_games',format='json') #print output league = 'nfl' beg_yr = 2014 end_yr = 2019 rec_plyff = False valid_feeds = [ 'seasonal_games', 'daily_games', 'weekly_games', 'seasonal_dfs', 'daily_dfs', 'weekly_dfs', 'seasonal_player_gamelogs', 'daily_player_gamelogs', 'weekly_player_gamelogs', 'seasonal_team_gamelogs', 'daily_team_gamelogs', 'weekly_team_gamelogs', 'game_boxscore', 'game_playbyplay', 'game_lineup', 'current_season', 'player_injuries', 'latest_updates', 'seasonal_team_stats', 'seasonal_player_stats', 'seasonal_venues', 'players', 'seasonal_standings' ] valid_types = ['regular', 'playoff'] for feed in valid_feeds: for type in valid_types: for year in range(beg_yr, end_yr + 1): if not (year == beg_yr and type == 'playoff') and not ( year == end_yr and type == 'playoff' and not rec_plyff): try: msf.msf_get_data(league=league, season=str(year) + '-' + type, feed=feed, format='json') except: print 'failure on ' + feed + ' ' + str(year) + ' ' + type
msf = MySportsFeeds(version='2.0') msf.authenticate("API KEY", "MYSPORTSFEEDS") # Pull JSON from My Sports Feeds API year = 2019 days = [ datetime.datetime(year, 6, 8) + datetime.timedelta(days=i) for i in range(1) ] days = [day for day in days] game_date = [day.strftime("%Y%m%d") for day in days] for date in game_date: json_gamelog = msf.msf_get_data(league='mlb', season='2019-regular', feed='daily_player_gamelogs', format='json', date=str(date)) # Read and format JSON # game_date_csv = 20170402 # while game_date_csv <= 20171001: # with open('mlb/%d/json/daily_player_gamelogs-mlb-%d-regular-%d.json' % (year, game_date)) as json_gamelog: # loaded_json = json.load(json_gamelog) # player_game_stats = json_normalize(loaded_json['gamelogs']) # player_game_stats.fillna(0, inplace=True) # player_game_stats.columns = [ 'gameAwayTeam', # 'gameHomeTeam', # 'gameID', # 'gameStartTime',
from ohmysportsfeedspy import MySportsFeeds Data_query = MySportsFeeds('1.0',verbose=True) Data_query.authenticate('poly91', 'abel1234') Output = Data_query.msf_get_data(league='nba',season='2016-2017-regular',feed='daily_player_stats',fordate='20161202',format='json',sort='player.team') #Output = Data_query.msf_get_data(league='nba',season='2016-2017-regular',feed='full_game_schedule',format='csv') #Output = Data_query.msf_get_data(league='nba',season='2016-2017-regular',feed='game_boxscore',format='csv') # 20161025 print(Output) ''' Get the dates from the full_game_schedule and iterate through the dates with the daily_player_stats with sort = player.team Sort the data based on teams, and make different CSV files. '''
from ohmysportsfeedspy import MySportsFeeds Data_query = MySportsFeeds('2.0', verbose=True) Data_query.authenticate('7a623d32-510c-4ccf-8a7b-8a3af9', 'MYSPORTSFEEDS') Output = Data_query.msf_get_data(league='nba', season='2018-2019-regular', feed='seasonal_player_gamelogs', player='karlanthony-towns', format='json', force='true') print(Output)
from ohmysportsfeedspy import MySportsFeeds import json import pprint Data_query = MySportsFeeds('1.2', verbose=True) Data_query.authenticate('7af68037-2f3f-4bf5-bbd5-173ad2', 'Dk30RQHT') Output = Data_query.msf_get_data(league='nfl', season='upcoming', feed='active_players', team=['HOU'], format='json') for item in Output['activeplayers']['playerentry']: first_name = item["player"]["FirstName"] last_name = item["player"]["LastName"] if "BirthCity" in item["player"].keys(): hometown = item["player"]["BirthCity"] comma = ',' print(first_name + last_name + comma + hometown, "USA") else: print(first_name + last_name)
from ohmysportsfeedspy import MySportsFeeds import os msf = MySportsFeeds(version="2.0") msf.authenticate(os.getenv('MySportsFeedsAPI')), 'MYSPORTSFEEDS') schedule = msf.msf_get_data(league='nba',season='2016-2017-regular',feed='seasonal_games',format='json', force=True) games = schedule['games'] refs = schedule['references'] teams = refs['teamReferences'] game0 = games[0] plays0 = msf.msf_get_data(league='nba',season='2016-2017-regular',game='20161025-NYK-CLE',feed='game_playbyplay',format='json', force=True) plays_refs = plays0['references'] players = plays_refs['playerReferences'] play_by_play = plays0[]
from ohmysportsfeedspy import MySportsFeeds import csv import json # create a my sports feed object with api version msf = MySportsFeeds(version="1.2") # authenticate msf.authenticate("967deecf-93c4-4410-a443-4118c9", "Temp123!") # get 2016-2017 stats for steph curry output = msf.msf_get_data(league='nba',season='2018-2019-regular',feed='cumulative_player_stats',format='csv',player='stephen-curry') # print out the data print(str(output)) # open a file to place the data in the csv file with write method csv1= open("/Users/javarharris/Desktop/stephcurry.csv","w") # use the csv object to write he output data to the csv file writer = csv.writer(csv1, delimiter = ",") #Use a for loop to write the data in the file for line in output: writer.writerow((line)) #close the file so the data is saved csv1.close()
SAC SAS TOR UTA WAS ''' teams = teams_all.splitlines() # set up data query for mysports feed Data_query = MySportsFeeds(version='2.0', verbose=True) Data_query.authenticate('3532c47b-606d-4b5a-b0a9-a736c2', 'MYSPORTSFEEDS') for team_name in teams: print(team_name) Output = Data_query.msf_get_data(league='nba', season='2017-2018-regular', feed='seasonal_team_gamelogs', team=team_name, format='json') # process the json file dir = 'results' file_name = 'seasonal_team_gamelogs-nba-2017-2018-regular.json' with open("{}/{}".format(dir, file_name)) as f: d = json.load(f) df = pd.DataFrame.from_dict(json_normalize(d['gamelogs']), orient='columns') output_dir = 'team_game_logs' output_name = 'team_game_log' output_type = '.xlsx' df.to_excel("{}/{}_{}{}".format(output_dir, output_name, team_name, output_type),
from ohmysportsfeedspy import MySportsFeeds Data_query = MySportsFeeds(version='1.0', verbose=True) Data_query.authenticate('e68cbd5b-6a91-4e66-960a-e2cda3', 'CS5200data') Output = Data_query.msf_get_data(league='nfl', season='2017-regular', feed='player_gamelogs', format='csv', team='houston-texans', force='true') print(Output)
from ohmysportsfeedspy import MySportsFeeds from pymongo import MongoClient from bson import json_util msf = MySportsFeeds(version="1.0") msf.authenticate("Jaggia", "pxt-7xc-PtG-sSk") response = msf.msf_get_data(league='nba', season='2016-2017-regular', feed='cumulative_player_stats', format='json', team='chi') client = MongoClient() db = client.sportsDB posts = db.posts post_id = posts.insert_one(response).inserted_id print post_id # print(response)
class MsfImportMlb: def __init__(self): self.seasons = ['2019-regular', '2017-regular', '2017-playoff', '2018-regular', '2018-playoff'] #self.seasons = ['2019-regular'] apikey = '98de7b49-a696-4ed7-8efa-94b28a' self.msf = MySportsFeeds(version="2.0") self.msf.authenticate(apikey, "MYSPORTSFEEDS") self.bqu = BigqueryUtils() def get_seasonal_stats(self): start_time=dt.now() for feed in ['seasonal_games', 'seasonal_team_stats', 'seasonal_player_stats']: outfile_json = 'results/MLB/msf-mlb-{}-{}.json'.format(feed, dt.now().strftime('%Y%m%dT%H%M%S')) with open(outfile_json, 'w') as jsonfile: for season in self.seasons: params = { 'league': 'mlb', 'season': season, 'feed': feed, 'format': 'json', } print('Starting msf {}-{}, delta-time: {}'.format(season, feed, dt.now()-start_time)) seasondata = self.msf.msf_get_data(**params) outjson = json.dumps({'Season': season, 'Seasondata': seasondata}) jsonfile.write(outjson) jsonfile.write('\n') #delete_query = 'delete from `Baseball1.{}` where season="{}"'.format(feed, season) #self.bqu.execute_query(delete_query) jsonfile.close() print('Starting upload of file {}, delta-time: {}'.format(outfile_json, dt.now() - start_time)) uri = self.bqu.upload_file_to_gcp('sport-uploads', outfile_json, outfile_json) print('Starting table creation, delta-time: {}'.format(dt.now() - start_time)) ret = self.bqu.create_table_from_gcp_file(uri, 'Baseball1', feed, 'WRITE_TRUNCATE') def get_game_days_stats(self): start_time=dt.now() for statObject in ['team', 'player']: feed = 'daily_{}_gamelogs'.format(statObject) # # get the missing game-days query = 'SELECT season,gameDay FROM `Baseball1.missing_{}_gamelogs` group by 1,2'.format(statObject) games_df = self.bqu.execute_query_to_df(query) print(games_df.shape) if (games_df.shape[0] == 0): return # # loop over missing game days for i,game in games_df.iterrows(): # # open the main file. mainfile_name = 'results/MLB/msf-mlb-dayfeeds-{}-{}.json'.format(feed, dt.now().strftime('%Y%m%d')) mainfile = open(mainfile_name, 'w') params = { 'league': 'mlb', 'date': game['gameDay'], 'season': game['season'], 'feed': feed, 'format': 'json', } outfile_json = 'results/MLB/dayfeeds/msf-mlb-{feed}-{season}-{date}.json'.format(**params) if (not os.path.exists(outfile_json) or True): # and (os.path.getsize(outfile_json)>0): print('Getting msf #{}, {}, delta-time: {}'.format(i, outfile_json, dt.now()-start_time)) jsonfile = open(outfile_json, 'w') # # Getting the data from MySportsFeeds try: seasondata = self.msf.msf_get_data(**params) except Exception as e: print('msf_get_data returned with error {}'.format(e)) continue except Warning as w: print('msf_get_data returned with warning {}'.format(w)) continue jsonfile.write(json.dumps(seasondata)) jsonfile.close() else: print('Reading msf #{}, {}, delta-time: {}'.format(i, outfile_json, dt.now()-start_time)) # # loading the JSON from already existing file. try: jsonfile = open(outfile_json,'r') seasondata = json.load(jsonfile) except Exception as e: print('Error loading JSON from file {}'.format(e)) continue dayfeed = { 'gamelogs': seasondata['gamelogs'], 'lastUpdatedOn': seasondata['lastUpdatedOn'], 'season': params['season'] } mainfile.write(json.dumps(dayfeed)+'\n') mainfile.close() # # upload file and update table. try: print('Starting upload of file {}, delta-time: {}'.format(outfile_json, dt.now() - start_time)) uri = self.bqu.upload_file_to_gcp('sport-uploads', mainfile_name, outfile_json + dt.now().strftime('.%Y%m%dT%H%M%S')) print('Starting table creation from {}, delta-time: {}'.format(uri, dt.now() - start_time)) ret = self.bqu.create_table_from_gcp_file(uri, 'Baseball1', '{}_{}'.format(feed, game['gameDay']), 'WRITE_TRUNCATE') except Exception as e: print('Error while uploading table {}'.format(e)) continue def get_game_pbp(self): start_time = dt.now() query='SELECT * FROM `sportsight-tests.Baseball1.missing_pbp_bydate`' datesDF = self.bqu.execute_query_to_df(query) print(datesDF.shape) if (datesDF.shape[0]==0): return feed = 'game_playbyplay' pbpFilePattern = '/Users/ysherman/Documents/GitHub/results/MLB/pbp/msf-pbp-{}-{}.json' for i,dayGames in datesDF.iterrows(): dayGames = dict(dayGames) games = dayGames['games'] date = dayGames['date'].strftime('%Y-%m-%d') outfile_json='results/MLB/temp/{}-{}.json'.format(feed, date) jsonfile = open(outfile_json, 'w') for game in games: pbpFileName = pbpFilePattern.format(game['id'], date) print(pbpFileName) if (not os.path.exists(pbpFileName) or True): params = {} params['season'] = dayGames['season'] params['matchname'] = game['matchname'] params['game'] = game['id'] params['format'] = 'json' params['league'] = 'mlb' params['feed']=feed while True: try: print('Getting for day {}, game-id: {}, {}, season: {}, feed: {}, delta-time: {}'.format( i, game['id'], game['matchname'], dayGames['season'], feed, dt.now() - start_time)) seasondata = self.msf.msf_get_data(**params) break except Exception as e: print("Error: {}".format(e)) except Warning as w: print("Error - Warning: {}".format(w)) except: print("Unknow Error") time.sleep(10) outfile = open(pbpFileName, 'w') outfile.write(json.dumps(seasondata)) outfile.close() else: try: print('Reading file {}'.format(pbpFileName)) pbpfile = open(pbpFileName,'r') seasondata = json.load(pbpfile) pbpfile.close() except Exception as e: print('Error loading JSON from file {}'.format(e)) continue seasondata = self.pbp_to_bigqery_form(seasondata) seasondata['season'] = dayGames['season'] seasondata['gameid'] = game['id'] seasondata['gamename'] = game['matchname'] outjson = json.dumps(seasondata) jsonfile.write(outjson) jsonfile.write('\n') # # uploading file for the day jsonfile.close() try: print('Starting upload of file {}, delta-time: {}'.format(outfile_json, dt.now() - start_time)) uri = self.bqu.upload_file_to_gcp('sport-uploads', outfile_json, outfile_json) print('Starting table creation, delta-time: {}'.format(dt.now() - start_time)) ret = self.bqu.create_table_from_gcp_file(uri, 'Baseball1', '{}_{}'.format(feed, date.replace('-','')), 'WRITE_TRUNCATE') except Exception as e: print('Error while uploading table {}'.format(e)) continue def pbp_atbatsubplay_new(self, atBatSubPlay): newAtBatSubPlay = [] for key,value in atBatSubPlay.items(): if value is None: continue #newAtBatSubPlay.append({'key': key,'value': '', 'type': 'NULL'}) elif key in ['retrievedAtLocation', 'pitchedLocation']: newAtBatSubPlay.append({'key': key+'-x','value': str(value['x']), 'type': 'INTEGER'}) newAtBatSubPlay.append({'key': key+'-y','value': str(value['y']), 'type': 'INTEGER'}) elif type(value)==dict: newAtBatSubPlay.append({'key': key,'value': str(value['id']), 'type': 'PlayerId'}) else: newAtBatSubPlay.append({'key': key,'value': str(value), 'type': type(value).__name__}) return newAtBatSubPlay def pbp_atbatplaystatus_new(self, atBatPlayStatus): playerRoles = ['batter', 'catcher', 'centerFielder', 'firstBaseman', 'firstBaseRunner', 'leftFielder', 'outFielder', 'pitcher', 'rightFielder', 'secondBaseman', 'secondBaseRunner', 'shortStop', 'thirdBaseman', 'thirdBaseRunner'] runnerRoles = ['firstBaseRunner', 'secondBaseRunner', 'thirdBaseRunner'] nRunners = 0 newAtBatPlayStatus = {} for key,value in atBatPlayStatus.items(): if key in runnerRoles: if value is not None: nRunners+=1 if value is None: value={'id': -1} if key in playerRoles: newAtBatPlayStatus[key] = value['id'] else: newAtBatPlayStatus[key] = value newAtBatPlayStatus['numRunners'] = nRunners return newAtBatPlayStatus def pbp_to_bigqery_form(self, pbpDict): newAtBats = [] atBatCounter=0 playCounter=0 for atBat in pbpDict['atBats']: newAtBatPlays = [] atBatPlayCounter = 0 for atBatPlay in atBat['atBatPlay']: newAtBatPlay={} try: if type(atBatPlay)!=dict: continue for key, value in atBatPlay.items(): if key=='description': newAtBatPlay[key]=value elif key == 'playStatus': newAtBatPlay[key] = self.pbp_atbatplaystatus_new(value) else: newAtBatPlay['atBatSubPlay'] = {'name': key, 'properties': self.pbp_atbatsubplay_new(value)} except Exception as e: print('Error {} with atBatCounter = {}, atBatPlayCounter={}, key={}, atbatsubplay={}, atBatPlay={}'.format(e,atBatCounter,atBatPlayCounter,key,value, atBatPlay)) atBatPlayCounter+=1 playCounter+=1 newAtBatPlay['index']=atBatPlayCounter newAtBatPlay['playindex']=playCounter newAtBatPlays.append(newAtBatPlay) atBatCounter += 1 atBat['index'] = atBatCounter atBat['atBatPlay'] = newAtBatPlays newAtBats.append(atBat) pbpDict['atBats'] = newAtBats return pbpDict
class LiveSportsClient(object): """ Initilize by passing in api_key or setting it with the env var LS_API_KEY """ EASTERN_TIMEZONE = timezone('America/New_York') def __init__(self, api_key=None): if api_key is None: print("Api key is null, getting it from env var LS_API_KEY") self.api_key = os.environ.get('LS_API_KEY') if self.api_key is None: raise ValueError( "Api key not passed in and not available under env var LS_API_KEY" ) self.mysportsfeeds_client = MySportsFeeds('2.0', verbose=True) self.mysportsfeeds_client.authenticate(api_key, 'MYSPORTSFEEDS') self.scoreboards = [] self.games_today = [] def get_todays_nba_games(self, team_abbrev=None): todays_date = datetime.now( LiveSportsClient.EASTERN_TIMEZONE).strftime('%Y%m%d') return self.get_daily_games_data_for_date(todays_date, team_abbrev) def get_daily_games_data_for_date(self, the_date, team_abbrev=None): if team_abbrev: #Philly = PHI return self.mysportsfeeds_client.msf_get_data(league='nba', season='latest', feed='daily_games', date=the_date, team=team_abbrev, format='json', force='true') else: return self.mysportsfeeds_client.msf_get_data(league='nba', season='latest', feed='daily_games', date=the_date, format='json', force='true') def set_todays_scoreboard(self, team_abbrev=None): scoreboards = [] todays_games_data = self.get_todays_nba_games(team_abbrev) for game in todays_games_data['games']: scoreboards.append( Scoreboard(game['schedule']['homeTeam']['abbreviation'], game['schedule']['awayTeam']['abbreviation'], game['schedule']['playedStatus'], game['schedule']['startTime'], game['score']['awayScoreTotal'], game['score']['homeScoreTotal'])) self.scoreboards = [] self.scoreboards = scoreboards def set_todays_nba_games_list(self): self.games_today = [] todays_games = self.get_todays_nba_games() for game in todays_games['games']: self.games_today.append( game['schedule']['awayTeam']['abbreviation']) def get_scoreboard_for_todays_game(self, team_abbrev): todays_game_data = self.get_todays_nba_games(team_abbrev) return Scoreboard( todays_game_data['games'][0]['schedule']['awayTeam'] ['abbreviation'], todays_game_data['games'][0]['schedule'] ['homeTeam']['abbreviation'], todays_game_data['games'][0]['schedule']['playedStatus'], todays_game_data['games'][0]['schedule']['startTime'], todays_game_data['games'][0]['score']['awayScoreTotal'], todays_game_data['games'][0]['score']['homeScoreTotal'])
from ohmysportsfeedspy import MySportsFeeds Data_query = MySportsFeeds('1.2', verbose=True) Data_query.authenticate('YOUR_API_KEY', 'YOUR_ACCOUNT_PASSWORD') Output = Data_query.msf_get_data(league='nba', season='2016-2017-regular', feed='player_gamelogs', format='json', player='stephen-curry') print(Output)
:return: ''' delta = enddate - startdate # as timedelta days = [] for i in range(delta.days + 1): day = startdate + timedelta(days=i) days.append((day.year, day.month, day.day)) return days msf = MySportsFeeds(version="2.0") msf.authenticate(os.getenv('MySportsFeedsAPI'), 'MYSPORTSFEEDS') schedule = msf.msf_get_data(league='nba', season='2016-2017-regular', feed='seasonal_games', format='json', force=True) games = schedule['games'] refs = schedule['references'] teams = refs['teamReferences'] firstgame = games[0] firstday = firstgame['schedule']['startTime'][:10] y = firstday[:4] m = firstday[5:7] d = firstday[8:10] firstdaystring = '{}{}{}'.format(firstday[:4], firstday[5:7], firstday[8:10]) lastgame = games[-1] lastday = lastgame['schedule']['startTime'][:10]
import base64 import requests import json from ohmysportsfeedspy import MySportsFeeds import random msf = MySportsFeeds(version="1.2") msf.authenticate("5d25b9f7-fe4d-48fe-b492-20d97d", "sball12345") output = msf.msf_get_data(league='nba', season='2020-2021-regular', feed='active_players', format='json', force="false") def get_random_player_data(): players = output['activeplayers']['playerentry'] player_pic = None draft_status = None list_size = len(players) while player_pic == None or draft_status == None or roster_status == None: random_player_id = random.randint(1, list_size) player = players[random_player_id]['player'] player_pic = player['officialImageSrc'] roster_status = player['RosterStatus'] draft_status = player["draft"] playerName = player["FirstName"] + " " + player["LastName"] playerLastName = player["LastName"]