From c4f5023e255af5a0aed256790e6361239d38e4a9 Mon Sep 17 00:00:00 2001 From: Tushar Makkar Date: Sat, 7 May 2016 14:03:01 +0530 Subject: [PATCH 1/4] Watch command added --- soccer/main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/soccer/main.py b/soccer/main.py index 8ed8b8d..ae688b4 100644 --- a/soccer/main.py +++ b/soccer/main.py @@ -3,6 +3,7 @@ import requests import sys import json +import time as python_time from soccer import leagueids from soccer.exceptions import IncorrectParametersException, APIErrorException @@ -223,7 +224,9 @@ def list_team_codes(): @click.option('--apikey', default=load_config_key, help="API key to use") @click.option('--list', 'listcodes', is_flag=True, help="List all valid team code/team name pairs") @click.option('--live', is_flag=True, help="Shows live scores from various leagues") -@click.option('--use12hour', is_flag=True, default=False, help="Displays the time using 12 hour format instead of 24 (default).") +@click.option('--watch', '-w', default=None, help="Shows live scores after a time interval") +@click.option('--use12hour', is_flag=True, default=False, + help="Displays the time using 12 hour format instead of 24 (default).") @click.option('--standings', is_flag=True, help="Standings for a particular league") @click.option('--league', '-league', type=click.Choice(LEAGUE_IDS.keys()), help=("Choose the league whose fixtures you want to see. " @@ -244,7 +247,8 @@ def list_team_codes(): help='Output in JSON format') @click.option('-o', '--output-file', default=None, help="Save output to a file (only if csv or json option is provided)") -def main(league, time, standings, team, live, use12hour, players, output_format, output_file, upcoming, lookup, listcodes, apikey): +def main(league, time, standings, team, live, watch, use12hour, players, output_format, output_file, upcoming, lookup, + listcodes, apikey): """A CLI for live and past football scores from various football leagues""" global headers headers = { @@ -264,6 +268,13 @@ def main(league, time, standings, team, live, use12hour, players, output_format, get_live_scores(writer, use12hour) return + if watch: + while True: + get_live_scores(writer, use12hour) + python_time.sleep(int(watch)) + print(chr(27) + "[2J") + return + if standings: if not league: raise IncorrectParametersException('Please specify a league. ' From c992da4b36251db81bc1d8a0aeee5c6d36f7348b Mon Sep 17 00:00:00 2001 From: Tushar Makkar Date: Mon, 16 May 2016 20:03:21 +0530 Subject: [PATCH 2/4] Saturn's comment integration --- soccer/main.py | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/soccer/main.py b/soccer/main.py index ae688b4..3292185 100644 --- a/soccer/main.py +++ b/soccer/main.py @@ -9,7 +9,6 @@ from soccer.exceptions import IncorrectParametersException, APIErrorException from soccer.writers import get_writer - BASE_URL = 'http://api.football-data.org/alpha/' LIVE_URL = 'http://soccer-cli.appspot.com/' LEAGUE_IDS = leagueids.LEAGUE_IDS @@ -77,7 +76,7 @@ def load_config_key(): def _get(url): """Handles api.football-data.org requests""" - req = requests.get(BASE_URL+url, headers=headers) + req = requests.get(BASE_URL + url, headers=headers) if req.status_code == requests.codes.ok: return req @@ -115,7 +114,7 @@ def get_team_scores(team, time, writer, show_upcoming, use_12_hour_format): if team_id: try: req = _get('teams/{team_id}/fixtures?timeFrame={time_frame}{time}'.format( - team_id=team_id, time_frame=time_frame, time=time)) + team_id=team_id, time_frame=time_frame, time=time)) team_scores = req.json() if len(team_scores["fixtures"]) == 0: click.secho("No action during past week. Change the time " @@ -135,7 +134,7 @@ def get_standings(league, writer): league_id = LEAGUE_IDS[league] try: req = _get('soccerseasons/{id}/leagueTable'.format( - id=league_id)) + id=league_id)) writer.standings(req.json(), league) except APIErrorException: # Click handles incorrect League codes so this will only come up @@ -145,7 +144,6 @@ def get_standings(league, writer): def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): - """ Queries the API and fetches the scores for fixtures based upon the league and time parameter @@ -155,7 +153,7 @@ def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): try: league_id = LEAGUE_IDS[league] req = _get('soccerseasons/{id}/fixtures?timeFrame={time_frame}{time}'.format( - id=league_id, time_frame=time_frame, time=str(time))) + id=league_id, time_frame=time_frame, time=str(time))) fixtures_results = req.json() # no fixtures in the past week. display a help message and return if len(fixtures_results["fixtures"]) == 0: @@ -169,7 +167,7 @@ def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): # When no league specified. Print all available in time frame. try: req = _get('fixtures?timeFrame={time_frame}{time}'.format( - time_frame=time_frame, time=str(time))) + time_frame=time_frame, time=str(time))) fixtures_results = req.json() writer.league_scores(fixtures_results, time, show_upcoming, use_12_hour_format) except APIErrorException: @@ -184,7 +182,7 @@ def get_team_players(team, writer): team_id = TEAM_NAMES.get(team, None) try: req = _get('teams/{team_id}/players'.format( - team_id=team_id)) + team_id=team_id)) team_players = req.json() if int(team_players["count"]) == 0: click.secho("No players found for this team", fg="red", bold=True) @@ -220,11 +218,14 @@ def list_team_codes(): click.secho("") -@click.command() +@click.command(context_settings=dict( + ignore_unknown_options=True, + allow_extra_args=True, + help_option_names=['-h', '--help'], +)) @click.option('--apikey', default=load_config_key, help="API key to use") @click.option('--list', 'listcodes', is_flag=True, help="List all valid team code/team name pairs") @click.option('--live', is_flag=True, help="Shows live scores from various leagues") -@click.option('--watch', '-w', default=None, help="Shows live scores after a time interval") @click.option('--use12hour', is_flag=True, default=False, help="Displays the time using 12 hour format instead of 24 (default).") @click.option('--standings', is_flag=True, help="Standings for a particular league") @@ -247,9 +248,11 @@ def list_team_codes(): help='Output in JSON format') @click.option('-o', '--output-file', default=None, help="Save output to a file (only if csv or json option is provided)") -def main(league, time, standings, team, live, watch, use12hour, players, output_format, output_file, upcoming, lookup, +@click.pass_context +def main(ctx, league, time, standings, team, live, use12hour, players, output_format, output_file, upcoming, lookup, listcodes, apikey): - """A CLI for live and past football scores from various football leagues""" + """A CLI for live and past football scores from various football leagues\n + [Option]-w/--watch time: Refreshes screen after a given time interval in sec (default = 120 sec)""" global headers headers = { 'X-Auth-Token': apikey @@ -259,7 +262,6 @@ def main(league, time, standings, team, live, watch, use12hour, players, output_ raise IncorrectParametersException('Printing output to stdout and ' 'saving to a file are mutually exclusive') writer = get_writer(output_format, output_file) - if listcodes: list_team_codes() return @@ -267,11 +269,23 @@ def main(league, time, standings, team, live, watch, use12hour, players, output_ if live: get_live_scores(writer, use12hour) return - - if watch: + if len(ctx.args): + sleep_time = 120 # Default watch time + flag_watch = False + for var in ctx.args: + if "--watch" == var or "--watch" == var.split("=")[0]: + flag_watch = True + if "-w" == var or "-w" == var.split("=")[0]: + flag_watch = True + if '=' in var: + sleep_time = int(var.split("=")[1]) + if type(var) == int: + sleep_time = int(var) + if not flag_watch: + return while True: get_live_scores(writer, use12hour) - python_time.sleep(int(watch)) + python_time.sleep(sleep_time) print(chr(27) + "[2J") return @@ -297,5 +311,6 @@ def main(league, time, standings, team, live, watch, use12hour, players, output_ except IncorrectParametersException as e: click.secho(e.message, fg="red", bold=True) + if __name__ == '__main__': main() From 814dd9c3e17d5f2e765881afee73c382422af43d Mon Sep 17 00:00:00 2001 From: Tushar Makkar Date: Mon, 25 Jul 2016 19:18:56 +0530 Subject: [PATCH 3/4] Reverted back the original one --- soccer/main.py | 55 ++++++++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/soccer/main.py b/soccer/main.py index ad99742..dcfc702 100644 --- a/soccer/main.py +++ b/soccer/main.py @@ -9,6 +9,7 @@ from soccer.exceptions import IncorrectParametersException, APIErrorException from soccer.writers import get_writer + BASE_URL = 'http://api.football-data.org/alpha/' LIVE_URL = 'http://soccer-cli.appspot.com/' LEAGUE_IDS = leagueids.LEAGUE_IDS @@ -76,7 +77,7 @@ def load_config_key(): def _get(url): """Handles api.football-data.org requests""" - req = requests.get(BASE_URL + url, headers=headers) + req = requests.get(BASE_URL+url, headers=headers) if req.status_code == requests.codes.ok: return req @@ -114,7 +115,7 @@ def get_team_scores(team, time, writer, show_upcoming, use_12_hour_format): if team_id: try: req = _get('teams/{team_id}/fixtures?timeFrame={time_frame}{time}'.format( - team_id=team_id, time_frame=time_frame, time=time)) + team_id=team_id, time_frame=time_frame, time=time)) team_scores = req.json() if len(team_scores["fixtures"]) == 0: click.secho("No action during past week. Change the time " @@ -134,7 +135,7 @@ def get_standings(league, writer): league_id = LEAGUE_IDS[league] try: req = _get('soccerseasons/{id}/leagueTable'.format( - id=league_id)) + id=league_id)) writer.standings(req.json(), league) except APIErrorException: # Click handles incorrect League codes so this will only come up @@ -144,6 +145,7 @@ def get_standings(league, writer): def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): + """ Queries the API and fetches the scores for fixtures based upon the league and time parameter @@ -153,7 +155,7 @@ def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): try: league_id = LEAGUE_IDS[league] req = _get('soccerseasons/{id}/fixtures?timeFrame={time_frame}{time}'.format( - id=league_id, time_frame=time_frame, time=str(time))) + id=league_id, time_frame=time_frame, time=str(time))) fixtures_results = req.json() # no fixtures in the past week. display a help message and return if len(fixtures_results["fixtures"]) == 0: @@ -167,7 +169,7 @@ def get_league_scores(league, time, writer, show_upcoming, use_12_hour_format): # When no league specified. Print all available in time frame. try: req = _get('fixtures?timeFrame={time_frame}{time}'.format( - time_frame=time_frame, time=str(time))) + time_frame=time_frame, time=str(time))) fixtures_results = req.json() writer.league_scores(fixtures_results, time, show_upcoming, use_12_hour_format) except APIErrorException: @@ -182,7 +184,7 @@ def get_team_players(team, writer): team_id = TEAM_NAMES.get(team, None) try: req = _get('teams/{team_id}/players'.format( - team_id=team_id)) + team_id=team_id)) team_players = req.json() if int(team_players["count"]) == 0: click.secho("No players found for this team", fg="red", bold=True) @@ -218,18 +220,14 @@ def list_team_codes(): click.secho("") -<<<<<<< HEAD -@click.command(context_settings=dict( - ignore_unknown_options=True, - allow_extra_args=True, - help_option_names=['-h', '--help'], -)) +@click.command() @click.option('--apikey', default=load_config_key, help="API key to use.") @click.option('--list', 'listcodes', is_flag=True, help="List all valid team code/team name pairs.") @click.option('--live', is_flag=True, help="Shows live scores from various leagues.") +@click.option('--watch', '-w', default=None, help="Shows live scores after a time interval") @click.option('--use12hour', is_flag=True, default=False, help="Displays the time using 12 hour format instead of 24 (default).") @click.option('--standings', is_flag=True, @@ -253,12 +251,11 @@ def list_team_codes(): @click.option('--json', 'output_format', flag_value='json', help='Output in JSON format.') @click.option('-o', '--output-file', default=None, - help="Save output to a file (only if csv or json option is provided)") -@click.pass_context -def main(ctx, league, time, standings, team, live, use12hour, players, output_format, output_file, upcoming, lookup, - listcodes, apikey): - """A CLI for live and past football scores from various football leagues\n - [Option]-w/--watch time: Refreshes screen after a given time interval in sec (default = 120 sec) + help="Save output to a file (only if csv or json option is provided).") +def main(league, time, standings, team, live, watch, use12hour, players, output_format, + output_file, upcoming, lookup, listcodes, apikey): + """ + A CLI for live and past football scores from various football leagues. League codes: @@ -285,6 +282,7 @@ def main(ctx, league, time, standings, team, live, use12hour, players, output_fo raise IncorrectParametersException('Printing output to stdout and ' 'saving to a file are mutually exclusive') writer = get_writer(output_format, output_file) + if listcodes: list_team_codes() return @@ -292,23 +290,11 @@ def main(ctx, league, time, standings, team, live, use12hour, players, output_fo if live: get_live_scores(writer, use12hour) return - if len(ctx.args): - sleep_time = 120 # Default watch time - flag_watch = False - for var in ctx.args: - if "--watch" == var or "--watch" == var.split("=")[0]: - flag_watch = True - if "-w" == var or "-w" == var.split("=")[0]: - flag_watch = True - if '=' in var: - sleep_time = int(var.split("=")[1]) - if type(var) == int: - sleep_time = int(var) - if not flag_watch: - return + + if watch: while True: get_live_scores(writer, use12hour) - python_time.sleep(sleep_time) + python_time.sleep(int(watch)) print(chr(27) + "[2J") return @@ -334,6 +320,5 @@ def main(ctx, league, time, standings, team, live, use12hour, players, output_fo except IncorrectParametersException as e: click.secho(e.message, fg="red", bold=True) - if __name__ == '__main__': - main() + main() \ No newline at end of file From e98d10f917b9069d67efc89f2f67110a583e6546 Mon Sep 17 00:00:00 2001 From: Tushar Makkar Date: Mon, 25 Jul 2016 19:23:00 +0530 Subject: [PATCH 4/4] Help text improved --- soccer/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soccer/main.py b/soccer/main.py index dcfc702..c81eba6 100644 --- a/soccer/main.py +++ b/soccer/main.py @@ -227,7 +227,7 @@ def list_team_codes(): help="List all valid team code/team name pairs.") @click.option('--live', is_flag=True, help="Shows live scores from various leagues.") -@click.option('--watch', '-w', default=None, help="Shows live scores after a time interval") +@click.option('--watch', '-w', default=None, help="Refreshes screen after a given time interval in sec (in seconds)") @click.option('--use12hour', is_flag=True, default=False, help="Displays the time using 12 hour format instead of 24 (default).") @click.option('--standings', is_flag=True,