Skip to content

Commit

Permalink
Fix live scores display
Browse files Browse the repository at this point in the history
  • Loading branch information
Saturn committed Jan 14, 2019
1 parent 704f292 commit c0177d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion soccer/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ def get_live_scores(self, use_12_hour_format):
"""Gets the live scores"""
req = requests.get(RequestHandler.LIVE_URL)
if req.status_code == requests.codes.ok:
scores_data = []
scores = req.json()
if len(scores["games"]) == 0:
click.secho("No live action currently", fg="red", bold=True)
return
self.writer.live_scores(scores)

for score in scores['games']:
# match football-data api structure
d = {}
d['homeTeam'] = {'name': score['homeTeamName']}
d['awayTeam'] = {'name': score['awayTeamName']}
d['score'] = {'fullTime': {'homeTeam': score['goalsHomeTeam'],
'awayTeam': score['goalsAwayTeam']}}
d['league'] = score['league']
d['time'] = score['time']
scores_data.append(d)
self.writer.live_scores(scores_data)
else:
click.secho("There was problem getting live scores", fg="red", bold=True)

Expand Down
6 changes: 3 additions & 3 deletions soccer/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def __init__(self, output_file):
)
self.colors = type('Enum', (), enums)

def live_scores(self, live_scores, use_12_hour_format):
def live_scores(self, live_scores):
"""Prints the live scores in a pretty format"""
scores = sorted(live_scores["games"], key=lambda x: x["league"])
scores = sorted(live_scores, key=lambda x: x["league"])
for league, games in groupby(scores, key=lambda x: x["league"]):
self.league_header(league)
for game in games:
self.scores(self.parse_result(game), add_new_line=False)
click.secho(' %s' % Stdout.utc_to_local(game["time"],
use_12_hour_format),
use_12_hour_format=False),
fg=self.colors.TIME)
click.echo()

Expand Down

0 comments on commit c0177d1

Please sign in to comment.