Created
January 6, 2020 11:52
-
-
Save tunnelWithAC/5bcc4375d8247e107ed169b5697061ad to your computer and use it in GitHub Desktop.
Fetch Fantasy Premier League Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import os | |
import pandas as pd | |
import requests | |
from datetime import datetime | |
import json | |
def get(url): | |
response = requests.get(url) | |
return json.loads(response.content) | |
response = get('https://fantasy.premierleague.com/api/bootstrap-static/') | |
response.keys() | |
players = response['elements'] | |
teams = response['teams'] | |
events = response['events'] | |
players_df = pd.DataFrame(players) | |
teams_df = pd.DataFrame(teams) | |
events_df = pd.DataFrame(events) | |
# Some basic cleaning | |
events_df['deadline_time'] = pd.to_datetime(events_df['deadline_time']) | |
events_df['deadline_time'] = events_df['deadline_time'].dt.tz_localize(None) | |
#### Getting player summary data | |
def get_player_summary(player_id): | |
url = 'https://fantasy.premierleague.com/api/element-summary/' + str(player_id) + '/' | |
response = get(url) | |
fixtures = response['fixtures'] | |
history = response['history'] | |
history_past = response['history_past'] | |
return [fixtures, history, history_past] | |
#### Get my fantasy team lineup | |
now = datetime.now() | |
my_team_id = '3588868' | |
current_gw = events_df[events_df['deadline_time'] < now]['id'].max() | |
my_team_url = 'https://fantasy.premierleague.com/api/entry/{}/event/{}/picks/'.format(my_team_id,str(current_gw)) | |
my_team = get(my_team_url)['picks'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment