-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_nhl_data.py
More file actions
executable file
·57 lines (46 loc) · 1.43 KB
/
update_nhl_data.py
File metadata and controls
executable file
·57 lines (46 loc) · 1.43 KB
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
#!/usr/bin/env python3
import requests
import json
import os
import subprocess
from datetime import datetime
CURRENT_YEAR = datetime.now().year
NHL_API_URL = f"https://api-web.nhle.com/v1/playoff-bracket/{CURRENT_YEAR}"
OUTPUT_FILE = f"./playoffs/data/{CURRENT_YEAR}/api.json"
REPO_PATH = os.path.dirname(os.path.abspath(__file__))
def fetch_data():
try:
response = requests.get(NHL_API_URL)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
print(f"Error fetching data: {e}")
return None
def save_data(data):
with open(OUTPUT_FILE, "w") as file:
json.dump(data, file, indent=4)
def has_changes():
result = subprocess.run(
["git", "status", "--porcelain"],
cwd=REPO_PATH,
stdout=subprocess.PIPE,
text=True
)
return bool(result.stdout.strip())
def commit_and_push():
subprocess.run(["git", "add", OUTPUT_FILE], cwd=REPO_PATH)
subprocess.run(["git", "commit", "-m", "Update 2025.json with latest NHL data"], cwd=REPO_PATH)
subprocess.run(["git", "push"], cwd=REPO_PATH)
def main():
os.chdir(REPO_PATH)
data = fetch_data()
if not data:
return
save_data(data)
if has_changes():
print("Changes detected. Committing and pushing to GitHub...")
commit_and_push()
else:
print("No changes detected. Skipping commit.")
if __name__ == "__main__":
main()