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()
Beispiel #2
0
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():
        # 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))