Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@
|
|
| 3 |
from fastapi import FastAPI, BackgroundTasks
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
import threading
|
|
|
|
| 6 |
from datetime import datetime, timezone
|
| 7 |
|
| 8 |
from bluesky_fetcher import BlueskyFetcher, load_handles_from_csv
|
|
@@ -42,10 +43,22 @@ def do_fetch():
|
|
| 42 |
is_fetching = False
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Auto-fetch on startup
|
| 46 |
thread = threading.Thread(target=do_fetch, daemon=True)
|
| 47 |
thread.start()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
@app.get("/api/status")
|
| 51 |
def get_status():
|
|
@@ -135,7 +148,32 @@ def get_player_info(player_name: str):
|
|
| 135 |
@app.get("/api/team/{team_name}")
|
| 136 |
def get_team_info(team_name: str):
|
| 137 |
players = get_team_players(team_name)[:15]
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
|
| 141 |
@app.get("/api/mentions/{player_name}")
|
|
|
|
| 3 |
from fastapi import FastAPI, BackgroundTasks
|
| 4 |
from fastapi.responses import HTMLResponse
|
| 5 |
import threading
|
| 6 |
+
import time
|
| 7 |
from datetime import datetime, timezone
|
| 8 |
|
| 9 |
from bluesky_fetcher import BlueskyFetcher, load_handles_from_csv
|
|
|
|
| 43 |
is_fetching = False
|
| 44 |
|
| 45 |
|
| 46 |
+
def auto_refresh_loop():
|
| 47 |
+
"""Background loop that refreshes data every 15 minutes"""
|
| 48 |
+
while True:
|
| 49 |
+
time.sleep(900) # 15 minutes
|
| 50 |
+
print("Auto-refresh triggered")
|
| 51 |
+
do_fetch()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
# Auto-fetch on startup
|
| 55 |
thread = threading.Thread(target=do_fetch, daemon=True)
|
| 56 |
thread.start()
|
| 57 |
|
| 58 |
+
# Start auto-refresh background loop
|
| 59 |
+
refresh_thread = threading.Thread(target=auto_refresh_loop, daemon=True)
|
| 60 |
+
refresh_thread.start()
|
| 61 |
+
|
| 62 |
|
| 63 |
@app.get("/api/status")
|
| 64 |
def get_status():
|
|
|
|
| 148 |
@app.get("/api/team/{team_name}")
|
| 149 |
def get_team_info(team_name: str):
|
| 150 |
players = get_team_players(team_name)[:15]
|
| 151 |
+
|
| 152 |
+
# Get stats for each time period
|
| 153 |
+
time_periods = [6, 12, 24, 48, 168]
|
| 154 |
+
period_stats = []
|
| 155 |
+
for hours in time_periods:
|
| 156 |
+
counts = database.get_team_mention_counts(hours=hours, limit=30)
|
| 157 |
+
rank = None
|
| 158 |
+
mentions = 0
|
| 159 |
+
for i, item in enumerate(counts, 1):
|
| 160 |
+
if item["team_name"] == team_name:
|
| 161 |
+
rank = i
|
| 162 |
+
mentions = item["mention_count"]
|
| 163 |
+
break
|
| 164 |
+
period_stats.append({
|
| 165 |
+
"hours": hours,
|
| 166 |
+
"label": "7d" if hours == 168 else f"{hours}h",
|
| 167 |
+
"mentions": mentions,
|
| 168 |
+
"rank": rank
|
| 169 |
+
})
|
| 170 |
+
|
| 171 |
+
return {
|
| 172 |
+
"team": team_name,
|
| 173 |
+
"team_abbrev": get_team_abbreviation(team_name),
|
| 174 |
+
"players": players,
|
| 175 |
+
"period_stats": period_stats
|
| 176 |
+
}
|
| 177 |
|
| 178 |
|
| 179 |
@app.get("/api/mentions/{player_name}")
|