From 644108135825461de07fef10e8d9fc968065d310 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Wed, 1 May 2019 08:41:35 -0500 Subject: [PATCH] NHL: Fix playoff series wins (#740) These would show as 0 when a series was won. It now properly shows 4. --- i3pystatus/scores/__init__.py | 2 +- i3pystatus/scores/mlb.py | 25 +++---- i3pystatus/scores/nhl.py | 127 +++++++++++++++++++++------------- 3 files changed, 88 insertions(+), 66 deletions(-) diff --git a/i3pystatus/scores/__init__.py b/i3pystatus/scores/__init__.py index 5067911..1af0c61 100644 --- a/i3pystatus/scores/__init__.py +++ b/i3pystatus/scores/__init__.py @@ -125,7 +125,7 @@ class ScoresBackend(SettingsBase): except (TypeError, ValueError): return 0 - def get_nested(self, data, expr, callback=None, default=None): + def get_nested(self, data, expr, callback=None, default=''): if callback is None: def callback(x): return x diff --git a/i3pystatus/scores/mlb.py b/i3pystatus/scores/mlb.py index dea0102..f1ce2e6 100644 --- a/i3pystatus/scores/mlb.py +++ b/i3pystatus/scores/mlb.py @@ -212,12 +212,10 @@ class MLB(ScoresBackend): away_abbrev = self.get_nested( game, - 'teams:away:team:abbreviation', - default='').upper() + 'teams:away:team:abbreviation').upper() home_abbrev = self.get_nested( game, - 'teams:home:team:abbreviation', - default='').upper() + 'teams:home:team:abbreviation').upper() if away_abbrev and home_abbrev: try: for team in (home_abbrev, away_abbrev): @@ -240,27 +238,24 @@ class MLB(ScoresBackend): ret['id'] = game['gamePk'] ret['inning'] = self.get_nested(linescore, 'currentInning', default=0) - ret['outs'] = self.get_nested(linescore, 'outs', default='') + ret['outs'] = self.get_nested(linescore, 'outs') ret['live_url'] = self.live_url % ret['id'] for team in ('away', 'home'): team_data = self.get_nested(game, 'teams:%s' % team, default={}) if team == 'home': - ret['venue'] = self.get_nested(team_data, 'venue:name', default='') + ret['venue'] = self.get_nested(team_data, 'venue:name') ret['%s_city' % team] = self.get_nested( team_data, - 'team:locationName', - default='') + 'team:locationName') ret['%s_name' % team] = self.get_nested( team_data, - 'team:teamName', - default='') + 'team:teamName') ret['%s_abbrev' % team] = self.get_nested( team_data, - 'team:abbreviation', - default='') + 'team:abbreviation') ret['%s_wins' % team] = self.get_nested( team_data, @@ -279,7 +274,7 @@ class MLB(ScoresBackend): for key in ('delay', 'postponed', 'suspended'): ret[key] = '' - ret['status'] = self.get_nested(game, 'status:detailedState', default='').replace(' ', '_').lower() + ret['status'] = self.get_nested(game, 'status:detailedState').replace(' ', '_').lower() if ret['status'] == 'delayed_start': ret['status'] = 'pregame' @@ -303,14 +298,14 @@ class MLB(ScoresBackend): except ValueError: ret['extra_innings'] = '' - top_bottom = self.get_nested(linescore, 'inningHalf', default='').lower() + top_bottom = self.get_nested(linescore, 'inningHalf').lower() ret['top_bottom'] = self.inning_top if top_bottom == 'top' \ else self.inning_bottom if top_bottom == 'bottom' \ else '' try: game_time = datetime.strptime( - self.get_nested(game, 'gameDate', default=''), + self.get_nested(game, 'gameDate'), '%Y-%m-%dT%H:%M:%SZ') except ValueError as exc: # Log when the date retrieved from the API return doesn't match the diff --git a/i3pystatus/scores/nhl.py b/i3pystatus/scores/nhl.py index dfcdaef..5c3281d 100644 --- a/i3pystatus/scores/nhl.py +++ b/i3pystatus/scores/nhl.py @@ -242,73 +242,100 @@ class NHL(ScoresBackend): def process_game(self, game): ret = {} - def _update(ret_key, game_key=None, callback=None, default='?'): - ret[ret_key] = self.get_nested(game, - game_key or ret_key, - callback=callback, - default=default) - self.logger.debug('Processing %s game data: %s', self.__class__.__name__, game) - _update('id', 'gamePk') + linescore = self.get_nested(game, 'linescore', default={}) + + ret['id'] = game['gamePk'] ret['live_url'] = self.live_url % ret['id'] - _update('period', 'linescore:currentPeriodOrdinal', default='') - _update('time_remaining', - 'linescore:currentPeriodTimeRemaining', - lambda x: x.capitalize(), - default='') - _update('venue', 'venue:name') + ret['period'] = self.get_nested( + linescore, + 'currentPeriodOrdinal') + ret['time_remaining'] = self.get_nested( + linescore, + 'currentPeriodTimeRemaining', + callback=lambda x: x.capitalize()) + ret['venue'] = self.get_nested( + game, + 'venue:name') - pp_strength = self.get_nested(game, - 'linescore:powerPlayStrength', - default='') - for team in ('home', 'away'): - _update('%s_score' % team, - 'teams:%s:score' % team, - callback=self.force_int, - default=0) - _update('%s_wins' % team, - 'teams:%s:leagueRecord:wins' % team, - callback=self.force_int, - default=0) - _update('%s_losses' % team, - 'teams:%s:leagueRecord:losses' % team, - callback=self.force_int, - default=0) - _update('%s_otl' % team, - 'teams:%s:leagueRecord:ot' % team, - callback=self.force_int, - default=0) + pp_strength = self.get_nested(linescore, 'powerPlayStrength') - _update('%s_city' % team, 'teams:%s:team:shortName' % team) - _update('%s_name' % team, 'teams:%s:team:teamName' % team) - _update('%s_abbrev' % team, 'teams:%s:team:abbreviation' % team) - _update('%s_power_play' % team, - 'linescore:teams:%s:powerPlay' % team, - lambda x: pp_strength if x and pp_strength != 'Even' else '') - _update('%s_empty_net' % team, - 'linescore:teams:%s:goaliePulled' % team, - lambda x: self.empty_net if x else '') + for team in ('away', 'home'): + team_data = self.get_nested(game, 'teams:%s' % team, default={}) + + if team == 'home': + ret['venue'] = self.get_nested(team_data, 'venue:name') + + ret['%s_score' % team] = self.get_nested( + team_data, + 'score', + callback=self.force_int, + default=0) + ret['%s_wins' % team] = self.get_nested( + team_data, + 'leagueRecord:wins', + callback=self.force_int, + default=0) + ret['%s_losses' % team] = self.get_nested( + team_data, + 'leagueRecord:losses', + callback=self.force_int, + default=0) + ret['%s_otl' % team] = self.get_nested( + team_data, + 'leagueRecord:ot', + callback=self.force_int, + default=0) + + ret['%s_city' % team] = self.get_nested( + team_data, + 'team:shortName') + ret['%s_name' % team] = self.get_nested( + team_data, + 'team:teamName') + ret['%s_abbrev' % team] = self.get_nested( + team_data, + 'team:abbreviation') + ret['%s_power_play' % team] = self.get_nested( + linescore, + 'teams:%s:powerPlay' % team, + callback=lambda x: pp_strength if x and pp_strength != 'Even' else '') + ret['%s_empty_net' % team] = self.get_nested( + linescore, + 'teams:%s:goaliePulled' % team, + callback=lambda x: self.empty_net if x else '') if game.get('gameType') == 'P': for team in ('home', 'away'): - # Series wins are the remainder of dividing wins by 4 - ret['_'.join((team, 'wins'))] %= 4 + # Calculate wins in current series. The win value will be the + # total wins thus far in the playoffs, so a positive win count + # which is equally divisible by 4 indicates 4 wins (i.e. a + # series win). Otherwise, the series win will be the remainder + # when dividing total wins by 4. + key = '%s_wins' % team + if ret[key] and ret[key] % 4 == 0: + ret[key] = 4 + else: + ret[key] %= 4 + # Series losses are the other team's wins ret['home_losses'] = ret['away_wins'] ret['away_losses'] = ret['home_wins'] - _update('status', - 'status:abstractGameState', - lambda x: x.lower().replace(' ', '_')) + ret['status'] = self.get_nested( + game, + 'status:abstractGameState', + callback=lambda x: x.lower().replace(' ', '_')) if ret['status'] == 'live': ret['status'] = 'in_progress' elif ret['status'] == 'final': - _update('overtime', - 'linescore:currentPeriodOrdinal', - lambda x: x if 'OT' in x or x == 'SO' else '') + ret['overtime'] = self.get_nested( + linescore, + 'currentPeriodOrdinal', + callback=lambda x: x if 'OT' in x or x == 'SO' else '') elif ret['status'] != 'in_progress': ret['status'] = 'pregame'