|
3 | 3 | import sys
|
4 | 4 |
|
5 | 5 | from dotenv import load_dotenv
|
| 6 | + |
6 | 7 | load_dotenv()
|
7 | 8 |
|
8 | 9 | LEAGUE_CODE = {
|
9 | | - "BSA": {'code': 2013, 'name': 'Brazilian First Division'}, |
10 | | - "BL": {'code': 2002, 'name': 'Bundesliga'}, # Bundesliga |
11 | | - "ECL": {'code': 2016, 'name': 'English Championship'}, # English Championship |
12 | | - "ERD": {'code': 2003, 'name': 'Eredivisie'}, # Eredivisie [Dutch] |
13 | | - "L1": {'code': 2015, 'name': 'Ligue 1'}, # Ligue 1 |
14 | | - "SPA": {'code': 2014, 'name': 'La Liga'}, # La Liga |
15 | | - "PPL": {'code': 2017, 'name': 'Portugese first division'}, # Portugese First Dvision |
16 | | - "PL": {'code': 2021, 'name': 'Premier League'}, # Premier League |
17 | | - "SA": {'code': 2019, 'name': 'Serie A'}, # Serie A |
| 10 | + "BSA": {"code": 2013, "name": "Brazilian First Division"}, |
| 11 | + "BL": {"code": 2002, "name": "Bundesliga"}, # Bundesliga |
| 12 | + "ECL": {"code": 2016, "name": "English Championship"}, # English Championship |
| 13 | + "ERD": {"code": 2003, "name": "Eredivisie"}, # Eredivisie [Dutch] |
| 14 | + "L1": {"code": 2015, "name": "Ligue 1"}, # Ligue 1 |
| 15 | + "SPA": {"code": 2014, "name": "La Liga"}, # La Liga |
| 16 | + "PPL": { |
| 17 | + "code": 2017, |
| 18 | + "name": "Portugese first division", |
| 19 | + }, # Portugese First Dvision |
| 20 | + "PL": {"code": 2021, "name": "Premier League"}, # Premier League |
| 21 | + "SA": {"code": 2019, "name": "Serie A"}, # Serie A |
18 | 22 | }
|
19 | 23 |
|
20 | | -API_KEY = os.getenv('API_KEY') |
| 24 | +API_KEY = os.getenv("API_KEY") |
21 | 25 | API_URL = "https://api.football-data.org/v2/"
|
22 | | -HEADER = {'X-Auth-Token': str(API_KEY)} |
| 26 | +HEADER = {"X-Auth-Token": str(API_KEY)} |
| 27 | + |
23 | 28 |
|
24 | 29 | def print_standings(league_id):
|
25 | | - |
26 | | - league_code = league_id.get('code') |
| 30 | + |
| 31 | + league_code = league_id.get("code") |
27 | 32 | resource = f"competitions/{league_code}/standings"
|
28 | 33 | api_url = API_URL + resource
|
29 | 34 | r = requests.get(api_url, headers=HEADER)
|
30 | 35 | obj = r.json()
|
31 | 36 |
|
32 | | - str_re = '\nLEAGUE: ' + str(obj['competition']['name']) +\ |
33 | | - ' ' * (75 - 2 - 8 - 10 - len(str(obj['competition']['name']))) +\ |
34 | | - 'MATCHDAY: ' + str(obj['season']['currentMatchday']) + '\n' |
35 | | - str_re += '╔════╤════════════════════════════════════╤════╤════╤════╤════╤═════╤═════╗\n' |
36 | | - str_re += '║ SN │ TEAM │ M │ W │ D │ L │ PTS │ GD ║\n' |
37 | | - str_re += '╠════╪════════════════════════════════════╪════╪════╪════╪════╪═════╪═════╣\n' |
38 | | - for team in obj['standings'][0]['table']: |
39 | | - text = '║ %-2d │ %-34s │ %-2d │ %-2d │ %-2d │ %-2d │ %-3d │ %+-3d ║\n'\ |
40 | | - % (team['position'], team['team']['name'][:34], team['playedGames'], team['won'], |
41 | | - team['draw'], team['lost'], team['points'], team['goalDifference']) |
| 37 | + str_re = ( |
| 38 | + "\nLEAGUE: " |
| 39 | + + str(obj["competition"]["name"]) |
| 40 | + + " " * (75 - 2 - 8 - 10 - len(str(obj["competition"]["name"]))) |
| 41 | + + "MATCHDAY: " |
| 42 | + + str(obj["season"]["currentMatchday"]) |
| 43 | + + "\n" |
| 44 | + ) |
| 45 | + str_re += ( |
| 46 | + "╔════╤════════════════════════════════════╤════╤════╤════╤════╤═════╤═════╗\n" |
| 47 | + ) |
| 48 | + str_re += ( |
| 49 | + "║ SN │ TEAM │ M │ W │ D │ L │ PTS │ GD ║\n" |
| 50 | + ) |
| 51 | + str_re += ( |
| 52 | + "╠════╪════════════════════════════════════╪════╪════╪════╪════╪═════╪═════╣\n" |
| 53 | + ) |
| 54 | + for team in obj["standings"][0]["table"]: |
| 55 | + text = "║ %-2d │ %-34s │ %-2d │ %-2d │ %-2d │ %-2d │ %-3d │ %+-3d ║\n" % ( |
| 56 | + team["position"], |
| 57 | + team["team"]["name"][:34], |
| 58 | + team["playedGames"], |
| 59 | + team["won"], |
| 60 | + team["draw"], |
| 61 | + team["lost"], |
| 62 | + team["points"], |
| 63 | + team["goalDifference"], |
| 64 | + ) |
42 | 65 |
|
43 | 66 | str_re += text
|
44 | 67 |
|
45 | | - str_re += '╚════╧════════════════════════════════════╧════╧════╧════╧════╧═════╧═════╝' |
| 68 | + str_re += ( |
| 69 | + "╚════╧════════════════════════════════════╧════╧════╧════╧════╧═════╧═════╝" |
| 70 | + ) |
46 | 71 |
|
47 | 72 | print(str_re)
|
48 | 73 |
|
| 74 | + |
49 | 75 | if __name__ == "__main__":
|
50 | 76 | try:
|
51 | 77 |
|
52 | 78 | # If API key is not stored
|
53 | | - if API_KEY is None or API_KEY == 'YOUR_KEY_HERE': |
54 | | - print('This script requires a free API key from https://www.football-data.org/ for stats!\ |
55 | | - \nGet a free API Key and store it in the .env file in place of \"YOUR_KEY_HERE\"') |
| 79 | + if API_KEY is None or API_KEY == "YOUR_KEY_HERE": |
| 80 | + print( |
| 81 | + 'This script requires a free API key from https://www.football-data.org/ for stats!\ |
| 82 | + \nGet a free API Key and store it in the .env file in place of "YOUR_KEY_HERE"' |
| 83 | + ) |
56 | 84 | sys.exit(1)
|
57 | 85 |
|
58 | 86 | arg = sys.argv[1]
|
59 | 87 |
|
60 | | - if arg in ['--help', '-h']: |
61 | | - print('List of leagues and the codes: ') |
| 88 | + if arg in ["--help", "-h"]: |
| 89 | + print("List of leagues and the codes: ") |
62 | 90 | for sno, key in enumerate(LEAGUE_CODE.keys()):
|
63 | | - print('%2d. %-30s %s' % (sno+1, LEAGUE_CODE[key]['name'], key)) |
| 91 | + print("%2d. %-30s %s" % (sno+1, LEAGUE_CODE[key]["name"], key)) |
64 | 92 | sys.exit(0)
|
65 | 93 |
|
66 | 94 | league_id = LEAGUE_CODE.get(arg)
|
67 | 95 | if league_id is None:
|
68 | | - print('Invalid League Code!\nrun: python3 main.py --help | -h to get list of league codes') |
| 96 | + print( |
| 97 | + "Invalid League Code!\nrun: python3 main.py --help | -h to get list of league codes" |
| 98 | + ) |
69 | 99 |
|
70 | 100 | print_standings(league_id)
|
71 | 101 |
|
72 | 102 | # When no arguments are passed to the script
|
73 | 103 | except IndexError:
|
74 | | - print('Invalid Usage!\nrun: python3 main.py [league-code]\ |
75 | | - \nrun: python3 main.py --help | -h to get list of league codes') |
| 104 | + print( |
| 105 | + "Invalid Usage!\nrun: python3 main.py [league-code]\ |
| 106 | + \nrun: python3 main.py --help | -h to get list of league codes" |
| 107 | + ) |
0 commit comments