65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
import sqlite3
|
|
import requests
|
|
|
|
def create_standings_table(conn):
|
|
cursor = conn.cursor()
|
|
cursor.execute("""
|
|
CREATE TABLE IF NOT EXISTS standings (
|
|
team_common_name TEXT,
|
|
league_sequence INTEGER,
|
|
league_l10_sequence INTEGER
|
|
)
|
|
""")
|
|
conn.commit()
|
|
|
|
def truncate_standings_table(conn):
|
|
cursor = conn.cursor()
|
|
cursor.execute("DELETE FROM standings")
|
|
conn.commit()
|
|
|
|
def insert_standings_info(conn, standings_info):
|
|
cursor = conn.cursor()
|
|
for team in standings_info:
|
|
cursor.execute("""
|
|
INSERT INTO standings (team_common_name, league_sequence, league_l10_sequence)
|
|
VALUES (?, ?, ?)
|
|
""", (team["team_common_name"], team["league_sequence"], team["league_l10_sequence"]))
|
|
conn.commit()
|
|
|
|
def extract_standings_info():
|
|
url = "https://api-web.nhle.com/v1/standings/now"
|
|
response = requests.get(url)
|
|
if response.status_code == 200:
|
|
standings_data = response.json()
|
|
standings_info = []
|
|
for team in standings_data.get("standings", []):
|
|
team_info = {
|
|
"team_common_name": team["teamCommonName"]["default"],
|
|
"league_sequence": team["leagueSequence"],
|
|
"league_l10_sequence": team["leagueL10Sequence"]
|
|
}
|
|
standings_info.append(team_info)
|
|
return standings_info
|
|
else:
|
|
print("Error:", response.status_code)
|
|
return None
|
|
|
|
# Connect to SQLite database
|
|
conn = sqlite3.connect("nhl_standings.db")
|
|
|
|
# Create standings table if it doesn't exist
|
|
create_standings_table(conn)
|
|
|
|
# Truncate standings table before inserting new data
|
|
truncate_standings_table(conn)
|
|
|
|
# Extract standings info
|
|
standings_info = extract_standings_info()
|
|
|
|
# Insert standings info into the database
|
|
if standings_info:
|
|
insert_standings_info(conn, standings_info)
|
|
|
|
# Close database connection
|
|
conn.close()
|