-
Notifications
You must be signed in to change notification settings - Fork 0
/
competitions.py
147 lines (122 loc) · 4.76 KB
/
competitions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import argparse
import sys
import json
import os
import requests
URL = "https://api.football-data.org/v4/competitions/{}/matches?status=FINISHED&season={}"
def update_fixtures(competition, season, token, path):
endpoint = URL.format(competition, season)
headers = {"X-Auth-Token": token}
response = requests.get(endpoint, headers=headers)
if response.status_code == 200:
json_data = response.json()
simplified_data = {
"id": competition,
"name": json_data["competition"]["name"],
"season": season,
"teams": [],
"fixtures": []
}
unique_teams = set()
for match in json_data["matches"]:
home_team = match["homeTeam"]
away_team = match["awayTeam"]
if home_team["id"] not in unique_teams:
unique_teams.add(home_team["id"])
home_mapping = {
"id": home_team["id"],
"shortName": home_team["shortName"],
"tla": home_team["tla"]
}
simplified_data["teams"].append(home_mapping)
if away_team["id"] not in unique_teams:
unique_teams.add(away_team["id"])
away_team = {
"id": away_team["id"],
"shortName": away_team["shortName"],
"tla": away_team["tla"]
}
simplified_data["teams"].append(away_team)
simplified_match = {
"awayScore": match["score"]["fullTime"]["away"],
"awayTeam": away_team["id"],
"homeScore": match["score"]["fullTime"]["home"],
"homeTeam": home_team["id"],
}
simplified_data["fixtures"].append(simplified_match)
filename = f"{competition}-{season}.json"
file_path = os.path.join(path, filename)
with open(file_path, "w", encoding="UTF-8") as file:
json.dump(simplified_data, file)
return True
else:
return False
def generate_index_file(path):
competitions = []
seasons = {}
unique_competitions = set()
for file_name in os.listdir(path):
if file_name.endswith(".json"):
with open(os.path.join(path, file_name), "r", encoding="UTF-8") as file:
data = json.load(file)
competition_id = data["id"]
competition_name = data["name"]
competition_season = data["season"]
if competition_id not in unique_competitions:
unique_competitions.add(competition_id)
if competition_id not in seasons:
seasons[competition_id] = []
competition = {
"value": competition_id,
"name": competition_name
}
competitions.append(competition)
seasons[competition_id].append(competition_season)
index_file_path = os.path.join(path, "index.ts")
with open(index_file_path, "w", encoding="UTF-8") as index_file:
index_file.write('import type { Option } from "../types";\n\n')
index_file.write('export const competitions: Option<number>[] = ')
json.dump(competitions, index_file, indent=2)
index_file.write(';\n\n')
index_file.write('export const seasons: Record<number, number[]> = ')
json.dump(seasons, index_file, indent=2)
index_file.write(';\n')
return True
parser = argparse.ArgumentParser()
parser.add_argument("command", choices=["update", "help", "index"], help="Command to execute")
parser.add_argument("-c", "--competition", type=int)
parser.add_argument("-s", "--season", type=int)
parser.add_argument("-t", "--token", type=str, default=os.getenv("FOOTBALL_DATA_TOKEN"))
parser.add_argument("-p", "--path", type=str, default="")
args = parser.parse_args()
if args.command == "update":
if args.competition is None or args.season is None or args.path is None:
print("Error: Missing required arguments.")
parser.print_help()
sys.exit(1)
SUCCESS = update_fixtures(args.competition, args.season, args.token, args.path)
if SUCCESS:
print("Success")
sys.exit(0)
else:
print("Failure")
sys.exit(1)
elif args.command == "index":
if args.path is None:
print("Error: Missing required argument.")
parser.print_help()
sys.exit(1)
SUCCESS = generate_index_file(args.path)
if SUCCESS:
print("Success")
sys.exit(0)
else:
print("Failure")
sys.exit(1)
elif args.command == "help":
parser.print_help()
sys.exit(0)
else:
print("Error: Invalid command.")
parser.print_help()
sys.exit(1)