Skip to content

Commit 189cd76

Browse files
committed
Merge branch 'carlosvargas-upcoming-games'
2 parents 5eca6d3 + f21bb20 commit 189cd76

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

soccer/main.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,20 @@ def get_live_scores(writer, use_12_hour_format):
6363
click.secho("There was problem getting live scores", fg="red", bold=True)
6464

6565

66-
def get_team_scores(team, time, writer):
66+
def get_team_scores(team, time, writer, show_upcoming, use_12_hour_format):
6767
"""Queries the API and gets the particular team scores"""
6868
team_id = TEAM_NAMES.get(team, None)
69+
time_frame = 'n' if show_upcoming else 'p'
6970
if team_id:
7071
try:
71-
req = _get('teams/{team_id}/fixtures?timeFrame=p{time}'.format(
72-
team_id=team_id, time=time))
72+
req = _get('teams/{team_id}/fixtures?timeFrame={time_frame}{time}'.format(
73+
team_id=team_id, time_frame=time_frame, time=time))
7374
team_scores = req.json()
7475
if len(team_scores["fixtures"]) == 0:
7576
click.secho("No action during past week. Change the time "
7677
"parameter to get more fixtures.", fg="red", bold=True)
7778
else:
78-
writer.team_scores(team_scores, time)
79+
writer.team_scores(team_scores, time, show_upcoming, use_12_hour_format)
7980
except APIErrorException as e:
8081
click.secho(e.args[0],
8182
fg="red", bold=True)
@@ -98,32 +99,34 @@ def get_standings(league, writer):
9899
fg="red", bold=True)
99100

100101

101-
def get_league_scores(league, time, writer):
102+
def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format):
103+
102104
"""
103105
Queries the API and fetches the scores for fixtures
104106
based upon the league and time parameter
105107
"""
108+
time_frame = 'n' if show_upcoming else 'p'
106109
if league:
107110
try:
108111
league_id = LEAGUE_IDS[league]
109-
req = _get('soccerseasons/{id}/fixtures?timeFrame=p{time}'.format(
110-
id=league_id, time=str(time)))
112+
req = _get('soccerseasons/{id}/fixtures?timeFrame={time_frame}{time}'.format(
113+
id=league_id, time_frame=time_frame, time=str(time)))
111114
fixtures_results = req.json()
112115
# no fixtures in the past week. display a help message and return
113116
if len(fixtures_results["fixtures"]) == 0:
114117
click.secho("No {league} matches in the past week.".format(league=league),
115118
fg="red", bold=True)
116119
return
117-
writer.league_scores(fixtures_results, time)
120+
writer.league_scores(fixtures_results, time, show_upcoming, use_12_hour_format)
118121
except APIErrorException:
119122
click.secho("No data for the given league.", fg="red", bold=True)
120123
else:
121124
# When no league specified. Print all available in time frame.
122125
try:
123-
req = _get('fixtures?timeFrame=p{time}'.format(
124-
time=str(time)))
126+
req = _get('fixtures?timeFrame={time_frame}{time}'.format(
127+
time_frame=time_frame, time=str(time)))
125128
fixtures_results = req.json()
126-
writer.league_scores(fixtures_results, time)
129+
writer.league_scores(fixtures_results, time, show_upcoming, use_12_hour_format)
127130
except APIErrorException:
128131
click.secho("No data available.", fg="red", bold=True)
129132

@@ -160,6 +163,7 @@ def get_team_players(team, writer):
160163
"See team codes listed in README."))
161164
@click.option('--time', default=6,
162165
help="The number of days in the past for which you want to see the scores")
166+
@click.option('--upcoming', is_flag=True, default=False, help="Displays upcoming games when used with --time command.")
163167
@click.option('--stdout', 'output_format', flag_value='stdout',
164168
default=True, help="Print to stdout")
165169
@click.option('--csv', 'output_format', flag_value='csv',
@@ -168,7 +172,7 @@ def get_team_players(team, writer):
168172
help='Output in JSON format')
169173
@click.option('-o', '--output-file', default=None,
170174
help="Save output to a file (only if csv or json option is provided)")
171-
def main(league, time, standings, team, live, use12hour, players, output_format, output_file):
175+
def main(league, time, standings, team, live, use12hour, players, output_format, output_file, upcoming):
172176
"""A CLI for live and past football scores from various football leagues"""
173177
try:
174178
if output_format == 'stdout' and output_file:
@@ -192,10 +196,10 @@ def main(league, time, standings, team, live, use12hour, players, output_format,
192196
get_team_players(team, writer)
193197
return
194198
else:
195-
get_team_scores(team, time, writer)
199+
get_team_scores(team, time, writer, upcoming, use12hour)
196200
return
197201

198-
get_league_scores(league, time, writer)
202+
get_league_scores(league, time, writer, upcoming, use12hour)
199203
except IncorrectParametersException as e:
200204
click.secho(e.message, fg="red", bold=True)
201205

soccer/writers.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,21 @@ def live_scores(self, live_scores, use_12_hour_format):
8989
fg=self.colors.TIME)
9090
click.echo()
9191

