Exemple #1
0
def get_player(player_name_or_id, dbsession=None):
    """
    query the player table by name or id, return the player object (or None).
    NOTE the player_id that can be passed as an argument here is NOT
    guaranteed to be the id for that player in the FPL API.  The one here
    is the entry (primary key) in our database.
    Use the function get_player_from_api_id() to find the player corresponding
    to the FPL API ID.
    """
    if not dbsession:
        dbsession = session  # use the one defined in this module

    # if an id has been passed as a string, convert it to an integer
    if isinstance(player_name_or_id, str) and player_name_or_id.isdigit():
        player_name_or_id = int(player_name_or_id)

    if isinstance(player_name_or_id, int):
        filter_attr = Player.player_id
    else:
        filter_attr = Player.name
    p = dbsession.query(Player).filter(
        filter_attr == player_name_or_id).first()
    if p:
        return p
    if isinstance(player_name_or_id, int):  # didn't find by id - return None
        return None
    # assume we have a name, now try alternative names
    for k, v in alternative_player_names.items():
        if player_name_or_id in v:
            p = dbsession.query(Player).filter_by(name=k).first()
            if p:
                return p
    # didn't find it - return None
    return None
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!")
Exemple #3
0
def get_player_id(player_name, dbsession=None):
    """
    lookup player id, for machine readability
    """
    if not dbsession:
        dbsession = session
    p = dbsession.query(Player).filter_by(name=player_name).first()
    if p:
        return p.player_id
    # not found by name in DB - try alternative names
    for k, v in alternative_player_names.items():
        if player_name in v:
            p = dbsession.query(Player).filter_by(name=k).first()
            if p:
                return p.player_id
            break
    # still not found
    print("Unknown player_name {}".format(player_name))
    return None