app.py: add update_nhl_standings function and set to run every 5 min
This commit is contained in:
12
app.py
12
app.py
@@ -7,6 +7,7 @@ import threading
|
|||||||
import time
|
import time
|
||||||
import schedule
|
import schedule
|
||||||
import json
|
import json
|
||||||
|
from update_nhl_standings_db import update_nhl_standings
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@@ -39,8 +40,9 @@ def store_scoreboard_data():
|
|||||||
global scoreboard_data
|
global scoreboard_data
|
||||||
scoreboard_data = get_nhle_scoreboard()
|
scoreboard_data = get_nhle_scoreboard()
|
||||||
|
|
||||||
# Schedule the task to run every 10 seconds
|
# Schedule tasks
|
||||||
def schedule_task():
|
def schedule_tasks():
|
||||||
|
schedule.every(300).seconds.do(update_nhl_standings)
|
||||||
schedule.every(10).seconds.do(store_scoreboard_data)
|
schedule.every(10).seconds.do(store_scoreboard_data)
|
||||||
while True:
|
while True:
|
||||||
schedule.run_pending()
|
schedule.run_pending()
|
||||||
@@ -157,9 +159,6 @@ def get_game_outcome(game_state, game):
|
|||||||
|
|
||||||
return last_period_type
|
return last_period_type
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_game_priority(game):
|
def calculate_game_priority(game):
|
||||||
if game["gameState"] in ["FINAL", "OFF", "PRE", "FUT"] or game["clock"]["inIntermission"]:
|
if game["gameState"] in ["FINAL", "OFF", "PRE", "FUT"] or game["clock"]["inIntermission"]:
|
||||||
return 0
|
return 0
|
||||||
@@ -250,5 +249,6 @@ def get_scoreboard():
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
store_scoreboard_data()
|
store_scoreboard_data()
|
||||||
threading.Thread(target=schedule_task).start()
|
update_nhl_standings()
|
||||||
|
threading.Thread(target=schedule_tasks).start()
|
||||||
serve(app, host="0.0.0.0", port=2897)
|
serve(app, host="0.0.0.0", port=2897)
|
||||||
@@ -44,21 +44,22 @@ def extract_standings_info():
|
|||||||
print("Error:", response.status_code)
|
print("Error:", response.status_code)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Connect to SQLite database
|
def update_nhl_standings():
|
||||||
conn = sqlite3.connect("nhl_standings.db")
|
# Connect to SQLite database
|
||||||
|
conn = sqlite3.connect("nhl_standings.db")
|
||||||
|
|
||||||
# Create standings table if it doesn't exist
|
# Create standings table if it doesn't exist
|
||||||
create_standings_table(conn)
|
create_standings_table(conn)
|
||||||
|
|
||||||
# Truncate standings table before inserting new data
|
# Truncate standings table before inserting new data
|
||||||
truncate_standings_table(conn)
|
truncate_standings_table(conn)
|
||||||
|
|
||||||
# Extract standings info
|
# Extract standings info
|
||||||
standings_info = extract_standings_info()
|
standings_info = extract_standings_info()
|
||||||
|
|
||||||
# Insert standings info into the database
|
# Insert standings info into the database
|
||||||
if standings_info:
|
if standings_info:
|
||||||
insert_standings_info(conn, standings_info)
|
insert_standings_info(conn, standings_info)
|
||||||
|
|
||||||
# Close database connection
|
# Close database connection
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user