92-
def team_scores(self, team_scores, time):
92+
def team_scores(self, team_scores, time, show_datetime, use_12_hour_format):
9393
"""Prints the teams scores in a pretty format"""
9494
for score in team_scores["fixtures"]:
9595
if score["status"] == "FINISHED":
9696
click.echo()
9797
click.secho("%s\t" % score["date"].split('T')[0],
9898
fg=self.colors.TIME, nl=False)
9999
self.scores(self.parse_result(score))
100+
elif show_datetime:
101+
click.echo()
102+
self.scores(self.parse_result(score), add_new_line=False)
103+
click.secho(' %s' % Stdout.convert_utc_to_local_time(score["date"],
104+
use_12_hour_format, show_datetime),
105+
fg=self.colors.TIME)
106+
100107

101108
def team_players(self, team):
102109
"""Prints the team players in a pretty format"""
@@ -172,14 +179,18 @@ def standings(self, league_table, league):
172179
),
173180
fg=self.colors.POSITION)
174181

175-
def league_scores(self, total_data, time):
182+
def league_scores(self, total_data, time, show_datetime, use_12_hour_format):
176183
"""Prints the data in a pretty format"""
177184
seen = set()
178185
for league, data in self.supported_leagues(total_data):
179186
if league not in seen:
180187
seen.add(league)
181188
self.league_header(league)
182-
self.scores(self.parse_result(data))
189+
self.scores(self.parse_result(data), add_new_line=not show_datetime)
190+
if show_datetime:
191+
click.secho(' %s' % Stdout.convert_utc_to_local_time(data["date"],
192+
use_12_hour_format, show_datetime),
193+
fg=self.colors.TIME)
183194
click.echo()
184195

185196
def league_header(self, league):
@@ -225,24 +236,30 @@ def valid_score(score):
225236
return result
226237

227238
@staticmethod
228-
def convert_utc_to_local_time(time_str, use_12_hour_format):
239+
def convert_utc_to_local_time(time_str, use_12_hour_format, show_datetime=False):
229240
"""Converts the API UTC time string to the local user time."""
230-
if not time_str.endswith(" UTC"):
241+
if not (time_str.endswith(" UTC") or time_str.endswith("Z")):
231242
return time_str
232243

233244
today_utc = datetime.datetime.utcnow()
234245
utc_local_diff = today_utc - datetime.datetime.now()
235246

236-
time_str, _ = time_str.split(" UTC")
237-
utc_time = datetime.datetime.strptime(time_str,'%I:%M %p')
238-
utc_datetime = datetime.datetime(today_utc.year, today_utc.month, today_utc.day,
239-
utc_time.hour, utc_time.minute)
247+
if time_str.endswith(" UTC"):
248+
time_str, _ = time_str.split(" UTC")
249+
utc_time = datetime.datetime.strptime(time_str, '%I:%M %p')
250+
utc_datetime = datetime.datetime(today_utc.year, today_utc.month, today_utc.day,
251+
utc_time.hour, utc_time.minute)
252+
else:
253+
utc_datetime = datetime.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%SZ')
254+
240255
local_time = utc_datetime - utc_local_diff
241256

242257
if use_12_hour_format:
243-
return datetime.datetime.strftime(local_time,'%I:%M %p')
258+
date_format = '%I:%M %p' if not show_datetime else '%a %d, %I:%M %p'
244259
else:
245-
return datetime.datetime.strftime(local_time,'%H:%M')
260+
date_format = '%H:%M' if not show_datetime else '%a %d, %H:%M'
261+
262+
return datetime.datetime.strftime(local_time, date_format)
246263

247264

248265
class Csv(BaseWriter):

0 commit comments

Comments
 (0)