예제 #1
0
def update_results(season, dbsession):
    """
    If the last gameweek in the db is earlier than the last finished gameweek,
    update the 'results', 'playerscore', and (optionally) 'attributes' tables.
    """
    last_in_db = get_last_complete_gameweek_in_db(season, dbsession=dbsession)
    if not last_in_db:
        # no results in database for this season yet
        last_in_db = 0
    last_finished = get_last_finished_gameweek()

    if NEXT_GAMEWEEK == 1:
        print("Skipping team and result updates - season hasn't started.")
    elif last_finished > last_in_db:
        # need to update
        print("Updating results table ...")
        fill_results_from_api(
            gw_start=last_in_db + 1,
            gw_end=NEXT_GAMEWEEK,
            season=season,
            dbsession=dbsession,
        )
        print("Updating playerscores table ...")
        fill_playerscores_from_api(
            season=season,
            gw_start=last_in_db + 1,
            gw_end=NEXT_GAMEWEEK,
            dbsession=dbsession,
        )
    else:
        print("Matches and player-scores already up-to-date")
    return True
def main():

    parser = argparse.ArgumentParser(
        description="fill db tables with recent scores and transactions")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--tag",
                        help="identifying tag",
                        default="AIrsenal" + CURRENT_SEASON)
    parser.add_argument("--noattr",
                        help="don't update player attributes",
                        action="store_true")

    args = parser.parse_args()

    season = args.season
    tag = args.tag
    do_attributes = not args.noattr

    with session_scope() as session:

        last_in_db = get_last_gameweek_in_db(season, session)
        if not last_in_db:
            # no results in database for this season yet
            last_in_db = 0
        last_finished = get_last_finished_gameweek()

        # TODO update players table
        # TODO update fixtures table (e.g. in case of rescheduling)?

        if do_attributes:
            print("Updating attributes")
            fill_attributes_table_from_api(season,
                                           session,
                                           gw_start=last_in_db,
                                           gw_end=NEXT_GAMEWEEK)

        if NEXT_GAMEWEEK != 1:
            if last_finished > last_in_db:
                # need to update
                fill_results_from_api(last_in_db + 1, NEXT_GAMEWEEK, season,
                                      session)
                fill_playerscores_from_api(season, session, last_in_db + 1,
                                           NEXT_GAMEWEEK)
            else:
                print("Matches and player-scores already up-to-date")
            # now check transfers
            print("Checking team")
            db_players = sorted(
                get_current_players(season=season, dbsession=session))
            api_players = sorted(get_players_for_gameweek(last_finished))
            if db_players != api_players:
                update_team(season=season, session=session, verbose=True)
            else:
                print("Team is up-to-date")
        else:
            print("Skipping team and result updates - season hasn't started.")
예제 #3
0
def fill_session_team(team_id, session_id, dbsession=DBSESSION):
    """
    Use the FPL API to get list of players in an FPL squad with id=team_id,
    then fill the session team with these players.
    """
    # first reset the team
    reset_session_team(session_id, dbsession)
    # now query the API
    players = fetcher.get_fpl_team_data(get_last_finished_gameweek(), team_id)
    player_ids = [p["element"] for p in players]
    for pid in player_ids:
        add_session_player(pid, session_id, dbsession)
    team_history = fetcher.get_fpl_team_history_data()["current"]
    index = (get_last_finished_gameweek() - 1
             )  # as gameweek starts counting from 1 but list index starts at 0
    budget = team_history[index]["value"]
    set_session_budget(budget, session_id)
    return player_ids