コード例 #1
0
def make_attributes_table(seasons=[], dbsession=session):
    """Create the player attributes table using the previous 3 seasons (from
    player details JSON files) and the current season (from API)
    """
    if not seasons:
        seasons = get_past_seasons(3)
        seasons.append(CURRENT_SEASON)

    for season in seasons:
        if season == CURRENT_SEASON:
            continue
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/player_details_{}.json".format(season))
        with open(input_path, "r") as f:
            input_data = json.load(f)

        fill_attributes_table_from_file(detail_data=input_data,
                                        season=season,
                                        dbsession=dbsession)

    # this season's data from the API
    if CURRENT_SEASON in seasons:
        fill_attributes_table_from_api(season=CURRENT_SEASON,
                                       dbsession=dbsession)

    dbsession.commit()
コード例 #2
0
def make_player_details(seasons=get_past_seasons(3)):
    """generate player details json files"""
    if isinstance(seasons, str):
        seasons = [seasons]

    for season in seasons:
        season_longname = get_long_season_name(season)
        print("-------- SEASON", season_longname, "--------")

        teams_dict = get_teams_dict(season)
        positions_df = get_positions_df(season)

        fixtures_df, got_fixtures = get_fixtures_df(season)

        # names of all player directories for this season
        sub_dirs = glob(PLAYERS_DIR.format(season_longname) + "/*/")

        output = {}
        for directory in sub_dirs:
            name = path_to_name(directory)
            print("Doing", name)

            player_dict = process_file(
                os.path.join(directory, PLAYERS_FILE),
                teams_dict,
                fixtures_df,
                got_fixtures,
            )

            # get player position
            if name in positions_df.index:
                position = str(positions_df.loc[name])
            else:
                position = "NA"
                for k, v in alternative_player_names.items():
                    if name == k or name in v:
                        if k in positions_df.index:
                            position = str(positions_df.loc[k])
                        else:
                            for alt_name in v:
                                if alt_name in positions_df.index:
                                    position = str(positions_df.loc[alt_name])
                                    break
                        print("found", position, "via alternative name")
                        break
            if position == "NA":
                print("!!!FAILED!!! Could not find position for", name)
            for fixture in player_dict:
                fixture["position"] = position

            output[name] = player_dict

        print("Saving JSON")
        with open(SAVE_NAME.format(season), "w") as f:
            json.dump(output, f)

    print("DONE!")
コード例 #3
0
ファイル: fill_team_table.py プロジェクト: Abelarm/AIrsenal
def make_team_table(session):

    seasons = [CURRENT_SEASON]
    seasons += get_past_seasons(4)
    for season in seasons:
        filename = os.path.join(
            os.path.join(os.path.dirname(__file__), "..", "data",
                         "teams_{}.csv".format(season)))
        fill_team_table_from_file(filename, session)
コード例 #4
0
def make_fixture_table(session):
    # fill the fixture table for past seasons
    for season in get_past_seasons(3):
        filename = os.path.join(
            os.path.dirname(__file__),
            "..",
            "data",
            "results_{}_with_gw.csv".format(season),
        )
        fill_fixtures_from_file(filename, season, session)
    # now fill the current season from the api
    fill_fixtures_from_api(CURRENT_SEASON, session)
コード例 #5
0
ファイル: fill_player_table.py プロジェクト: Abelarm/AIrsenal
def make_player_table(session):

    fill_player_table_from_api(CURRENT_SEASON, session)
    for season in get_past_seasons(3):
        filename = os.path.join(
            os.path.join(
                os.path.dirname(__file__),
                "..",
                "data",
                "player_summary_{}.json".format(season),
            ))
        fill_player_table_from_file(filename, season, session)
コード例 #6
0
def make_team_table(seasons=[], dbsession=session):
    """
    Fill the db table containing the list of teams in the
    league for each season.
    """

    if not seasons:
        seasons = [CURRENT_SEASON]
        seasons += get_past_seasons(4)
    for season in seasons:
        filename = os.path.join(
            os.path.join(os.path.dirname(__file__), "..", "data",
                         "teams_{}.csv".format(season)))
        fill_team_table_from_file(filename, dbsession=dbsession)
コード例 #7
0
def make_result_table(session):
    """
    past seasons - read results from csv
    """
    for season in get_past_seasons(3):
        inpath = os.path.join(os.path.dirname(__file__),
                              "../data/results_{}_with_gw.csv".format(season))
        infile = open(inpath)
        fill_results_from_csv(infile, season, session)
    """
    current season - use API
    """
    gw_end = NEXT_GAMEWEEK
    fill_results_from_api(1, gw_end, CURRENT_SEASON, session)
コード例 #8
0
def make_playerscore_table(seasons=[], dbsession=session):
    # previous seasons data from json files
    if not seasons:
        seasons = get_past_seasons(3)
        seasons.append(CURRENT_SEASON)
    for season in seasons:
        if season == CURRENT_SEASON:
            continue
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/player_details_{}.json".format(season))
        input_data = json.load(open(input_path))
        fill_playerscores_from_json(input_data, season, dbsession=dbsession)
    # this season's data from the API
    if CURRENT_SEASON in seasons:
        fill_playerscores_from_api(CURRENT_SEASON, dbsession=dbsession)
