示例#1
0
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
示例#2
0
def update_transactions(season, fpl_team_id, dbsession):
    """
    Ensure that the transactions table in the database is up-to-date.
    """

    if NEXT_GAMEWEEK != 1:
        print("Checking team")
        n_transfers_api = len(fetcher.get_fpl_transfer_data(fpl_team_id))
        n_transactions_db = count_transactions(season, fpl_team_id, dbsession)
        # DB has 2 rows per transfer, and rows for the 15 players selected in the
        # initial squad which are not returned by the transfers API
        n_transfers_db = (n_transactions_db - 15) / 2
        if n_transfers_db != n_transfers_api:
            update_squad(
                season=season,
                fpl_team_id=fpl_team_id,
                dbsession=dbsession,
                verbose=True,
            )
        else:
            print("Team is up-to-date")
    else:
        print("No transactions as season hasn't started")
    return True
示例#3
0
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,
            )
import json

from airsenal.framework.season import CURRENT_SEASON
from airsenal.framework.utils import fetcher

sdata = fetcher.get_current_summary_data()
with open(f"../data/FPL_{CURRENT_SEASON}.json", "w") as f:
    json.dump(sdata, f)

fixtures = fetcher.get_fixture_data()
with open(f"../data/fixture_data_{CURRENT_SEASON}.json", "w") as f:
    json.dump(fixtures, f)

history = fetcher.get_fpl_team_history_data()
with open(f"../data/airsenal_history_{CURRENT_SEASON}.json", "w") as f:
    json.dump(history, f)

transfers = fetcher.get_fpl_transfer_data()
with open(f"../data/airsenal_transfer_{CURRENT_SEASON}.json", "w") as f:
    json.dump(transfers, f)

gws = [fetcher.get_fpl_team_data(gw) for gw in range(1, 39)]
with open(f"../data/airsenal_gw_{CURRENT_SEASON}.json", "w") as f:
    json.dump(gws, f)