def get_entire_gameweek_results_per_player_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) event_status_call = conn.get_event_status() if event_status_call.status_code != 200: return event_status = json.loads(event_status_call.text) if event_status["leagues"] == "Updated": previous_gameweek = event_status["status"][0]["event"] else: previous_gameweek = event_status["status"][0]["event"] - 1 for player in bootstrap_static["elements"]: print("Player", player["id"]) player_summary_call = conn.get_player_summary(player["id"]) if player_summary_call.status_code != 200: return player_summary = json.loads(player_summary_call.text) for gw in player_summary["history"]: with open( config["settings"]["current_season"] + "/data/players/gameweek_history/gameweek_" + str(gw["round"]) + ".json", "r") as file: gameweek_json = json.load(file) gameweek_json.append(gw) with open( config["settings"]["current_season"] + "/data/players/gameweek_history/gameweek_" + str(gw["round"]) + ".json", "w") as file: file.write(json.dumps(gameweek_json))
def get_all_team_ids(): bootstrap_static_call = fpl_api.FPLCalls().get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) teams = list() for team in bootstrap_static["teams"]: teams.append(team["id"]) return teams
def get_standings(self): if self.league_type == "CLASSIC": standings_call = fpl_api.FPLCalls().get_classic_league_standings( self.id, page_new_entries=None, page_standings=None, phase=None) elif self.league_type == "H2H": standings_call = fpl_api.FPLCalls().get_h2h_league_standings( self.id, page_new_entries=None, page_standings=None, phase=None) else: raise ValueError( "Invalid league type. Expected one of: ['CLASSIC', 'H2H']") if standings_call.status_code != 200: return return json.loads(standings_call.text)
def get_properties(self): fixtures_call = fpl_api.FPLCalls().get_fixtures( event=None, only_future_fixtures=False) if fixtures_call.status_code != 200: return fixtures = json.loads(fixtures_call.text) for fixture in fixtures: if fixture["id"] == self.id: return fixture return
def get_bootstrap_static_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) with open( f"{config['settings']['current_season']}/data/bootstrap_static.json", "w") as file: file.write(json.dumps(bootstrap_static))
def player_web_name_to_id(web_name: str) -> Union[None, int]: conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static_json = json.loads(bootstrap_static_call.text) for item in bootstrap_static_json["elements"]: if item["web_name"].lower() == web_name.lower(): return item["id"] return None
def get_fixture(home_team: Team, away_team: Team) -> Union[None, Fixture]: fixtures_call = fpl_api.FPLCalls().get_fixtures(event=None, only_future_fixtures=False) if fixtures_call.status_code != 200: return fixtures = json.loads(fixtures_call.text) for fixture in fixtures: if fixture["team_h"] == home_team.id and fixture[ "team_a"] == away_team.id: return Fixture(fixture["id"]) return
def team_name_to_id(team_name: str): teams = get_all_teams() if team_name.lower() not in teams: raise ValueError(f"Team {team_name.lower()} not found.") bootstrap_static_call = fpl_api.FPLCalls().get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) for team in bootstrap_static["teams"]: if team_name.lower() in [ team["name"].lower(), team["short_name"].lower() ]: return team["id"]
def get_player_performances_and_save(): conn = fpl_api.FPLCalls() for gameweek in range( len( os.listdir(config["settings"]["current_season"] + "/data/gameweeks/general/"))): fixture_results_call = conn.get_live_player_stats(gameweek + 1) if fixture_results_call.status_code != 200: continue fixture_results = json.loads(fixture_results_call.text) with open( config["settings"]["current_season"] + "/data/gameweeks/player_performances/gameweek_" + str(gameweek + 1) + ".json", "w") as file: file.write(json.dumps(fixture_results))
def get_all_team_info_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) for team in bootstrap_static["teams"]: with open( config["settings"]["current_season"] + "/data/teams/" + str(team["id"]) + "_" + team["name"] + ".json", "w") as file: file.write(json.dumps(team)) with open( config["settings"]["current_season"] + "/data/teams/all_teams.json", "w") as file: file.write(json.dumps(bootstrap_static["teams"]))
def get_entire_player_properties_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) for player in bootstrap_static["elements"]: player_summary_call = conn.get_player_summary(player["id"]) if player_summary_call.status_code != 200: return player_summary = json.loads(player_summary_call.text) print("Player", player["id"]) with open( f"{config['settings']['current_season']}/data/players/{player['id']}_{player['web_name']}.json", "w") as file: file.write(json.dumps(player_summary["history"]))
def get_finished_gameweeks_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) last_finished_gameweek = 0 for gameweek in bootstrap_static["events"]: if gameweek["finished"] and gameweek["data_checked"]: with open( config["settings"]["current_season"] + "/data/gameweeks/general/gameweek_" + str(gameweek["id"]) + ".json", "w") as file: file.write(json.dumps(gameweek)) if gameweek["id"] > last_finished_gameweek: last_finished_gameweek = gameweek["id"] config["settings"]["last_finished_gameweek"] = str(last_finished_gameweek) save_config() get_player_performances_and_save()
def get_all_person_data_and_save_to_json(): conn = fpl_api.FPLCalls() event_status_call = conn.get_event_status() if event_status_call.status_code != 200: return event_status = json.loads(event_status_call.text) if event_status["leagues"] == "Updated": previous_gameweek = event_status["status"][0]["event"] else: previous_gameweek = event_status["status"][0]["event"] - 1 for person_name, person_id in config["managers"].items(): gw_history = list() for item in range(1, previous_gameweek + 1): result_call = conn.get_person_picks(person_id, item) result = json.loads(result_call.text) gw_history.append(result) with open( config["settings"]["current_season"] + "/data/managers/" + person_name + "_" + person_id + ".json", 'w') as outfile: outfile.write(json.dumps(gw_history))
def get_all_fixtures_and_save(): conn = fpl_api.FPLCalls() fixtures_call = conn.get_fixtures(event=None, only_future_fixtures=False) if fixtures_call.status_code != 200: return fixtures = json.loads(fixtures_call.text) for fixture in fixtures: print(fixture["id"]) if fixture["finished"]: with open( config["settings"]["current_season"] + "/data/fixtures/" + str(fixture["id"]) + "-" + str(fixture["team_h"]) + "_" + str(fixture["team_a"]) + "-finished" + ".json", "w") as file: file.write(json.dumps(fixture)) else: with open( config["settings"]["current_season"] + "/data/fixtures/" + str(fixture["id"]) + "-" + str(fixture["team_h"]) + "_" + str(fixture["team_a"]) + ".json", "w") as file: file.write(json.dumps(fixture))
def get_bootstrap_static_and_save(): conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: return bootstrap_static = json.loads(bootstrap_static_call.text) with open( f"{config['settings']['current_season']}/data/bootstrap_static.json", "w") as file: file.write(json.dumps(bootstrap_static)) if __name__ == '__main__': conn = fpl_api.FPLCalls() bootstrap_static_call = conn.get_bootstrap_static() if bootstrap_static_call.status_code != 200: exit() bootstrap_static = json.loads(bootstrap_static_call.text) # for gameweek in range(25, 34): # y1 = list() # x1 = list() # for person in persons: # captain = get_person_captain_for_gameweek(conn, person["id"], gameweek) # print(captain) # y1.append(get_points_for_player(conn, captain, gameweek)) # # x1.append("GW" + str(gameweek))
def get_info(self): person_info_call = fpl_api.FPLCalls().get_person_info(self.id) if person_info_call.status_code != 200: return return json.loads(person_info_call.text)
def get_history(self): history_call = fpl_api.FPLCalls().get_person_history(self.id) if history_call.status_code != 200: return return json.loads(history_call.text)
def get_players_stats(self): live_call = fpl_api.FPLCalls().get_live_player_stats(self.id) if live_call.status_code != 200: return return json.loads(live_call.text)