Skip to content

Instantly share code, notes, and snippets.

@ConorAspell
Last active December 12, 2021 01:10
Show Gist options
  • Save ConorAspell/f284381bb142623d89f9aa34c2a7b15d to your computer and use it in GitHub Desktop.
Save ConorAspell/f284381bb142623d89f9aa34c2a7b15d to your computer and use it in GitHub Desktop.
import requests
import json
import pandas as pd
import csv
from datetime import datetime, timedelta
import boto3
s3_client = boto3.client('s3')
def update_team(email, password, id):
players_df, fixtures_df, gameweek=get_data()
def get_data():
today = datetime.now()
key = "odds" +today.strftime("%d-%m-%Y") +".csv"
bucket_name = os.environ.get("BUCKET")
resp = s3_client.get_object(Bucket=bucket_name, Key=key)
bet_df = pd.read_csv(resp['Body'], sep=',')
players = get('https://fantasy.premierleague.com/api/bootstrap-static/')
players_df = pd.DataFrame(players['elements'])
teams_df = pd.DataFrame(players['teams'])
fixtures_df = pd.DataFrame(players['events'])
today = datetime.now().timestamp()
fixtures_df = fixtures_df.loc[fixtures_df.deadline_time_epoch>today]
gameweek = fixtures_df.iloc[0].id
players_df = players_df[columns]
players_df.chance_of_playing_next_round = players_df.chance_of_playing_next_round.fillna(100.0)
players_df.chance_of_playing_this_round = players_df.chance_of_playing_this_round.fillna(100.0)
fixtures = get('https://fantasy.premierleague.com/api/fixtures/?event='+str(gameweek))
fixtures_df = pd.DataFrame(fixtures)
fixtures_df['home_chance'] = 100/bet_df['home_odds']
fixtures_df['away_chance'] = 100/bet_df['away_odds']
teams=dict(zip(teams_df.id, teams_df.name))
players_df['team_name'] = players_df['team'].map(teams)
fixtures_df['team_a_name'] = fixtures_df['team_a'].map(teams)
fixtures_df['team_h_name'] = fixtures_df['team_h'].map(teams)
fixtures_df=fixtures_df.drop(columns=['id'])
a_players = pd.merge(players_df, fixtures_df, how="inner", left_on=["team"], right_on=["team_a"])
h_players = pd.merge(players_df, fixtures_df, how="inner", left_on=["team"], right_on=["team_h"])
a_players['diff'] = a_players['away_chance'] - a_players['home_chance']
h_players['diff'] = h_players['home_chance'] - h_players['away_chance']
players_df = a_players.append(h_players)
return players_df, fixtures_df, gameweek
def get(url):
response = requests.get(url)
return json.loads(response.content)
def lambda_handler(event, context):
email = "your_email"
password = "your_password"
user_id = "your_id"
update_team(email, password,user_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment