RosterHash/services/sleeper_api.py
efigueroa 10f8e07ed4 Add player injury status display feature
- Added injury status processing in app.py to check multiple Sleeper API fields
- Filter out 'Active' status to only display actual injury conditions
- Added injury status display in dashboard template with red styling
- Added CSS styling for injury status badges with red color and border
- Only non-active injury statuses (IR, SUS, PUP, DNI, Questionable, OUT) will be shown

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-06 22:57:42 -07:00

75 lines
2.7 KiB
Python

import requests
from datetime import datetime
class SleeperAPI:
BASE_URL = 'https://api.sleeper.app/v1'
def __init__(self):
self.session = requests.Session()
self.players_cache = None # Cache player data
self.players_updated = None # Track last update time
def get_user(self, username):
"""Get user info by username"""
try:
response = self.session.get(f"{self.BASE_URL}/user/{username}")
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException:
return None
def get_user_by_id(self, user_id):
"""Get user info by user_id"""
try:
response = self.session.get(f"{self.BASE_URL}/user/{user_id}")
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException:
return None
def get_nfl_state(self):
"""Get current NFL state (week, season, etc)"""
response = self.session.get(f"{self.BASE_URL}/state/nfl")
response.raise_for_status()
return response.json()
def get_user_leagues(self, user_id, season):
"""Get all leagues for a user in a season"""
response = self.session.get(f"{self.BASE_URL}/user/{user_id}/leagues/nfl/{season}")
response.raise_for_status()
return response.json()
def get_matchups(self, league_id, week):
"""Get matchups for a league and week"""
response = self.session.get(f"{self.BASE_URL}/league/{league_id}/matchups/{week}")
response.raise_for_status()
return response.json()
def get_rosters(self, league_id):
"""Get rosters for a league"""
response = self.session.get(f"{self.BASE_URL}/league/{league_id}/rosters")
response.raise_for_status()
return response.json()
def get_players(self, force_refresh=False):
"""Get all NFL players (cached for 24 hours)"""
now = datetime.now()
# Check if cache is stale or force refresh requested
if (not self.players_cache or not self.players_updated or
(now - self.players_updated).total_seconds() > 86400 or force_refresh):
response = self.session.get(f"{self.BASE_URL}/players/nfl")
response.raise_for_status()
self.players_cache = response.json()
self.players_updated = now
return self.players_cache
def get_player_info(self, player_id):
"""Get specific player info"""
players = self.get_players()
player_info = players.get(str(player_id))
return player_info