fplChatbot / fpl_client.py
Alejadro Sanchez-Giraldo
dream team
8b6f519
raw
history blame
4.33 kB
import requests
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class FPLClient:
def __init__(self):
self.base_url = "https://fantasy.premierleague.com/api"
self.data = self.get_static_data()
def player_position(self, position_id):
logging.info(f"Mapping position ID: {position_id}")
positions = {
1: 'Goalkeeper',
2: 'Defender',
3: 'Midfielder',
4: 'Forward'
}
return positions.get(position_id, 'Unknown')
def get_static_data(self):
url = f"{self.base_url}/bootstrap-static/"
logging.info(f"Fetching static data from {url}")
response = requests.get(url)
if response.status_code == 200:
logging.debug("Static data fetched successfully.")
return response.json()
else:
logging.error(f"Failed to fetch static data. Status code: {response.status_code}")
return None
def get_teams(self):
teams = self.data.get('teams', [])
logging.info(f"Fetching team names. Total teams: {len(teams)}")
return [team['name'] for team in teams]
def get_player_info(self, player_name):
logging.info(f"Searching for player: {player_name}")
players = self.data['elements']
player = next((p for p in players if p['web_name'].lower() == player_name.lower()), None)
if player:
logging.debug(f"Player found: {player['web_name']}")
else:
logging.warning(f"Player not found: {player_name}")
return player
def get_team_info(self, team_id):
logging.info(f"Fetching team info for team ID: {team_id}")
teams = self.data['teams']
team = next((t for t in teams if t['id'] == team_id), None)
if team:
logging.debug(f"Team found: {team['name']}")
else:
logging.warning(f"Team not found for ID: {team_id}")
return team
def get_player_stats(self, player_name):
logging.info(f"Fetching stats for player: {player_name}")
player = self.get_player_info(player_name)
if player:
logging.info(f"Player position ID: {player['element_type']}")
player_pos = self.player_position(player['element_type'])
team_name = self.get_team_info(player['team'])['name']
news = player['news']
if not news:
news = "No news available"
stats = {
"Name": player['web_name'],
"Team": team_name,
"Points per game avarage": player['points_per_game'],
"Price": player['now_cost'] / 10,
"Position": player_pos,
"News": news,
"Selected By": player['selected_by_percent'] + "%",
}
logging.debug(f"Stats for player {player_name}: {stats}")
return stats
logging.warning(f"Stats not found for player: {player_name}")
return "Player not found"
def get_team_players(self, team_name):
logging.info(f"Fetching players for team: {team_name}")
teams = self.data['teams']
team = next((t for t in teams if t['name'].lower() == team_name.lower()), None)
if team:
players = [p['web_name'] for p in self.data['elements'] if p['team'] == team['id']]
logging.debug(f"Players found for team {team_name}: {players}")
return players
logging.warning(f"Team not found: {team_name}")
return "Team not found"
def list_injuries(self):
logging.info("Fetching injuries list")
players = self.data['elements']
injuries = [(p['web_name'], p['news']) for p in players if p['news'] and 'injury' in p['news'].lower()]
logging.debug(f"Injured players: {injuries}")
return injuries
# get list of Dream team by FPL by filtering Elements that have in_dreamteam: true
def dream_team(self):
logging.info("Fetching Dream Team")
players = self.data['elements']
dream_team = [p['web_name'] for p in players if p['in_dreamteam']== True]
logging.info(f"Dream Team: {dream_team}")
return dream_team