Skip to content
Permalink
5e1a8b8eb5
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
121 lines (121 sloc) 4.91 KB
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import pandas as pd\n",
"\n",
"# Pre-fetch general information and cache it to avoid repeated API requests\n",
"general_info = requests.get(\"https://fantasy.premierleague.com/api/bootstrap-static/\").json()\n",
"teams_info = {team[\"id\"]: team[\"name\"] for team in general_info[\"teams\"]}\n",
"positions_info = {position[\"id\"]: position[\"singular_name_short\"] for position in general_info[\"element_types\"]}\n",
"\n",
"def get_team_name(team_id):\n",
" \"\"\"Retrieve the team name by team ID from cached data.\"\"\"\n",
" return teams_info.get(team_id)\n",
"\n",
"def get_player_position(element_type):\n",
" \"\"\"Retrieve the player's position shorthand from cached data based on element type.\"\"\"\n",
" return positions_info.get(element_type)\n",
"\n",
"def get_player_fixture_info(player_id):\n",
" \"\"\"\n",
" Fetch the first upcoming fixture information for a given player.\n",
" Utilizes cached team names for efficiency.\n",
" \"\"\"\n",
" player_info = requests.get(f\"https://fantasy.premierleague.com/api/element-summary/{player_id}/\").json()\n",
" fixture = player_info[\"fixtures\"][0] # Assuming we always want the first fixture\n",
" home_team = get_team_name(fixture[\"team_h\"])\n",
" away_team = get_team_name(fixture[\"team_a\"])\n",
" return home_team, away_team, fixture[\"kickoff_time\"], fixture[\"is_home\"]\n",
"\n",
"def get_gameweek_result(player_id, gameweek):\n",
" \"\"\"\n",
" Fetch and return a dictionary of the player's statistics for a specific gameweek.\n",
" \"\"\"\n",
" gameweek_results = requests.get(f\"https://fantasy.premierleague.com/api/element-summary/{player_id}/\").json()\n",
" return gameweek_results[\"history\"][gameweek - 1]\n",
"\n",
"# Load the gameweek fixture from a CSV file\n",
"gameweek = 26\n",
"gameweek_fixture = pd.read_csv(f\"C:\\\\Users\\\\prane\\\\Downloads\\\\FPL\\\\GW_PointsPredictor\\\\Default_data\\\\2023-24\\\\fixtures\\\\GW{gameweek}.csv\")\n",
"\n",
"def get_fixture(name):\n",
" \"\"\"\n",
" Retrieve fixture information for a given player name from the pre-loaded DataFrame.\n",
" \"\"\"\n",
" fixture_row = gameweek_fixture[gameweek_fixture[\"name\"] == name].iloc[0]\n",
" return fixture_row[\"home_team\"], fixture_row[\"away_team\"], fixture_row[\"kickoff_time\"], fixture_row[\"is_home\"]\n",
"\n",
"# Process and gather information for all players\n",
"all_players = []\n",
"for player_info in general_info[\"elements\"]:\n",
" try:\n",
" player_id = player_info[\"id\"]\n",
" print(player_id)\n",
" name = f\"{player_info['first_name']} {player_info['second_name']}\"\n",
" team_id = player_info[\"team\"]\n",
" cost = player_info[\"now_cost\"]\n",
" element_type = player_info[\"element_type\"]\n",
"\n",
" # Utilize cached functions to fetch team name and player position\n",
" my_team = get_team_name(team_id)\n",
" position = get_player_position(element_type)\n",
"\n",
" # Get gameweek result and fixture info using optimized functions\n",
" gameweek_result = get_gameweek_result(player_id, gameweek)\n",
" home_team, away_team, kickoff_time, is_home = get_fixture(name)\n",
"\n",
" # Update the gameweek result with additional player details\n",
" gameweek_result.update({\n",
" \"id\": player_id,\n",
" \"name\": name,\n",
" \"cost\": cost,\n",
" \"position\": position,\n",
" \"home_team\": home_team,\n",
" \"away_team\": away_team,\n",
" \"team_x\": my_team,\n",
" \"opponent_team\": get_team_name(gameweek_result[\"opponent_team\"])\n",
" })\n",
"\n",
" all_players.append(gameweek_result)\n",
" except IndexError: # Catch cases where specific data may be missing\n",
" continue\n",
"\n",
"# Create and display a DataFrame with all collected player data\n",
"df = pd.DataFrame(all_players)\n",
"print(df)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv(f\"C:\\\\Users\\\\prane\\\\Downloads\\\\FPL\\\\GW_PointsPredictor\\\\Default_data\\\\2023-24\\\\results\\\\GW{gameweek}.csv\")"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}