Esempio n. 1
0
def fill_results_from_api(gw_start, gw_end, season, dbsession):
    fetcher = FPLDataFetcher()
    matches = fetcher.get_fixture_data()
    for m in matches:
        if not m["finished"]:
            continue
        gameweek = m["event"]
        if gameweek < gw_start or gameweek > gw_end:
            continue
        home_id = m["team_h"]
        away_id = m["team_a"]
        home_team = None
        away_team = None
        for k, v in alternative_team_names.items():
            if str(home_id) in v:
                home_team = k
            elif str(away_id) in v:
                away_team = k
        if not home_team:
            raise ValueError("Unable to find team with id {}".format(home_id))
        if not away_team:
            raise ValueError("Unable to find team with id {}".format(away_id))
        home_score = m["team_h_score"]
        away_score = m["team_a_score"]
        f = _find_fixture(season, home_team, away_team, dbsession)
        res = Result()
        res.fixture = f
        res.home_score = int(home_score)
        res.away_score = int(away_score)
        dbsession.add(res)
    dbsession.commit()
Esempio n. 2
0
def fill_results_from_csv(input_file, season, dbsession):
    for line in input_file.readlines()[1:]:
        (
            date,
            home_team,
            away_team,
            home_score,
            away_score,
            gameweek,
        ) = line.strip().split(",")
        print(line.strip())
        for k, v in alternative_team_names.items():
            if home_team in v:
                home_team = k
            elif away_team in v:
                away_team = k
        ## query database to find corresponding fixture
        tag = get_latest_fixture_tag(season, dbsession)
        f = _find_fixture(season, home_team, away_team, dbsession)
        res = Result()
        res.fixture = f
        res.home_score = int(home_score)
        res.away_score = int(away_score)
        dbsession.add(res)
    dbsession.commit()
Esempio n. 3
0
def get_fixtures_df(season):
    """Load fixture info (which teams played in which matches), either
    from vaastav/Fantasy-Premier-League repo or AIrsenal data depending
    on what's available.
    """
    season_longname = get_long_season_name(season)

    if os.path.exists(FIXTURES_PATH.format(season_longname)):
        #  fixtures file in vaastav/Fantasy-Premier-League repo
        # contains fixture ids
        fixtures_df = pd.read_csv(FIXTURES_PATH.format(season_longname),
                                  index_col="id")
        got_fixtures = True
    elif os.path.exists(RESULTS_PATH.format(season)):
        #  match results files in airsenal data
        # need to match teams by gameweek etc.
        fixtures_df = pd.read_csv(RESULTS_PATH.format(season))

        #  replace full team names with 3 letter codes
        for short_name, long_names in alternative_team_names.items():
            replace_dict = {name: short_name for name in long_names}
            fixtures_df["home_team"].replace(replace_dict, inplace=True)
            fixtures_df["away_team"].replace(replace_dict, inplace=True)

        got_fixtures = False
    else:
        raise FileNotFoundError(
            "Couldn't find fixtures file for {} season".format(season))

    return fixtures_df, got_fixtures
def make_fifa_ratings_table(session, season=CURRENT_SEASON):
    # make the fifa ratings table
    # TODO: scrape the data first rather than committing file to repo
    input_path = os.path.join(
        os.path.dirname(__file__),
        "../data/fifa_team_ratings_{}.csv".format(season))
    input_file = open(input_path)
    for line in input_file.readlines()[1:]:
        team, att, mid, defn, ovr = line.strip().split(",")
        print(line.strip())
        r = FifaTeamRating()
        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))
        session.add(r)
    session.commit()
Esempio n. 5
0
def fill_fixtures_from_api(season, dbsession=session):
    """
    Use the FPL API to get a list of fixures.
    """
    tag = str(uuid.uuid4())
    fetcher = FPLDataFetcher()
    fixtures = fetcher.get_fixture_data()
    for fixture in fixtures:
        try:
            f = find_fixture(
                fixture["team_h"],
                was_home=True,
                other_team=fixture["team_a"],
                season=season,
                dbsession=dbsession,
            )
            update = True
        except ValueError:
            f = Fixture()
            update = False

        f.date = fixture["kickoff_time"]
        f.gameweek = fixture["event"]
        f.season = season
        f.tag = tag

        home_id = fixture["team_h"]
        away_id = fixture["team_a"]
        found_home = False
        found_away = False
        for k, v in alternative_team_names.items():
            if str(home_id) in v:
                f.home_team = k
                found_home = True
            elif str(away_id) in v:
                f.away_team = k
                found_away = True
            if found_home and found_away:
                break

        error_str = "Can't find team(s) with id(s): {}."
        if not found_home and found_away:
            raise ValueError(error_str.format(home_id + ", " + away_id))
        elif not found_home:
            raise ValueError(error_str.format(home_id))
        elif not found_away:
            raise ValueError(error_str.format(away_id))
        else:
            pass

        if not update:
            dbsession.add(f)

    dbsession.commit()
    return True
Esempio n. 6
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()
Esempio n. 7
0
def fill_fixtures_from_file(filename, season, session):
    """
    use the match results csv files to get a list of matches in a season,
    """
    infile = open(filename)
    for line in infile.readlines()[1:]:
        fields = line.strip().split(",")
        f = Fixture()
        f.date = fields[0]
        f.gameweek = fields[5]
        home_team = fields[1]
        away_team = fields[2]
        for k, v in alternative_team_names.items():
            if home_team in v:
                f.home_team = k
            elif away_team in v:
                f.away_team = k
        print(" ==> Filling fixture {} {}".format(f.home_team, f.away_team))
        f.season = season
        f.tag = "latest"  # not really needed for past seasons
        session.add(f)
    session.commit()