コード例 #9
0
def make_player_table(seasons=[], dbsession=session):

    if not seasons:
        seasons = [CURRENT_SEASON]
        seasons += get_past_seasons(3)
    if CURRENT_SEASON in seasons:
        fill_player_table_from_api(CURRENT_SEASON, session)
    for season in seasons:
        if season == CURRENT_SEASON:
            continue
        filename = os.path.join(
            os.path.join(
                os.path.dirname(__file__),
                "..",
                "data",
                "player_summary_{}.json".format(season),
            ))
        fill_player_table_from_file(filename, season, session)
コード例 #10
0
def make_attributes_table(session):
    """Create the player attributes table using the previous 3 seasons (from
    player details JSON files) and the current season (from API)
    """
    seasons = get_past_seasons(3)

    for season in seasons:
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/player_details_{}.json".format(season))
        with open(input_path, "r") as f:
            input_data = json.load(f)

        fill_attributes_table_from_file(input_data, season, session)

    # this season's data from the API
    fill_attributes_table_from_api(CURRENT_SEASON, session)

    session.commit()
コード例 #11
0
def make_fifa_ratings_table(seasons=[], dbsession=session):
    # make the fifa ratings table
    # TODO: scrape the data first rather than committing file to repo

    if not seasons:
        seasons = get_past_seasons(3)
        seasons.append(CURRENT_SEASON)

    for season in seasons:
        print("FIFA RATINGS {}".format(season))
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/fifa_team_ratings_{}.csv".format(season))
        try:
            input_file = open(input_path)
        except FileNotFoundError:
            print("!!! No FIFA ratings file found for {}".format(season))
            continue

        for line in input_file.readlines()[1:]:
            team, att, mid, defn, ovr = line.strip().split(",")
            r = FifaTeamRating()
            r.season = season
            r.team = team
            r.att = int(att)
            r.defn = int(defn)
            r.mid = int(mid)
            r.ovr = int(ovr)
            team_is_known = False
            for k, v in alternative_team_names.items():
                if team in v:
                    r.team = k
                    team_is_known = True
                elif team == k:
                    team_is_known = True
            if not team_is_known:
                raise ValueError("Unknown team {}.".format(team))
            dbsession.add(r)

    dbsession.commit()
コード例 #12
0
def make_player_details(seasons=get_past_seasons(3)):
    """generate player details json files"""
    if isinstance(seasons, str):
        seasons = [seasons]

    for season in seasons:
        season_longname = get_long_season_name(season)
        print("SEASON", season_longname)

        sub_dirs = glob(DATA_DIR.format(season_longname) + "/*/")

        teams_dict = get_teams_dict(season)

        output = {}
        for directory in sub_dirs:
            name = path_to_name(directory)
            print("Doing", name)
            player_dict = process_file(os.path.join(directory, FILE_NAME),
                                       teams_dict)
            output[name] = player_dict

        print("Saving JSON")
        with open(SAVE_NAME.format(season), "w") as f:
            json.dump(output, f)
コード例 #13
0
from airsenal.framework.utils import (get_past_seasons, get_teams_for_season,
                                      session, CURRENT_SEASON,
                                      get_fixtures_for_season,
                                      get_result_for_fixture,
                                      get_player_scores_for_fixture)
from airsenal.framework.schema import (Fixture, Result, PlayerScore)

CHECK_SEASONS = [CURRENT_SEASON] + get_past_seasons(3)
SEPARATOR = "\n" + ("=" *
                    50) + "\n"  # used to separate groups of print statements


def fixture_string(fixture, result=None):
    """Get a string with basic info about a fixture.
    
    Arguments:
        fixture {SQLAlchemy class object} -- fixture from the database.
        result {SQLAlchemy class object} -- result from the database. If given
        returned string contains the match score.
    
    Returns:
        [string] -- formatted string with id, season, gameweek, home team and
        away team.
    """

    if result:
        return "{} GW{} {} {}-{} {} (id {})".format(
            fixture.season, fixture.gameweek, fixture.home_team,
            result.home_score, result.away_score, fixture.away_team,
            fixture.fixture_id)
コード例 #14
0
    teams = {team["id"]: team["short_name"] for team in data["teams"]}
    positions = {
        et["id"]: et["singular_name_short"]
        for et in data["element_types"]
    }

    player_summaries = []

    for player in data["elements"]:
        name = player["first_name"] + " " + player["second_name"]
        print(player["first_name"] + " " + player["second_name"])
        player_dict = {"name": name}
        for input_key, output_key in keys_to_extract.items():
            player_dict[output_key] = player[input_key]

        player_dict["team"] = teams[player_dict["team"]]
        player_dict["position"] = positions[player_dict["position"]]

        player_summaries.append(player_dict)

    with open(SAVE_FILE.format(season), "w") as f:
        json.dump(player_summaries, f)


if __name__ == "__main__":
    for season in get_past_seasons(3):
        print(f"---- MAKING PLAYER SUMMARIES FOR {season} SEASON ----")
        make_player_summary(season)
    print("---- DONE ----")