Last active
December 11, 2023 15:18
-
-
Save ConorAspell/c444e6c65326dabb5f6b4c25f5a455d6 to your computer and use it in GitHub Desktop.
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
import pandas as pd | |
from fpl_utils import fpl_utils | |
import os | |
def lambda_handler(event, context): | |
try: | |
pl_profile = event['pl_profile'] | |
cookie = f"pl_profile={pl_profile};" | |
calc_player_out(event['user_id'], cookie) | |
return event | |
except Exception as e: | |
sns_topic = fpl_utils.get_parameter('failure_email_sns_key') | |
error_message = f"Error in Lambda function getPlayerDetails: {str(e)}" | |
fpl_utils.send_sns_notification(error_message, sns_topic) | |
raise e # Re-raise the exception after sending the notification | |
def calc_player_out(user_id, cookie): | |
team = fpl_utils.get_team_auth(user_id, cookie) | |
gameweek = fpl_utils.get_gameweek() | |
bucket_name = fpl_utils.get_parameter('fpl_bucket_name') | |
players_df = fpl_utils.get_csv_df(bucket_name, f'players-gameweek-{gameweek}.csv') | |
players = [x['element'] for x in team['picks']] | |
my_original_team = players_df.loc[players_df.id.isin(players)] | |
my_original_team = calc_out_weight(my_original_team) | |
player_out = my_original_team.sample(1, weights=my_original_team.out_weight) | |
file_name = f'gameweek-{gameweek}/player_out.json' | |
fpl_utils.upload_json_to_s3(bucket_name, file_name, player_out.to_dict(orient='records')) | |
def calc_out_weight(players): | |
players_copy = players.copy() | |
players_copy['out_weight'] = 100 | |
players_copy['out_weight'] -= players_copy['diff'] | |
players_copy['out_weight'] -= players_copy['form'].astype("float") * 10 | |
players_copy.loc[players_copy['element_type'] == 1, 'out_weight'] -= 10 | |
players_copy.loc[players_copy['out_weight'] < 0, 'out_weight'] = 0 | |
return players_copy.sort_values('out_weight', ascending=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment