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 display_game_data(self, game_date, game_records, arg_filter): show_scores = config.CONFIG.parser.getboolean('scores') show_scores = config.CONFIG.parser.getboolean('scores') border = displayutil.Border(use_unicode=config.UNICODE) if game_records is None: # outl.append("No game data for {}".format(game_date)) LOG.info("No game data for {}".format(game_date)) # LOG.info("No game data to display") return outl = list() # holds list of strings for output print_outl = False # print header date_hdr = '{:7}{}'.format('', '{}'.format(game_date)) if show_scores: outl.append("{:64} {pipe} {:^5} {pipe} {:^9} {pipe} {}".format( date_hdr, 'Score', 'State', 'Feeds', pipe=border.pipe)) outl.append("{c_on}{}{pipe}{}{pipe}{}{pipe}{}{c_off}".format( border.thickdash * 65, border.thickdash * 7, border.thickdash * 11, border.thickdash * 14, pipe=border.junction, c_on=border.border_color, c_off=border.color_off)) else: outl.append("{:64} {pipe} {:^9} {pipe} {}".format( date_hdr, 'State', 'Feeds', pipe=border.pipe)) outl.append("{c_on}{}{pipe}{}{pipe}{}{c_off}".format( border.thickdash * 65, border.thickdash * 11, border.thickdash * 14, pipe=border.junction, c_on=border.border_color, c_off=border.color_off)) displayed_game_count = 0 for game_pk in game_records: if apply_filter(game_records[game_pk], arg_filter) is not None: displayed_game_count += 1 outl.extend( self._display_game_details(game_pk, game_records[game_pk], displayed_game_count)) print_outl = True if print_outl: print('\n'.join(outl))
def display_standings(standings_type, display_title, date_str, rank_tag='divisionRank', header_tags=('conference', 'division')): url = STANDINGS_URL.format(standings_type=standings_type, date=date_str) json_data = util.request_json(url, 'standings') border = displayutil.Border(use_unicode=config.UNICODE) outl = list() if display_title != '': outl.append( '{color_on}{name:22}\t{win:^2} {ot:^2} {loss:^2} {point:^3} {streak}{color_off}' .format(color_on=border.border_color, name=' {thickborder} {title} {thickborder}'.format( title=display_title, thickborder=border.doubledash * int((29 - len(display_title)) / 2 - 1)), win='W', ot='OT', loss='L', point='P', streak='Streak', color_off=ANSI.reset())) needs_line_hr = False for record in json_data['records']: if standings_type == record['standingsType']: if needs_line_hr > 0: pass # outl.append('-' * 10) header = '' for tag in header_tags: if tag in record: if 'name' in record[tag]: header = _add_to_header(header, record[tag]['name']) else: header = _add_to_header(header, record[tag]) if header: header = '{color_on}{b1} {title} {b2}{color_off}'.format( color_on=border.border_color, title=header, b1=border.dash * 3, b2=border.dash * (41 - len(header)), color_off=ANSI.reset()) outl.append(' {}'.format(header)) needs_line_hr = True else: LOG.error('Unexpected: standingsType=%s, not %s', record['standingsType'], standings_type) for teamrec in record['teamRecords']: clinch = '' if 'clinchIndicator' in teamrec: clinch = teamrec['clinchIndicator'] + '-' rank = '' if rank_tag in teamrec: rank = teamrec[rank_tag] color_on = '' color_off = '' if _is_fav(teamrec['team']['name']): if config.CONFIG.parser['fav_colour'] != '': color_on = ANSI.fg(config.CONFIG.parser['fav_colour']) color_off = ANSI.reset() outl.append( '{color_on}{rank:2} {clinch}{name:22}\t{win:2} {ot:2} {loss:2} {point:3} [{streak}]{color_off}' .format(color_on=color_on, rank=rank, clinch=clinch, name=teamrec['team']['name'], win=teamrec['leagueRecord']['wins'], ot=teamrec['leagueRecord']['ot'], loss=teamrec['leagueRecord']['losses'], point=teamrec['points'], streak=teamrec['streak']['streakCode'], color_off=color_off)) print('\n'.join(outl))
def _display_game_details(self, game_pk, game_rec, displayed_game_count): outl = list() border = displayutil.Border(use_unicode=config.UNICODE) color_on = '' color_off = '' if is_fav(game_rec): if config.CONFIG.parser['fav_colour'] != '': color_on = ANSI.fg(config.CONFIG.parser['fav_colour']) color_off = ANSI.reset() if game_rec['abstractGameState'] == 'Live': color_on += ANSI.control_code('bold') color_off = ANSI.reset() show_scores = config.CONFIG.parser.getboolean('scores') game_info_str = "{}: {} ({}) at {} ({})".format( util.convert_time_to_local(game_rec['nhldate']), game_rec['away_name'], game_rec['away_abbrev'].upper(), game_rec['home_name'], game_rec['home_abbrev'].upper()) game_state = '' game_state_color_on = color_on game_state_color_off = color_off if game_rec['abstractGameState'] not in ('Preview', ): if not show_scores: game_state = game_rec['abstractGameState'] if 'In Progress - ' in game_rec['detailedState']: game_state = game_rec['detailedState'].split( 'In Progress - ')[-1] elif game_rec['detailedState'] not in ('Live', 'Final', 'Scheduled', 'In Progress'): game_state = game_rec['detailedState'] else: if 'Critical' in game_rec['detailedState']: game_state_color_on = ANSI.fg( config.CONFIG.parser['game_critical_colour']) game_state_color_off = ANSI.reset() if game_rec['linescore']['currentPeriodTimeRemaining'] == 'Final' \ and game_rec['linescore']['currentPeriodOrdinal'] == '3rd': game_state = 'Final' else: game_state = '{} {}'.format( game_rec['linescore'] ['currentPeriodTimeRemaining'].title(), game_rec['linescore']['currentPeriodOrdinal']) # else: # game_state = 'Pending' if config.CONFIG.parser.getboolean('scores'): score = '' if game_rec['abstractGameState'] not in ('Preview', ): score = '{}-{}'.format(game_rec['away_score'], game_rec['home_score']) outl.append( "{c_on}{gameinfo:<64}{c_off} {pipe} {c_on}{score:^5}{c_off} {pipe} {gsc_on}{state:>9}{gsc_off} {pipe} {c_on}{feeds}{c_off}" .format(gameinfo=game_info_str, score=score, state=game_state, gsc_on=game_state_color_on, gsc_off=game_state_color_off, feeds=self.__get_feeds_for_display(game_rec), pipe=border.pipe, c_on=color_on, c_off=color_off)) else: outl.append( "{c_on}{gameinfo:<64}{c_off} {pipe} {c_on}{state:^9}{c_off} {pipe} {c_on}{feeds}{c_off}" .format(gameinfo=game_info_str, state=game_state, feeds=self.__get_feeds_for_display(game_rec), pipe=border.pipe, c_on=color_on, c_off=color_off)) if config.CONFIG.parser.getboolean( 'debug') and config.CONFIG.parser.getboolean('verbose'): for feedtype in game_rec['feed']: outl.append( ' {}: {} [game_pk:{}, mediaPlaybackId:{}]'.format( feedtype, game_rec['abstractGameState'], game_pk, game_rec['feed'][feedtype]['mediaPlaybackId'])) return outl
def _display_game_details(self, game_pk, game_rec, show_linescore, games_displayed_count): show_scores = config.CONFIG.parser.getboolean('scores') outl = list() border = displayutil.Border(use_unicode=config.UNICODE) color_on = '' color_off = '' odd_even = games_displayed_count % 2 # if odd_even: # color_on = ANSI.fg('yellow') # else: # color_on = display'reset'.ANSI.fg('lightblue') color_off = ANSI.reset() if is_fav(game_rec): if config.CONFIG.parser['fav_colour'] != '': color_on = ANSI.fg(config.CONFIG.parser['fav_colour']) color_off = ANSI.reset() if game_rec['abstractGameState'] == 'Live': color_on += ANSI.control_code('bold') color_off = ANSI.reset() if game_rec['doubleHeader'] == 'N': series_info = "{sgn}/{gis}".format( sgn=game_rec['seriesGameNumber'], gis=game_rec['gamesInSeries']) else: # series_info = "DH{gn} {sgn}/{gis}".format(sgn=game_rec['seriesGameNumber'], # gis=game_rec['gamesInSeries'], # gn=game_rec['gameNumber']) series_info = "DH-{gn}".format(gn=game_rec['gameNumber']) game_info_str = "{time}: {a1} ({a2}) at {h1} ({h2})"\ .format(time=util.convert_time_to_local(game_rec['mlbdate']), a1=game_rec['away']['display'], a2=game_rec['away']['abbrev'].upper(), h1=game_rec['home']['display'], h2=game_rec['home']['abbrev'].upper()) game_state = '' game_state_color_on = color_on game_state_color_off = color_off if game_rec['abstractGameState'] in ('Preview', ): if game_rec['detailedState'] != 'Scheduled': if 'Delayed' in game_rec['detailedState']: game_state = 'Delayed' else: game_state = game_rec['detailedState'] else: if show_scores: if game_rec['detailedState'] in ('Critical', ): game_state_color_on = ANSI.fg( config.CONFIG.parser['game_critical_colour']) game_state_color_off = ANSI.reset() if game_rec['detailedState'] in ('Final', ): game_state = game_rec['detailedState'] if 'currentInning' in game_rec['linescore'] and int( game_rec['linescore']['currentInning']) != 9: game_state += '({})'.format( game_rec['linescore']['currentInning']) else: if game_rec['linescore']['inningState'] != '': game_state = '{} {}'.format( game_rec['linescore']['inningState'].title(), game_rec['linescore']['currentInningOrdinal']) else: game_state = game_rec['linescore'][ 'currentInningOrdinal'] else: game_state = game_rec['abstractGameState'] if 'In Progress - ' in game_rec['detailedState']: game_state = game_rec['detailedState'].split( 'In Progress - ')[-1] elif game_rec['detailedState'] not in ('Live', 'Final', 'Scheduled', 'In Progress'): game_state = game_rec['detailedState'] if show_scores: score = '' if game_rec['abstractGameState'] not in ( 'Preview', ) and game_rec['detailedState'] not in ( 'Postponed', ): score = '{}-{}'.format(game_rec['linescore']['away']['runs'], game_rec['linescore']['home']['runs']) # linescore if show_linescore: if games_displayed_count > 1: outl.append('{coloron}{dash}{coloroff}'.format( coloron=ANSI.fg('darkgrey'), dash=border.dash * 91, coloroff=ANSI.reset())) linescore_dict = self._format_linescore(game_rec) """ 18:05: LA Dodgers (LAD) at San Francisco (SF) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 R H E 1/2 Final(14): 5-7 LAD 0 0 1 0 0 2 1 0 0 0 0 0 0 1 5 12 0 Feeds: a,h / cnd,rcp SF 1 0 0 2 0 1 0 0 0 0 0 0 0 3 7 17 1 """ line_fmt = "{coloron}{ginfo:<50} {lscore}{coloroff}" # if game_rec['abstractGameState'] not in ('Live', 'Final'): # or game_rec['detailedState'] in ('Postponed', ): # outl.append(line_fmt.format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, lscore='')) # return outl outl.append( line_fmt.format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, lscore=linescore_dict['header'], pipe=border.pipe)) if game_state == '': # game_info_str = '{series:7}Not Started'.format(series=series_info) game_info_str = '{series:7}'.format(series=series_info) else: if game_rec['abstractGameState'] in ('Live',) \ and game_rec['linescore']['inningState'] != 'Mid' and 'raw' in game_rec['linescore']: outs_info = ', {} out'.format( game_rec['linescore']['raw']['outs']) else: outs_info = '' if score: game_info_str = '{series:7}{gstate}: {score}{outs}'.format( series=series_info, gstate=game_state, score=score, outs=outs_info) else: game_info_str = '{series:7}{gstate}'.format( series=series_info, gstate=game_state) outl.append( line_fmt.format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, lscore=linescore_dict['away'])) feeds = self.__get_feeds_for_display(game_rec) if feeds: game_info_str = '{:7}Feeds: {feeds}'.format( '', feeds=self.__get_feeds_for_display(game_rec)) outl.append( line_fmt.format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, lscore=linescore_dict['home'])) else: outl.append( line_fmt.format(coloron=color_on, coloroff=color_off, ginfo='', lscore=linescore_dict['home'])) else: # single-line game score outl.append(( "{coloron}{ginfo:<48} {series:^7}{coloroff} " "{pipe} {coloron}{score:^5}{coloroff} {pipe} " "{gscoloron}{gstate:<9}{gscoloroff} {pipe} {coloron}{feeds}{coloroff}" ).format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, series=series_info, score=score, gscoloron=game_state_color_on, gstate=game_state, gscoloroff=game_state_color_off, feeds=self.__get_feeds_for_display(game_rec), pipe=border.pipe)) else: # no scores outl.append(( "{coloron}{ginfo:<48} {series:^7}{coloroff} {pipe} " "{coloron}{gstate:^9}{coloroff} {pipe} {coloron}{feeds}{coloroff}" ).format(coloron=color_on, coloroff=color_off, ginfo=game_info_str, series=series_info, gstate=game_state, feeds=self.__get_feeds_for_display(game_rec), pipe=border.pipe)) return outl
def display_game_data(self, game_date, game_records, filter): show_scores = config.CONFIG.parser.getboolean('scores') show_linescore = config.CONFIG.parser.getboolean('linescore') border = displayutil.Border(use_unicode=config.UNICODE) if game_records is None: # outl.append("No game data for {}".format(game_date)) LOG.info("No game data for {}".format(game_date)) # LOG.info("No game data to display") return outl = list() # holds list of strings for output print_outl = False # print header date_hdr = '{:7}{} {}'.format( '', game_date, datetime.strftime(datetime.strptime(game_date, "%Y-%m-%d"), "%a")) if show_scores: if show_linescore: outl.append("{:56}".format(date_hdr)) outl.append('{c_on}{dash}{c_off}'.format( c_on=border.border_color, dash=border.thickdash * 91, c_off=border.color_off)) else: outl.append( "{:48} {:^7} {pipe} {:^5} {pipe} {:^9} {pipe} {}".format( date_hdr, 'Series', 'Score', 'State', 'Feeds', pipe=border.pipe)) outl.append("{c_on}{}{pipe}{}{pipe}{}{pipe}{}{c_off}".format( border.thickdash * 57, border.thickdash * 7, border.thickdash * 11, border.thickdash * 16, pipe=border.junction, c_on=border.border_color, c_off=border.color_off)) else: outl.append("{:48} {:^7} {pipe} {:^9} {pipe} {}".format( date_hdr, 'Series', 'State', 'Feeds', pipe=border.pipe)) outl.append("{c_on}{}{pipe}{}{pipe}{}{c_off}".format( border.thickdash * 57, border.thickdash * 11, border.thickdash * 16, pipe=border.junction, c_on=border.border_color, c_off=border.color_off)) games_displayed_count = 0 for game_pk in game_records: if apply_filter(game_records[game_pk], filter) is not None: games_displayed_count += 1 outl.extend( self._display_game_details(game_pk, game_records[game_pk], show_linescore, games_displayed_count)) print_outl = True if print_outl: print('\n'.join(outl))