<s> import click <EOL> import os <EOL> import requests <EOL> import sys <EOL> import json <EOL> from soccer import leagueids <EOL> from soccer . exceptions import IncorrectParametersException , APIErrorException <EOL> from soccer . writers import get_writer <EOL> BASE_URL = '<STR_LIT>' <EOL> LIVE_URL = '<STR_LIT>' <EOL> LEAGUE_IDS = leagueids . LEAGUE_IDS <EOL> def load_json ( file ) : <EOL> """<STR_LIT>""" <EOL> here = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> with open ( os . path . join ( here , file ) ) as jfile : <EOL> data = json . load ( jfile ) <EOL> return data <EOL> TEAM_DATA = load_json ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> TEAM_NAMES = { team [ "<STR_LIT:code>" ] : team [ "<STR_LIT:id>" ] for team in TEAM_DATA } <EOL> def get_input_key ( ) : <EOL> """<STR_LIT>""" <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> click . secho ( "<STR_LIT>" . format ( BASE_URL ) , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> while True : <EOL> confkey = click . prompt ( click . style ( "<STR_LIT>" , <EOL> fg = "<STR_LIT>" , bold = True ) ) <EOL> if len ( confkey ) == <NUM_LIT:32> : <EOL> try : <EOL> int ( confkey , <NUM_LIT:16> ) <EOL> except ValueError : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> else : <EOL> break <EOL> else : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> return confkey <EOL> def load_config_key ( ) : <EOL> """<STR_LIT>""" <EOL> global api_token <EOL> try : <EOL> api_token = os . environ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> home = os . path . expanduser ( "<STR_LIT>" ) <EOL> config = os . path . join ( home , "<STR_LIT>" ) <EOL> if not os . path . exists ( config ) : <EOL> with open ( config , "<STR_LIT:w>" ) as cfile : <EOL> key = get_input_key ( ) <EOL> cfile . write ( key ) <EOL> else : <EOL> with open ( config , "<STR_LIT:r>" ) as cfile : <EOL> key = cfile . read ( ) <EOL> if key : <EOL> api_token = key <EOL> else : <EOL> os . remove ( config ) <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> . format ( BASE_URL ) , fg = "<STR_LIT>" , bold = True ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return api_token <EOL> def _get ( url ) : <EOL> """<STR_LIT>""" <EOL> req = requests . get ( BASE_URL + url , headers = headers ) <EOL> if req . status_code == requests . codes . ok : <EOL> return req <EOL> if req . status_code == requests . codes . bad : <EOL> raise APIErrorException ( '<STR_LIT>' ) <EOL> if req . status_code == requests . codes . forbidden : <EOL> raise APIErrorException ( '<STR_LIT>' ) <EOL> if req . status_code == requests . codes . not_found : <EOL> raise APIErrorException ( '<STR_LIT>' ) <EOL> if req . status_codes == requests . codes . too_many_requests : <EOL> raise APIErrorException ( '<STR_LIT>' ) <EOL> def get_live_scores ( writer , use_12_hour_format ) : <EOL> """<STR_LIT>""" <EOL> req = requests . get ( LIVE_URL ) <EOL> if req . status_code == requests . codes . ok : <EOL> scores = req . json ( ) <EOL> if len ( scores [ "<STR_LIT>" ] ) == <NUM_LIT:0> : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> return <EOL> writer . live_scores ( scores , use_12_hour_format ) <EOL> else : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> def get_team_scores ( team , time , writer , show_upcoming , use_12_hour_format ) : <EOL> """<STR_LIT>""" <EOL> team_id = TEAM_NAMES . get ( team , None ) <EOL> time_frame = '<STR_LIT:n>' if show_upcoming else '<STR_LIT:p>' <EOL> if team_id : <EOL> try : <EOL> req = _get ( '<STR_LIT>' . format ( <EOL> team_id = team_id , time_frame = time_frame , time = time ) ) <EOL> team_scores = req . json ( ) <EOL> if len ( team_scores [ "<STR_LIT>" ] ) == <NUM_LIT:0> : <EOL> click . secho ( "<STR_LIT>" <EOL> "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> else : <EOL> writer . team_scores ( team_scores , time , show_upcoming , use_12_hour_format ) <EOL> except APIErrorException as e : <EOL> click . secho ( e . args [ <NUM_LIT:0> ] , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> else : <EOL> click . secho ( "<STR_LIT>" , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> def get_standings ( league , writer ) : <EOL> """<STR_LIT>""" <EOL> league_id = LEAGUE_IDS [ league ] <EOL> try : <EOL> req = _get ( '<STR_LIT>' . format ( <EOL> id = league_id ) ) <EOL> writer . standings ( req . json ( ) , league ) <EOL> except APIErrorException : <EOL> click . secho ( "<STR_LIT>" . format ( league = league ) , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> def get_league_scores ( league , time , writer , show_upcoming , use_12_hour_format ) : <EOL> """<STR_LIT>""" <EOL> time_frame = '<STR_LIT:n>' if show_upcoming else '<STR_LIT:p>' <EOL> if league : <EOL> try : <EOL> league_id = LEAGUE_IDS [ league ] <EOL> req = _get ( '<STR_LIT>' . format ( <EOL> id = league_id , time_frame = time_frame , time = str ( time ) ) ) <EOL> fixtures_results = req . json ( ) <EOL> if len ( fixtures_results [ "<STR_LIT>" ] ) == <NUM_LIT:0> : <EOL> click . secho ( "<STR_LIT>" . format ( league = league ) , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> return <EOL> writer . league_scores ( fixtures_results , time , show_upcoming , use_12_hour_format ) <EOL> except APIErrorException : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> else : <EOL> try : <EOL> req = _get ( '<STR_LIT>' . format ( <EOL> time_frame = time_frame , time = str ( time ) ) ) <EOL> fixtures_results = req . json ( ) <EOL> writer . league_scores ( fixtures_results , time , show_upcoming , use_12_hour_format ) <EOL> except APIErrorException : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> def get_team_players ( team , writer ) : <EOL> """<STR_LIT>""" <EOL> team_id = TEAM_NAMES . get ( team , None ) <EOL> try : <EOL> req = _get ( '<STR_LIT>' . format ( <EOL> team_id = team_id ) ) <EOL> team_players = req . json ( ) <EOL> if int ( team_players [ "<STR_LIT:count>" ] ) == <NUM_LIT:0> : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> else : <EOL> writer . team_players ( team_players ) <EOL> except APIErrorException : <EOL> click . secho ( "<STR_LIT>" , <EOL> fg = "<STR_LIT>" , bold = True ) <EOL> def map_team_id ( code ) : <EOL> """<STR_LIT>""" <EOL> for team in TEAM_DATA : <EOL> if team [ "<STR_LIT:code>" ] == code : <EOL> click . secho ( team [ "<STR_LIT:name>" ] , fg = "<STR_LIT>" ) <EOL> break <EOL> else : <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" , bold = True ) <EOL> def list_team_codes ( ) : <EOL> """<STR_LIT>""" <EOL> cleanlist = sorted ( TEAM_DATA , key = lambda k : ( k [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] , k [ "<STR_LIT:code>" ] ) ) <EOL> leaguenames = sorted ( list ( set ( [ team [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] for team in cleanlist ] ) ) ) <EOL> for league in leaguenames : <EOL> teams = [ team for team in cleanlist if team [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] == league ] <EOL> click . secho ( league , fg = "<STR_LIT>" , bold = True ) <EOL> for team in teams : <EOL> if team [ "<STR_LIT:code>" ] != "<STR_LIT:null>" : <EOL> click . secho ( u"<STR_LIT>" . format ( team [ "<STR_LIT:code>" ] , team [ "<STR_LIT:name>" ] ) , fg = "<STR_LIT>" ) <EOL> click . secho ( "<STR_LIT>" ) <EOL> @ click . command ( ) <EOL> @ click . option ( '<STR_LIT>' , default = load_config_key , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , default = False , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , type = click . Choice ( LEAGUE_IDS . keys ( ) ) , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , type = click . Choice ( TEAM_NAMES . keys ( ) ) , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , default = <NUM_LIT:6> , <EOL> help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , default = False , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , flag_value = '<STR_LIT>' , <EOL> default = True , help = "<STR_LIT>" ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , flag_value = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , flag_value = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , default = None , <EOL> help = "<STR_LIT>" ) <EOL> def main ( league , time , standings , team , live , use12hour , players , output_format , output_file , upcoming , lookup , listcodes , apikey ) : <EOL> """<STR_LIT>""" <EOL> global headers <EOL> headers = { <EOL> '<STR_LIT>' : apikey <EOL> } <EOL> try : <EOL> if output_format == '<STR_LIT>' and output_file : <EOL> raise IncorrectParametersException ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> writer = get_writer ( output_format , output_file ) <EOL> if listcodes : <EOL> list_team_codes ( ) <EOL> return <EOL> if live : <EOL> get_live_scores ( writer , use12hour ) <EOL> return <EOL> if standings : <EOL> if not league : <EOL> raise IncorrectParametersException ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> get_standings ( league , writer ) <EOL> return <EOL> if team : <EOL> if lookup : <EOL> map_team_id ( team ) <EOL> return <EOL> if players : <EOL> get_team_players ( team , writer ) <EOL> return <EOL> else : <EOL> get_team_scores ( team , time , writer , upcoming , use12hour ) <EOL> return <EOL> get_league_scores ( league , time , writer , upcoming , use12hour ) <EOL> except IncorrectParametersException as e : <EOL> click . secho ( e . message , fg = "<STR_LIT>" , bold = True ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
