def get_gw_transfer_suggestions(fpl_team_id=None): # gets the transfer suggestions for the latest optimization run, # regardless of fpl_team_id rows = get_transfer_suggestions(session, TransferSuggestion) if fpl_team_id and fpl_team_id != rows[0].fpl_team_id: raise Exception( f"Team ID passed is {fpl_team_id}, but transfer suggestions are for \ team ID {rows[0].fpl_team_id}. We recommend re-running optimization." ) else: fpl_team_id = rows[0].fpl_team_id current_gw, chip = rows[0].gameweek, rows[0].chip_played players_out, players_in = [], [] for row in rows: if row.gameweek == current_gw: if row.in_or_out < 0: players_out.append(row.player_id) else: players_in.append(row.player_id) return ([players_out, players_in], fpl_team_id, current_gw, chip)
def build_init_priced_transfers(fetcher, fpl_team_id=None): """ Before gameweek 1, there won't be any 'sell' transfer suggestions in the db. We can instead query the API for our current 'picks' (requires login). """ if not fpl_team_id: if (not fetcher.FPL_TEAM_ID) or fetcher.FPL_TEAM_ID == "MISSING_ID": fpl_team_id = int(input("Please enter FPL team ID: ")) else: fpl_team_id = fetcher.FPL_TEAM_ID current_squad = fetcher.get_current_picks(fpl_team_id) transfers_out = [ {"element_out": el["element"], "selling_price": el["selling_price"]} for el in current_squad ] transfer_in_suggestions = get_transfer_suggestions(dbsession) if len(transfers_out) != len(transfer_in_suggestions): raise RuntimeError( "Number of transfers in and out don't match: {} {}".format( len(transfer_in_suggestions), len(transfers_out) ) ) transfers_in = [] for t in transfer_in_suggestions: api_id = get_player(t.player_id).fpl_api_id price = fetcher.get_player_summary_data()[api_id]["now_cost"] transfers_in.append({"element_in": api_id, "purchase_price": price}) # remove duplicates - can't add a player we already have transfers_in, transfers_out = remove_duplicates(transfers_in, transfers_out) # re-order both lists so they go DEF, FWD, GK, MID transfers_in = sort_by_position(transfers_in) transfers_out = sort_by_position(transfers_out) transfer_list = [ {**transfers_in[i], **transfers_out[i]} for i in range(len(transfers_in)) ] return transfer_list