-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
80 lines (69 loc) · 3.21 KB
/
functions.py
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
import requests
import pandas as pd
import time
import plotly.express as px
# URL pattern to get the weekly summary data for a player by their element ID
element_summary_url = "https://fantasy.premierleague.com/api/element-summary/{}/"
def get_player_name(player_id):
player_raw = pd.read_csv('players_raw.csv')
name = player_raw.loc[player_raw['id'] == player_id]['web_name'].to_string(index=False)
return name
def get_player_team(player_id):
player_raw = pd.read_csv('players_raw.csv')
team = player_raw.loc[player_raw['id'] == player_id]['team'].to_string(index=False)
return team
def get_player_position(player_id):
player_raw = pd.read_csv('players_raw.csv')
position = player_raw.loc[player_raw['id'] == player_id]['element_type'].to_string(index=False)
return position
# Function to get weekly data for a specific player by their element ID
def get_player_weekly_data(player_id):
response = requests.get(element_summary_url.format(player_id))
return response.json()
def extract_all_players_data(picks):
all_player_history = []
# Loop through each player
for player in picks:
player_id = player
name = get_player_name(player)
team = get_player_team(player)
position = get_player_position(player)
#print(f"Fetching data for player {name}")
# Get player weekly data
player_weekly_data = get_player_weekly_data(player_id)
# Loop through each gameweek history and create separate rows
for gameweek in player_weekly_data.get('history', []):
player_data = {
'id': player,
'name': name,
'team': team,
'position': position,
'total_points': gameweek['total_points'],
'round': gameweek['round'],
'minutes': gameweek['minutes'],
'goals_scored': gameweek['goals_scored'],
'assists': gameweek['assists'],
'clean_sheets': gameweek['clean_sheets'],
'goals_conceded': gameweek['goals_conceded'],
'own_goals': gameweek['own_goals'],
'penalties_saved': gameweek['penalties_saved'],
'penalties_missed': gameweek['penalties_missed'],
'yellow_cards': gameweek['yellow_cards'],
'red_cards': gameweek['red_cards'],
'saves': gameweek['saves'],
'bonus': gameweek['bonus'],
'bps': gameweek['bps'],
'influence': gameweek['influence'],
'creativity': gameweek['creativity'],
'threat': gameweek['threat'],
'ict_index': gameweek['ict_index'],
'starts': gameweek['starts'],
'expected_goals': gameweek['expected_goals'],
'expected_assists': gameweek['expected_assists'],
'expected_goal_involvements': gameweek['expected_goal_involvements'],
'expected_goals_conceded': gameweek['expected_goals_conceded'],
}
all_player_history.append(player_data)
# Pause between requests to avoid rate-limiting
time.sleep(1) # Adjust sleep time if necessary
return all_player_history