-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_schedule.py
66 lines (54 loc) · 2.21 KB
/
transform_schedule.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
import json
import requests
from datetime import datetime, timedelta
import os
import git
def transform_json(input_url, output_file):
response = requests.get(input_url)
response.raise_for_status()
data = response.json()
transformed_data = []
for i in range(len(data["round_number"])):
event = {
"name": data["event_name"][str(i)],
"countryName": data["country"][str(i)].replace(" ", ""),
"countryKey": None,
"roundNumber": data["round_number"][str(i)],
"start": None,
"end": None,
"gmt_offset": data["gmt_offset"][str(i)],
"sessions": [],
"over": False
}
for j in range(1, 6):
session_name = data[f'session{j}'][str(i)]
session_date = data[f'session{j}_date'][str(i)]
if session_name:
session_start = session_date
session_start_dt = datetime.fromisoformat(session_start)
session_end_dt = session_start_dt + timedelta(hours=1)
event["sessions"].append({
"sessionNumber": str(j),
"kind": session_name,
"start": session_start + 'Z',
"end": session_end_dt.isoformat() + 'Z'
})
event["start"] = min(session["start"] for session in event["sessions"])
event["end"] = max(session["end"] for session in event["sessions"])
transformed_data.append(event)
with open(output_file, 'w') as f:
json.dump(transformed_data, f, indent=4)
repo = git.Repo(os.getcwd())
repo.git.add(output_file)
repo.index.commit(f'Update {output_file} with transformed data')
origin = repo.remote(name='origin')
origin.push()
current_year = datetime.now().year
for year in range(2018, current_year+1):
input_url = f'https://raw.githubusercontent.com/theOehrly/f1schedule/refs/heads/master/schedule_{year}.json'
output_file = f'{year}.json'
response = requests.get(input_url)
if response.status_code == 200:
transform_json(input_url, output_file)
else:
print(f"{year} not avaliable")