def get_teamcode_to_id_dict(season): json_data = util.request_json(TEAMS_URL.format(season=season), 'teams') team_code_id_map = dict() for team in json_data['teams']: team_code_id_map[team['abbreviation'].lower()] = team['id'] # print(str(team_code_id_map)) return team_code_id_map
def _display_standings(standings_type, display_title, date_str, args_filter, rank_tag='divisionRank', header_tags=('league', 'division')): if date_str is None: season_str = time.strftime("%Y") url_date_str = '' else: season_str = datetime.strftime(datetime.strptime(date_str, "%Y-%m-%d"), "%Y") url_date_str = '&date=' + date_str url = STANDINGS_URL.format(standings_type=standings_type, league_ids=_get_league_ids(args_filter), season=season_str, date=url_date_str) json_data = util.request_json(url, 'standings') border = displayutil.Border(use_unicode=config.UNICODE) outl = list() if display_title != '': outl.append(_get_title_header(display_title, border)) needs_line_hr = False for record in json_data['records']: if args_filter and standings_type == 'byDivision' and args_filter in DIVISION_FILTERS: pass _get_standings_display_for_record(outl, standings_type, record, header_tags, rank_tag, border, needs_line_hr) print('\n'.join(outl))
def _get_roster(team_id, season): json_data = util.request_json( ROSTER_URL.format(teamId=team_id, season=season), 'roster-{}'.format(team_id)) roster = dict() for person in json_data['roster']: person_id = str(person['person']['id']) roster[person_id] = dict() roster[person_id]['fullName'] = person['person']['fullName'] roster[person_id]['link'] = person['person']['link'] roster[person_id]['jerseyNumber'] = person['jerseyNumber'] roster[person_id]['position'] = person['position']['abbreviation'] roster[person_id]['status'] = 'A' return roster
def _lookup_inning_timestamp_via_airings(game_rec, media_playback_id, inning, inning_half='top', overwrite_json=True): broadcast_start = None url = ( 'https://search-api-mlbtv.mlb.com/svc/search/v2/graphql/persisted/' 'query/core/Airings?variables={{%22partnerProgramIds%22%3A[%22{gamepk}%22]}}' ).format(gamepk=game_rec['game_pk']) json_data = util.request_json(url, 'airings') for airing in json_data['data']['Airings']: # there is a separate BROADCAST_START for each broadcast, so do lookup based on passed-in media id LOG.debug("airing['mediaId']: %s, media_playback_id: %s", str(airing['mediaId']), media_playback_id) if str(airing['mediaId']) != media_playback_id: continue if 'milestones' not in airing: LOG.warn("_lookup_inning_timestamp_via_airings: no milestone data for airing: %s", str(airing)) continue for milestone in airing['milestones']: if milestone['milestoneType'] == "BROADCAST_START": for milestone_time in milestone['milestoneTime']: if str(milestone_time['type']) == 'absolute': broadcast_start_str = str(milestone_time['startDatetime']) broadcast_start = parser.parse(broadcast_start_str).timestamp() elif milestone['milestoneType'] == "INNING_START": milestone_inning = '1' milestone_inning_half = 'top' for keyword in milestone['keywords']: if str(keyword['type']) == 'inning': milestone_inning = str(keyword['value']) elif str(keyword['type']) == 'top': if str(keyword['value']) != 'true': milestone_inning_half = 'bottom' if milestone_inning == inning and milestone_inning_half == inning_half: # we found it for milestone_time in milestone['milestoneTime']: if str(milestone_time['type']) == 'absolute': inning_start_timestamp_str = milestone_time['startDatetime'] # inning_start_timestamp_str = str(play['about']['startTime']) inning_start_timestamp = parser.parse(inning_start_timestamp_str).timestamp() LOG.info("Found inning start: %s", inning_start_timestamp_str) LOG.debug("Milestone data: %s", str(milestone)) return broadcast_start, inning_start_timestamp, inning_start_timestamp_str LOG.warn("Could not locate '%s %s' inning", inning_half, inning) return broadcast_start, None, None
def _get_games_by_date(self, date_str=None): if date_str is None: date_str = time.strftime("%Y-%m-%d") # https://statsapi.mlb.com/api/v1/schedule?sportId=1&startDate=2018-03-26&endDate=2018-03-26&hydrate=schedule.teams,schedule.linescore,schedule.game.content.media.epg # hydrate = 'hydrate=schedule.teams,schedule.linescore,schedule.game.content.media.epg' hydrate = 'hydrate=broadcasts(all),game(content(all),editorial(preview,recap)),linescore,team,probablePitcher(note)' # hydrate = 'hydrate=linescore,team,game(content(summary,media(epg),editorial(preview,recap),highlights(highlights(items))))' # "&hydrate=linescore,team,game(content(summary,media(epg),editorial(preview,recap),highlights(highlights(items))))" # hydrate = 'hydrate=linescore,team,game(content(summary,media(epg)),tickets)' url = '{0}/api/v1/schedule?sportId=1&startDate={1}&endDate={1}&{2}'.format(config.CONFIG.parser['api_url'], date_str, hydrate) json_data = util.request_json(url, 'gamedata') game_records = dict() # we return this dictionary if json_data['dates'] is None or len(json_data['dates']) < 1: LOG.debug("_get_games_by_date: no game data for %s", date_str) return None for game in json_data['dates'][0]['games']: # LOG.debug('game: {}'.format(game)) game_pk_str = str(game['gamePk']) game_records[game_pk_str] = dict() game_rec = game_records[game_pk_str] game_rec['game_pk'] = game_pk_str game_rec['abstractGameState'] = str(game['status']['abstractGameState']) # Preview, Live, Final game_rec['codedGameState'] = str(game['status']['codedGameState']) # is something like: F, O, C, I # is something like: Scheduled, Live, Final, In Progress, Critical, Postponed: game_rec['detailedState'] = str(game['status']['detailedState']) game_rec['doubleHeader'] = str(game['doubleHeader']) game_rec['gameNumber'] = str(game['gameNumber']) game_rec['mlbdate'] = parser.parse(str(game['gameDate'])) if 'gamesInSeries' in game: game_rec['gamesInSeries'] = str(game['gamesInSeries']) game_rec['seriesGameNumber'] = str(game['seriesGameNumber']) else: game_rec['gamesInSeries'] = "0" game_rec['seriesGameNumber'] = "0" game_rec['linescore'] = dict() if 'linescore' in game: game_rec['linescore']['raw'] = game['linescore'] if 'Delayed' in game_rec['detailedState']: game_rec['linescore']['currentInning'] = str(game_rec['detailedState']) game_rec['linescore']['currentInningOrdinal'] = 'Not Started' game_rec['linescore']['inningState'] = '' elif 'currentInning' in game['linescore']: game_rec['linescore']['currentInning'] = str(game['linescore']['currentInning']) else: game_rec['linescore']['currentInningOrdinal'] = '0' if 'currentInningOrdinal' in game['linescore']: game_rec['linescore']['currentInningOrdinal'] = str(game['linescore']['currentInningOrdinal']) if 'inningState' in game['linescore']: game_rec['linescore']['inningState'] = str(game['linescore']['inningState'])[:3] else: game_rec['linescore']['inningState'] = str(game['linescore']['inningHalf'])[:3] else: game_rec['linescore']['currentInningOrdinal'] = 'Not Started' game_rec['linescore']['inningState'] = '' else: game_rec['linescore']['currentInning'] = 'n/a' game_rec['linescore']['inningState'] = '' game_rec['linescore']['currentInningOrdinal'] = game_rec['detailedState'] for teamtype in ('home', 'away'): # pprint.pprint(game['teams']) game_rec[teamtype] = dict() # seems to be two different formats for away/home team info(!) if 'name' in game['teams'][teamtype]['team'] and 'abbrev' in game['teams'][teamtype]['team']['name']: game_rec[teamtype] = { 'abbrev': str(game['teams'][teamtype]['team']['name']['abbrev']).lower(), 'display': str(game['teams'][teamtype]['team']['name']['display']), 'brief': str(game['teams'][teamtype]['team']['name']['brief']), 'full': str(game['teams'][teamtype]['team']['name']['full']), 'league': str(game['teams'][teamtype]['league']), 'division': str(game['teams'][teamtype]['division']), } elif 'abbreviation' in game['teams'][teamtype]['team']: game_rec[teamtype] = { 'abbrev': str(game['teams'][teamtype]['team']['abbreviation']).lower(), 'display': str(game['teams'][teamtype]['team']['shortName']), 'brief': str(game['teams'][teamtype]['team']['teamName']), 'full': str(game['teams'][teamtype]['team']['name']), 'league': 'n/a', 'division': 'n/a', } else: LOG.error("Unexpected game['teams'] for teamtype=%s", teamtype) pprint.pprint(game['teams'][teamtype]) game_rec[teamtype] = { 'abbrev': 'n/a', 'display': 'n/a', 'brief': 'n/a', 'full': 'n/a', 'league': 'n/a', 'division': 'n/a', } if 'linescore' in game and teamtype in game['linescore']['teams'] and 'runs' in game['linescore']['teams'][teamtype]: game_rec['linescore'][teamtype] = { 'runs': str(game['linescore']['teams'][teamtype]['runs']), 'hits': str(game['linescore']['teams'][teamtype]['hits']), 'errors': str(game['linescore']['teams'][teamtype]['errors']), } else: game_rec['linescore'][teamtype] = {'runs': '0', 'hits': '0', 'errors': '0'} game_rec['favourite'] = gamedata.is_fav(game_rec) game_rec['preview'] = list() try: if 'probablePitcher' in game['teams']['away'] or 'probablePitcher' in game['teams']['home']: game_rec['preview'].append('Probable Pitchers') game_rec['preview'].append('-----------------') for teamtype in ('away', 'home'): if 'probablePitcher' in game['teams'][teamtype]: # if config.CONFIG.parser['info_display_articles'] and 'fullName' in game['teams'][teamtype]['probablePitcher']: if 'fullName' in game['teams'][teamtype]['probablePitcher']: pitcher_name = ' '.join(reversed(game['teams'][teamtype]['probablePitcher']['fullName'].split(','))).strip() if config.CONFIG.parser.getboolean('info_display_articles') and 'note' in game['teams'][teamtype]['probablePitcher']: note = game['teams'][teamtype]['probablePitcher']['note'] game_rec['preview'].append('{}: {}: {}'.format(game['teams'][teamtype]['team']['teamName'], pitcher_name, note)) else: game_rec['preview'].append('{}: {}'.format(game['teams'][teamtype]['team']['teamName'], pitcher_name)) if config.CONFIG.parser.getboolean('info_display_articles') and teamtype == 'away': game_rec['preview'].append('') except: game_rec['preview'] = None game_rec['summary'] = list() try: if 'headline' in game['content']['editorial']['recap']['mlb']: game_rec['summary'].append('SUMMARY: ' + game['content']['editorial']['recap']['mlb']['headline']) if 'subhead' in game['content']['editorial']['recap']['mlb']: game_rec['summary'].append(' ' + game['content']['editorial']['recap']['mlb']['subhead']) if config.CONFIG.parser.getboolean('info_display_articles', True): if len(game_rec['summary']) > 0: game_rec['summary'].append('') if 'seoTitle' in game['content']['editorial']['recap']['mlb']: # game_rec['summary'].append('TITLE: ' + game['content']['editorial']['recap']['mlb']['seoTitle']) game_rec['summary'].append(game['content']['editorial']['recap']['mlb']['seoTitle']) game_rec['summary'].append('-' * len(game['content']['editorial']['recap']['mlb']['seoTitle'])) if 'body' in game['content']['editorial']['recap']['mlb']: game_rec['summary'].append(game['content']['editorial']['recap']['mlb']['body']) except: game_rec['summary'] = None game_rec['feed'] = dict() if game_rec['abstractGameState'] == 'Preview': continue # epg if 'media' in game['content'] and 'epg' in game['content']['media']: for media in game['content']['media']['epg']: if media['title'] == 'MLBTV': for stream in media['items']: if stream['mediaFeedType'] != 'COMPOSITE' and stream['mediaFeedType'] != 'ISO': feedtype = str(stream['mediaFeedType']).lower() # home, away, national, french, ... game_rec['feed'][feedtype] = dict() if 'mediaId' in stream: game_rec['feed'][feedtype]['mediaPlaybackId'] = str(stream['mediaId']) game_rec['feed'][feedtype]['mediaState'] = str(stream['mediaState']) game_rec['feed'][feedtype]['eventId'] = str(stream['id']) game_rec['feed'][feedtype]['callLetters'] = str(stream['callLetters']) if 'epgAlternate' in game['content']['media']: for media in game['content']['media']['epgAlternate']: if media['title'] == 'Extended Highlights': feedtype = 'condensed' if len(media['items']) > 0: game_rec['feed'][feedtype] = dict() stream = media['items'][0] game_rec['feed'][feedtype]['mediaPlaybackId'] = str(stream['mediaPlaybackId']) for playback_item in stream['playbacks']: if playback_item['name'] == config.CONFIG.parser['playback_scenario']: game_rec['feed'][feedtype]['playback_url'] = playback_item['url'] elif media['title'] == 'Daily Recap': feedtype = 'recap' if len(media['items']) > 0: game_rec['feed'][feedtype] = dict() stream = media['items'][0] game_rec['feed'][feedtype]['mediaPlaybackId'] = str(stream['mediaPlaybackId']) for playback_item in stream['playbacks']: if playback_item['name'] == config.CONFIG.parser['playback_scenario']: game_rec['feed'][feedtype]['playback_url'] = playback_item['url'] # elif media['title'] == 'Audio': # for stream in media['items']: # feedtype = 'audio-' + str(stream['mediaFeedType']).lower() # home, away, national, french, ... # game_rec['feed'][feedtype] = dict() # game_rec['feed'][feedtype]['mediaPlaybackId'] = str(stream['mediaId']) # game_rec['feed'][feedtype]['eventId'] = str(stream['id']) # game_rec['feed'][feedtype]['callLetters'] = str(stream['callLetters']) return game_records
def get_boxscore(game_pk): url = '{0}/api/v1/game/{1}/boxscore'.format(config.CONFIG.parser['api_url'], game_pk) json_data = util.request_json(url, 'boxscore') return json_data
def _get_person_stats(person_ids, season): json_data = util.request_json( MULTI_PERSON_STATS_URL.format(personIds=person_ids, season=season), 'person-stats') return json_data