def print_output( team_id, current_gw, priced_transfers, pre_bank, post_bank, points_cost="TODO" ): print("\n") header = f"Transfers to apply for fpl_team_id: {team_id} for gameweek: {current_gw}" line = "=" * len(header) print(f"{header} \n {line} \n") print(f"Bank Balance Before transfers is: £{pre_bank/10}") t = PrettyTable(["Status", "Name", "Price"]) for transfer in priced_transfers: t.add_row( [ "OUT", get_player_from_api_id(transfer["element_out"]), f"£{transfer['selling_price']/10}", ] ) t.add_row( [ "IN", get_player_from_api_id(transfer["element_in"]), f"£{transfer['purchase_price']/10}", ] ) print(t) print(f"Bank Balance After transfers is: £{post_bank/10}") # print(f"Points Cost of Transfers: {points_cost}") print("\n")
def update_squad( season=CURRENT_SEASON, tag="AIrsenal" + CURRENT_SEASON, dbsession=session, verbose=True, ): """ Fill the Transactions table in the DB with all the transfers in gameweeks after 1, using the transfers API endpoint which has the correct buy and sell prices. """ transfers = fetcher.get_fpl_transfer_data() for transfer in transfers: gameweek = transfer["event"] api_pid_out = transfer["element_out"] pid_out = get_player_from_api_id(api_pid_out).player_id price_out = transfer["element_out_cost"] if verbose: print("Adding transaction: gameweek: {} removing player {} for {}". format(gameweek, pid_out, price_out)) free_hit = free_hit_used_in_gameweek(gameweek) add_transaction(pid_out, gameweek, -1, price_out, season, tag, free_hit, dbsession) api_pid_in = transfer["element_in"] pid_in = get_player_from_api_id(api_pid_in).player_id price_in = transfer["element_in_cost"] if verbose: print("Adding transaction: gameweek: {} adding player {} for {}". format(gameweek, pid_in, price_in)) add_transaction(pid_in, gameweek, 1, price_in, season, tag, free_hit, session) pass
def fill_ep(csv_filename, dbsession=session): """ fill the database with FPLs ep_next prediction, and also write output to a csv. """ if not os.path.exists(csv_filename): outfile = open(csv_filename, "w") outfile.write("player_id,gameweek,EP\n") else: outfile = open(csv_filename, "a") summary_data = fetcher.get_player_summary_data() gameweek = NEXT_GAMEWEEK for k, v in summary_data.items(): player = get_player_from_api_id(k) player_id = player.player_id outfile.write("{},{},{}\n".format(player_id, gameweek, v["ep_next"])) pp = PlayerPrediction() pp.player_id = player_id pp.gameweek = gameweek pp.predicted_points = v["ep_next"] pp.method = "EP" dbsession.add(pp) dbsession.commit() outfile.close()
def get_lineup_from_payload(lineup): """ inverse of build_lineup_payload. Returns a squad object from get_lineup lineup is a dictionary, with the entry "picks" being a list of dictionaries like: {"element":353,"position":1,"selling_price":55,"multiplier":1,"purchase_price":55,"is_captain":false,"is_vice_captain":false} """ s = Squad() for p in lineup["picks"]: player = get_player_from_api_id(p["element"]) s.add_player(player, check_budget=False) if s.is_complete(): return s else: raise RuntimeError("Squad incomplete")
def fill_attributes_table_from_api(season, gw_start=1, dbsession=session): """ use the FPL API to get player attributes info for the current season """ fetcher = FPLDataFetcher() next_gw = get_next_gameweek(season=season, dbsession=dbsession) # needed for selected by calculation from percentage below n_players = fetcher.get_current_summary_data()["total_players"] input_data = fetcher.get_player_summary_data() for player_api_id in input_data.keys(): # find the player in the player table player = get_player_from_api_id(player_api_id, dbsession=dbsession) if not player: print("ATTRIBUTES {} No player found with id {}".format( season, player_api_id)) continue print("ATTRIBUTES {} {}".format(season, player.name)) # First update the current gameweek using the summary data p_summary = input_data[player_api_id] position = positions[p_summary["element_type"]] pa = get_player_attributes(player.player_id, season=season, gameweek=next_gw, dbsession=dbsession) if pa: # found pre-existing attributes for this gameweek update = True else: # no attributes for this gameweek for this player yet pa = PlayerAttributes() update = False pa.player = player pa.player_id = player.player_id pa.season = season pa.gameweek = next_gw pa.price = int(p_summary["now_cost"]) pa.team = get_team_name(p_summary["team"], season=season, dbsession=dbsession) pa.position = positions[p_summary["element_type"]] pa.selected = int( float(p_summary["selected_by_percent"]) * n_players / 100) pa.transfers_in = int(p_summary["transfers_in_event"]) pa.transfers_out = int(p_summary["transfers_out_event"]) pa.transfers_balance = pa.transfers_in - pa.transfers_out pa.chance_of_playing_next_round = p_summary[ "chance_of_playing_next_round"] pa.news = p_summary["news"] if (pa.chance_of_playing_next_round is not None and pa.chance_of_playing_next_round <= 50): pa.return_gameweek = get_return_gameweek_from_news( p_summary["news"], season=season, dbsession=dbsession, ) if not update: # only need to add to the dbsession for new entries, if we're doing # an update the final dbsession.commit() is enough dbsession.add(pa) # now get data for previous gameweeks if next_gw > 1: player_data = fetcher.get_gameweek_data_for_player(player_api_id) if not player_data: print("Failed to get data for", player.name) continue for gameweek, data in player_data.items(): if gameweek < gw_start: continue for result in data: # check whether there are pre-existing attributes to update pa = get_player_attributes( player.player_id, season=season, gameweek=gameweek, dbsession=dbsession, ) if pa: update = True else: pa = PlayerAttributes() update = False # determine the team the player played for in this fixture opponent_id = result["opponent_team"] was_home = result["was_home"] kickoff_time = result["kickoff_time"] team = get_player_team_from_fixture( gameweek, opponent_id, was_home, kickoff_time, season=season, dbsession=dbsession, ) pa.player = player pa.player_id = player.player_id pa.season = season pa.gameweek = gameweek pa.price = int(result["value"]) pa.team = team pa.position = position # does not change during season pa.transfers_balance = int(result["transfers_balance"]) pa.selected = int(result["selected"]) pa.transfers_in = int(result["transfers_in"]) pa.transfers_out = int(result["transfers_out"]) if not update: # don't need to add to dbsession if updating pre-existing row dbsession.add(pa) break # done this gameweek now
def fill_playerscores_from_api(season, gw_start=1, gw_end=NEXT_GAMEWEEK, dbsession=session): fetcher = FPLDataFetcher() input_data = fetcher.get_player_summary_data() for player_api_id in input_data.keys(): player = get_player_from_api_id(player_api_id, dbsession=dbsession) if not player: # If no player found with this API ID something has gone wrong with the # Player table, e.g. clashes between players with the same name print(f"ERROR! No player with API id {player_api_id}. Skipped.") continue print("SCORES {} {}".format(season, player)) player_data = fetcher.get_gameweek_data_for_player(player_api_id) # now loop through all the matches that player played in for gameweek, results in player_data.items(): if gameweek not in range(gw_start, gw_end): continue for result in results: # try to find the match in the match table opponent = get_team_name(result["opponent_team"]) played_for, fixture = get_player_team_from_fixture( gameweek, opponent, player_at_home=result["was_home"], kickoff_time=result["kickoff_time"], season=season, dbsession=dbsession, return_fixture=True, ) if not fixture or not played_for or not fixture.result: print( " Couldn't find match result for {} in gw {}".format( player, gameweek)) continue ps = get_player_scores(fixture=fixture, player=player, dbsession=dbsession) if ps is None: ps = PlayerScore() add = True else: add = False ps.player_team = played_for ps.opponent = opponent ps.goals = result["goals_scored"] ps.assists = result["assists"] ps.bonus = result["bonus"] ps.points = result["total_points"] ps.conceded = result["goals_conceded"] ps.minutes = result["minutes"] ps.player = player ps.fixture = fixture ps.result = fixture.result # extended features # get features excluding the core ones already populated above extended_feats = [ col for col in ps.__table__.columns.keys() if col not in [ "id", "player_team", "opponent", "goals", "assists", "bonus", "points", "conceded", "minutes", "player_id", "result_id", "fixture_id", ] ] for feat in extended_feats: try: ps.__setattr__(feat, result[feat]) except KeyError: pass if add: dbsession.add(ps) print(" got {} points vs {} in gameweek {}".format( result["total_points"], opponent, gameweek)) dbsession.commit()
def _get_position(api_id): return get_player_from_api_id(api_id).position(CURRENT_SEASON)
def update_squad( season=CURRENT_SEASON, tag="AIrsenal" + CURRENT_SEASON, fpl_team_id=None, dbsession=session, verbose=True, ): """ Fill the Transactions table in the DB with all the transfers in gameweeks after 1, using the transfers API endpoint which has the correct buy and sell prices. """ if not fpl_team_id: fpl_team_id = fetcher.FPL_TEAM_ID print("Updating db with squad with fpl_team_id={}".format(fpl_team_id)) # do we already have the initial squad for this fpl_team_id? existing_transfers = (dbsession.query(Transaction).filter_by( fpl_team_id=fpl_team_id).filter_by(season=season).all()) if len(existing_transfers) == 0: # need to put the initial squad into the db fill_initial_squad(season=season, tag=tag, fpl_team_id=fpl_team_id, dbsession=dbsession) # now update with transfers transfers = fetcher.get_fpl_transfer_data(fpl_team_id) for transfer in transfers: gameweek = transfer["event"] api_pid_out = transfer["element_out"] pid_out = get_player_from_api_id(api_pid_out).player_id price_out = transfer["element_out_cost"] api_pid_in = transfer["element_in"] pid_in = get_player_from_api_id(api_pid_in).player_id price_in = transfer["element_in_cost"] time = transfer["time"] if not transaction_exists( fpl_team_id, gameweek, season, time, pid_out, price_out, pid_in, price_in, dbsession=dbsession, ): if verbose: print( "Adding transaction: gameweek: {} removing player {} for {}" .format(gameweek, pid_out, price_out)) free_hit = free_hit_used_in_gameweek(gameweek) add_transaction( pid_out, gameweek, -1, price_out, season, tag, free_hit, fpl_team_id, time, dbsession, ) if verbose: print( "Adding transaction: gameweek: {} adding player {} for {}". format(gameweek, pid_in, price_in)) add_transaction( pid_in, gameweek, 1, price_in, season, tag, free_hit, fpl_team_id, time, dbsession, )
def fill_playerscores_from_api(season, gw_start=1, gw_end=NEXT_GAMEWEEK, dbsession=session): fetcher = FPLDataFetcher() input_data = fetcher.get_player_summary_data() for player_api_id in input_data.keys(): # find the player in the player table. If they're not # there, then we don't care (probably not a current player). player = get_player_from_api_id(player_api_id, dbsession=dbsession) if not player: print("No player with API id {}".format(player_api_id)) player_id = player.player_id print("SCORES {} {}".format(season, player.name)) player_data = fetcher.get_gameweek_data_for_player(player_api_id) # now loop through all the matches that player played in for gameweek, results in player_data.items(): if gameweek not in range(gw_start, gw_end): continue for result in results: # try to find the match in the match table opponent = get_team_name(result["opponent_team"]) played_for, fixture = get_player_team_from_fixture( gameweek, opponent, player_at_home=result["was_home"], kickoff_time=result["kickoff_time"], season=season, dbsession=dbsession, return_fixture=True, ) if not fixture or not played_for: print(" Couldn't find match for {} in gw {}".format( player.name, gameweek)) continue ps = PlayerScore() ps.player_team = played_for ps.opponent = opponent ps.goals = result["goals_scored"] ps.assists = result["assists"] ps.bonus = result["bonus"] ps.points = result["total_points"] ps.conceded = result["goals_conceded"] ps.minutes = result["minutes"] ps.player = player ps.fixture = fixture ps.result = fixture.result # extended features # get features excluding the core ones already populated above extended_feats = [ col for col in ps.__table__.columns.keys() if col not in [ "id", "player_team", "opponent", "goals", "assists", "bonus", "points", "conceded", "minutes", "player_id", "result_id", "fixture_id", ] ] for feat in extended_feats: try: ps.__setattr__(feat, result[feat]) except KeyError: pass dbsession.add(ps) print(" got {} points vs {} in gameweek {}".format( result["total_points"], opponent, gameweek))