def load_results_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) results = get_match_results_of_league(league_name, season) if not type(results) == list: raise Exception('Failed to load results') for match_result in results: if not RESULT_MATCH_ID in match_result: continue match_id = match_result[RESULT_MATCH_ID] match = Match.objects.filter(match_api_id = match_id) if len(match) != 0: current_match = match[0] current_match.team1_score = match_result[RESULT_HOME_GOALS] current_match.team2_score = match_result[RESULT_AWAY_GOALS] current_match.save() store_player_stats_for_the_match(current_match, match_result) else: print "Could not find appropriate match.. Skipping it"
def load_results_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) results = get_match_results_of_league(league_name, season) if not type(results) == list: raise Exception('Failed to load results') for match_result in results: if not RESULT_MATCH_ID in match_result: continue match_id = match_result[RESULT_MATCH_ID] match = Match.objects.filter(match_api_id=match_id) if len(match) != 0: current_match = match[0] current_match.team1_score = match_result[RESULT_HOME_GOALS] current_match.team2_score = match_result[RESULT_AWAY_GOALS] current_match.save() store_player_stats_for_the_match(current_match, match_result) else: print "Could not find appropriate match.. Skipping it"
def load_results_tournament_from_last_update(tournament, lastUpdateDate, today): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) startDate = lastUpdateDate.strftime('%Y-%m-%d') endDate = today.strftime('%Y-%m-%d') results = get_match_results_of_league_between_dates(league_name, startDate, endDate) if len(results) == 0: return if not type(results) == list: raise Exception('Failed to load results') for match_result in results: if not RESULT_MATCH_ID in match_result: continue match_id = match_result[RESULT_MATCH_ID] match = Match.objects.get(match_api_id = match_id) match.team1_score = match_result[RESULT_HOME_GOALS] match.team2_score = match_result[RESULT_AWAY_GOALS] match.save() players = player.objects.filter() update_prediction_points_for_match(tournament, match)
def load_teams_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) teams = get_teams_by_league_season(league_name, season) teamlist = [] is_club = True if not type(teams) == list: raise Exception('Failed to load team data') team_cnt = 0 for team in teams: if not TEAM_ID_KEY in team: continue team_id = team[TEAM_ID_KEY] team_cnt = team_cnt + 1 if Team.objects.filter(team_api_id=team_id).count() > 0: continue new_team = Team() new_team.team_api_id = team_id new_team.team_cd = utility.get_team_code_from_name(team[TEAM_NAME_KEY]) new_team.team_logo = load_logo(team[TEAM_WIKI_KEY]) new_team.save() venue = Venue.add_and_get_venue(team[TEAM_STADIUM_KEY]) if is_club: new_club = Club() new_club.club_cd = new_team new_club.club_name = team[TEAM_NAME_KEY] new_club.country = Country.add_and_get_country( team[TEAM_COUNTRY_KEY]) new_club.home_ground = venue new_club.save() tourn_teams = TournamentTeam.objects.create(team=new_team, tourn=tournament) tourn_fmt = tournament.tourn_format if tourn_fmt.no_of_teams == 0: tourn_fmt.no_of_teams = team_cnt tourn_fmt.save() elif not tourn_fmt.no_of_teams == team_cnt: new_fmt = TournamentFormat() new_fmt.tourn = tourn_fmt.tourn new_fmt.gw_cycle_day = tourn_fmt.gw_cycle_day new_fmt.no_of_teams = team_cnt new_fmt.save() tournament.tourn_format = new_fmt tournament.save() # load_players(tournament) return tournament
def load_fixtures_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) fixtures = get_fixtures_by_league_season(league_name, season) if not type(fixtures) == list: raise Exception('Failed to load fixtures') max_round = 0 for fixture in fixtures: if not FIXTURE_ID_KEY in fixture: continue match_id = fixture[FIXTURE_ID_KEY] qs = Match.objects.filter(match_api_id=match_id) if qs.count() == 0: match = Match() match.match_api_id = match_id else: match = qs[0] match.team1 = Team.get_team_by_api_id(fixture[FIXTURE_HOMETEAM_ID_KEY]) match.team2 = Team.get_team_by_api_id(fixture[FIXTURE_AWAYTEAM_ID_KEY]) match.tourn = tournament match.match_date = parser.parse(fixture[FIXTURE_DATE_KEY]) match.gameweek_no = int(fixture[FIXTURE_GAMEWEEK_KEY]) match.gameweek = Gameweek.get_gameweek(tournament, fixture[FIXTURE_GAMEWEEK_KEY], add=True) match.venue = Venue.add_and_get_venue(fixture[FIXTURE_VENUE_KEY]) match.save() if match.gameweek_no > max_round: max_round = match.gameweek_no tourn_fmt = tournament.tourn_format if tourn_fmt.no_of_gameweeks == 0: tourn_fmt.no_of_gameweeks = max_round tourn_fmt.save() elif not tourn_fmt.no_of_gameweeks == max_round: new_fmt = TournamentFormat() new_fmt.tourn = tourn_fmt.tourn new_fmt.gw_cycle_day = tourn_fmt.gw_cycle_day new_fmt.no_of_teams = tourn_fmt.no_of_teams new_fmt.no_of_gameweeks = max_round new_fmt.save() tournament.tourn_format = new_fmt tournament.save() tournament.set_date_range() tournament.normalize_gameweek() return tournament
def load_results_tournament_from_last_update(tournament, lastUpdateDate, today): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) startDate = lastUpdateDate.strftime('%Y-%m-%d') endDate = today.strftime('%Y-%m-%d') results = get_match_results_of_league_between_dates( league_name, startDate, endDate) if len(results) == 0: return if not type(results) == list: raise Exception('Failed to load results') for match_result in results: if not RESULT_MATCH_ID in match_result: continue match_id = match_result[RESULT_MATCH_ID] match = Match.objects.get(match_api_id=match_id) match.team1_score = match_result[RESULT_HOME_GOALS] match.team2_score = match_result[RESULT_AWAY_GOALS] match.save() players = player.objects.filter() update_prediction_points_for_match(tournament, match)
def load_teams_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) teams = get_teams_by_league_season(league_name, season) teamlist = [] is_club = True if not type(teams) == list: raise Exception('Failed to load team data') team_cnt = 0 for team in teams: if not TEAM_ID_KEY in team: continue team_id = team[TEAM_ID_KEY] team_cnt = team_cnt + 1 if Team.objects.filter(team_api_id = team_id).count() > 0: continue new_team = Team() new_team.team_api_id = team_id new_team.team_cd = utility.get_team_code_from_name(team[TEAM_NAME_KEY]) new_team.team_logo = load_logo(team[TEAM_WIKI_KEY]) new_team.save() venue = Venue.add_and_get_venue(team[TEAM_STADIUM_KEY]) if is_club: new_club = Club() new_club.club_cd = new_team new_club.club_name = team[TEAM_NAME_KEY] new_club.country = Country.add_and_get_country(team[TEAM_COUNTRY_KEY]) new_club.home_ground = venue new_club.save() tourn_teams = TournamentTeam.objects.create(team=new_team, tourn=tournament) tourn_fmt = tournament.tourn_format if tourn_fmt.no_of_teams == 0: tourn_fmt.no_of_teams = team_cnt tourn_fmt.save() elif not tourn_fmt.no_of_teams == team_cnt: new_fmt = TournamentFormat() new_fmt.tourn = tourn_fmt.tourn new_fmt.gw_cycle_day = tourn_fmt.gw_cycle_day new_fmt.no_of_teams = team_cnt new_fmt.save() tournament.tourn_format = new_fmt tournament.save() # load_players(tournament) return tournament
def load_team_standings(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) standings = get_latest_league_standings(league_name, season) position = 1 if not type(standings) == list: raise Exception('Failed to load standings') for team in standings: if not STANDING_TEAM_ID in team: continue team_id = team[STANDING_TEAM_ID] teamStanding = TeamStandings.objects.filter(tourn = tournament, team__team_api_id = team_id) if teamStanding.count() == 0: standing = TeamStandings() standing.tourn = tournament standing.team = Team.objects.get(team_api_id = team_id) else: standing = teamStanding[0] standing.played = team[STANDING_TEAM_PLAYED] standing.points = team[STANDING_TEAM_POINTS] standing.position = position standing.save() position = position + 1
def load_fixtures_tournament(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) fixtures = get_fixtures_by_league_season(league_name, season) if not type(fixtures) == list: raise Exception('Failed to load fixtures') max_round = 0 for fixture in fixtures: if not FIXTURE_ID_KEY in fixture: continue match_id = fixture[FIXTURE_ID_KEY] qs = Match.objects.filter(match_api_id = match_id) if qs.count() == 0: match = Match() match.match_api_id = match_id else: match = qs[0] match.team1 = Team.get_team_by_api_id(fixture[FIXTURE_HOMETEAM_ID_KEY]) match.team2 = Team.get_team_by_api_id(fixture[FIXTURE_AWAYTEAM_ID_KEY]) match.tourn = tournament match.match_date = parser.parse(fixture[FIXTURE_DATE_KEY]) match.gameweek_no = int(fixture[FIXTURE_GAMEWEEK_KEY]) match.gameweek = Gameweek.get_gameweek(tournament, fixture[FIXTURE_GAMEWEEK_KEY], add = True) match.venue = Venue.add_and_get_venue(fixture[FIXTURE_VENUE_KEY]) match.save() if match.gameweek_no > max_round: max_round = match.gameweek_no tourn_fmt = tournament.tourn_format if tourn_fmt.no_of_gameweeks == 0: tourn_fmt.no_of_gameweeks = max_round tourn_fmt.save() elif not tourn_fmt.no_of_gameweeks == max_round: new_fmt = TournamentFormat() new_fmt.tourn = tourn_fmt.tourn new_fmt.gw_cycle_day = tourn_fmt.gw_cycle_day new_fmt.no_of_teams = tourn_fmt.no_of_teams new_fmt.no_of_gameweeks = max_round new_fmt.save() tournament.tourn_format = new_fmt tournament.save() tournament.set_date_range() tournament.normalize_gameweek() return tournament
def load_team_standings(tournament): league_name = tournament.tourn_format.tourn.tourn_name season = utility.convert_season_string(tournament.season) standings = get_latest_league_standings(league_name, season) position = 1 if not type(standings) == list: raise Exception('Failed to load standings') for team in standings: if not STANDING_TEAM_ID in team: continue team_id = team[STANDING_TEAM_ID] teamStanding = TeamStandings.objects.filter(tourn=tournament, team__team_api_id=team_id) if teamStanding.count() == 0: standing = TeamStandings() standing.tourn = tournament standing.team = Team.objects.get(team_api_id=team_id) else: standing = teamStanding[0] standing.played = team[STANDING_TEAM_PLAYED] standing.points = team[STANDING_TEAM_POINTS] standing.position = position standing.save() position = position + 1