File size: 4,325 Bytes
86e230c
 
 
23152a0
86e230c
 
 
 
 
 
 
 
23152a0
 
 
 
 
 
 
 
 
 
86e230c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23152a0
 
86e230c
2ffcbc1
 
 
86e230c
 
 
2ffcbc1
86e230c
23152a0
2ffcbc1
 
86e230c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
796b25f
 
 
 
 
 
 
8b6f519
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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