示例#1
0
    def get_current_team(self):
        """
        get the current team where this player has been playing
        :return:
        """
        import src.application.Domain.Team as Team
        try:
            return Cache.get_element(self.id, "PLAYER_CURRENT_TEAM")
        except KeyError:
            pass

        matches = self.get_matches()
        current_team = None
        if len(matches) > 0:
            last_match = sorted(matches, key=lambda match: match.date)[-1]
            home_player_i = 'home_player_'
            away_player_i = 'away_player_'
            for i in range(11):
                if last_match.__getattribute__(home_player_i +
                                               str(i +
                                                   1)) == self.player_api_id:
                    current_team = Team.read_by_team_api_id(
                        last_match.home_team_api_id)
                    break
                if last_match.__getattribute__(away_player_i +
                                               str(i +
                                                   1)) == self.player_api_id:
                    current_team = Team.read_by_team_api_id(
                        last_match.away_team_api_id)
                    break
        Cache.add_element(self.id, current_team, "PLAYER_CURRENT_TEAM")
        return current_team
    def get_team_name(self):
        """
        get names of the crawled team, both long and short
        :return:
        """
        team_long_name = str(
            self.soup.find_all('span',
                               {'class': 'mx-break-micro'})[0].string).strip()
        team_short_name = str(
            self.soup.find_all('span',
                               {'class': 'mx-show-micro'})[0].string).strip()

        teams = Team.read_by_name(team_long_name, like=True)
        if len(teams) == 0:
            # team not found --> store new team in the DB
            Team.write_new_team(team_long_name,
                                None,
                                team_api_id=self.team_api_id,
                                team_short_name=team_short_name)
        if len(teams) == 1:
            # team found --> update its team_api_id
            teams[0].team_api_id = self.team_api_id
            teams[0].team_short_name = team_short_name
            Team.update(teams[0])
        else:
            # more than one team has been found --> manual check must be done!
            log.warning("Team with api id [" + self.team_api_id +
                        "] and name [" + team_long_name + "] at the link [" +
                        self.team_link + "] must be matched at hand")

        return team_short_name
def search_by_team():
    user_input = input("Insert team name: ")
    GuiUtil.print_info("Looking for", user_input)

    teams_found = Team.read_by_name(user_input, like=False)
    if len(teams_found) == 0:
        GuiUtil.print_att("No team found with exact name", user_input)
        teams_found = Team.read_by_name(user_input, like=True)
        if len(teams_found) == 0:
            GuiUtil.print_att("No team found", user_input)
        elif len(teams_found) == 1:
            team = teams_found[0]
            show_players(user_input,
                         team.get_current_players(),
                         "Players of the team",
                         team=team,
                         show_details=True)

    elif len(teams_found) == 1:
        team = teams_found[0]
        show_players(user_input,
                     team.get_current_players(),
                     "Players of the team",
                     team=team,
                     show_details=True)
示例#4
0
    def start_crawling(self):
        """
        Start crawling this team
        :return:
        """
        if util.is_None(
                self.team) or (not util.is_None(self.team)
                               and util.is_None(self.team.team_fifa_api_id)):
            # If one of the follow:
            # 1) team not stored in the DB
            # 2) Team fifa api id not stored in the DB
            # --> looking for name and fifa_api_id
            team_long_name, team_fifa_api_id = self.look_for_base_data()

            if util.is_None(self.team):
                # team not present in the DB
                self.team = Team.write_new_team(team_long_name,
                                                team_fifa_api_id)
            else:
                # team present in the DB, but without set the team_fifa_api_id
                self.team.team_fifa_api_id = team_fifa_api_id
                self.team = Team.update(self.team)

        # looking for players belonging this team
        link_players_found = self.look_for_players()
        for player_link, player_name in link_players_found.items():
            player_fifa_api_id = player_link[25:]
            player = Player.read_by_fifa_api_id(player_fifa_api_id)

            # crawl the player if and only if on of the following happens:
            # 1) PLAYER DOES NOT EXIST IN THE DB
            # 2) PLAYER ATTRIBUTES DO NOT EXIST IN THE DB
            # 3) PLAYER ATTRIBUTES IN THE DB ARE OLD
            # 4) FORCE PARSING OF PLAYER ATTRIBUTES
            if \
                    not player \
                    or not player.get_last_player_attributes() \
                    or util.compare_time_to_now(player.get_last_player_attributes().date, self.day_passed) \
                    or self.force_parsing:
                log.debug("Player to crawl [" + player_link + ", " +
                          player_name + "]")
                cp = CrawlerPlayer(player, player_link)
                cp.start_crawling()

        # looking for build up play
        attributes_found = self.look_for_team_attributes()
        if len(attributes_found) > 0:
            self.team.save_team_attributes(attributes_found)
 def get_away_team(self):
     """
     Return the away-team of this match
     :return:
     """
     import src.application.Domain.Team as Team
     return Team.read_by_team_api_id(self.away_team_api_id)
示例#6
0
    def get_ranking(self, season, home=None):
        """
        Return the position of the teams in this league
        Return a list of pair <Points, Team>, ordered py descending points
        :param season:
        :param stage:
        :param home:
        :return:
        """

        import src.application.Domain.Team as Team

        matches = self.get_matches(season=season, finished=True)
        teams = self.get_teams(season=season)
        ranking = {team.id: 0 for team in teams}
        for m in matches:
            result, winner = m.get_winner()
            if not util.is_None(winner):
                ranking[winner.id] += 3
            else:
                ranking[m.get_home_team().id] += 1
                ranking[m.get_away_team().id] += 1

        ranking_ret = []
        for team, p in sorted(ranking.items(),
                              key=operator.itemgetter(1))[::-1]:
            ranking_ret.append((p, Team.read_by_id(team)))

        return ranking_ret
示例#7
0
 def get_teams(self, season=None):
     """
     Return teams of a league, can be filtered by season
     :param season:
     :return:
     """
     import src.application.Domain.Team as Team
     return Team.read_teams_by_league(self, season)
示例#8
0
def search_by_team():
    user_input = input("Insert team name: ")
    GuiUtil.print_info("Looking for", user_input)
    teams_found = Team.read_by_name(user_input)

    GuiUtil.print_info("Teams found", len(teams_found))
    if len(teams_found) == 0:
        GuiUtil.print_att("Looking for team with similar name", user_input)
        teams_found = Team.read_by_name(user_input, like=True)

    if len(teams_found) == 0:
        GuiUtil.print_att("Teams found", 0)
    elif len(teams_found) == 1:
        team = teams_found[0]
        matches = team.get_matches(season=util.get_current_season(),
                                   ordered=True)
        for i, match in enumerate(matches):
            match_out = get_printable_match(match)
            GuiUtil.print_indent_answer(i + 1, match_out, True)
    else:
        GuiUtil.print_att("Too many teams found", "Be more precise")
def check_team(team_api_id):
    """
    Check if the team is stored in the DB
    If not --> crawl its web page and get information
    :param team_api_id:
    :return:
    """
    team = Team.read_by_team_api_id(team_api_id)
    if not team:
        cm = CrawlerTeam(team_api_id)
        return cm.get_team_name()
    else:
        return team.team_long_name
def search_by_name():
    user_input = input("Insert team name: ")
    teams = Team.read_by_name(user_input, like=True)

    if len(teams) == 0:
        GuiUtil.print_att("No teams found", user_input)
    else:
        teams = sorted(teams, key=lambda team: team.team_long_name)
        teams_to_print = [
            t.team_long_name + ": http://sofifa.com/team/" +
            str(t.team_fifa_api_id) for t in teams
        ]
        GuiUtil.show_list_answer(teams_to_print, print_index=True)
    def start_crawling(self):
        """
        Start crawling the league
        :return:
        """
        link_teams_found = self.look_for_teams(self.league_link)

        for team_link, team_name in link_teams_found.items():
            teams = Team.read_by_name(team_name, like=True)
            if len(teams) == 0:
                # no team found with this name
                log.debug("No team found with the name [" + team_name + "]")
                ct = CrawlerTeam(None, team_link)
                ct.start_crawling()
            elif len(teams) == 1:
                print("\t-", team_name, team_link)
                ct = CrawlerTeam(teams[0], team_link)
                ct.start_crawling()
            else:
                log.warning("More than one team with the same name [" +
                            team_name + "]")
示例#12
0
    def get_best_team_predicted(self, league, season, stage, n_teams_returned=3):

        best_teams = dict()
        s = season
        i = 1
        if stage - 1 == 0:
            y = int(s.split("/")[0]) - 1
            s = str(y) + "/" + str(y + 1)
            stage_predictions = league.get_stages_by_season(s)
        else:
            stage_predictions = stage - 1

        while i <= self.ml_train_stages_to_train:
            predictions = self.predict(league, s, stage_predictions)

            for match_id, pair in predictions.items():
                if len(pair) == 0:
                    continue
                match = Match.read_by_match_id(match_id)
                pred_label = pair[0]
                if pred_label == MLUtil.get_label(match):
                    util.increase_dict_entry(match.home_team_api_id, best_teams)
                    util.increase_dict_entry(match.away_team_api_id, best_teams)
            i += 1
            if stage_predictions - i == 0:
                y = int(s.split("/")[0])-1
                s = str(y)+"/"+str(y+1)
                stage_predictions = league.get_stages_by_season(s)
            else:
                stage_predictions -= 1

        h = []
        for team_api_id, accuracy in best_teams.items():
            heapq.heappush(h, (accuracy, team_api_id))

        top_k = [Team.read_by_team_api_id(team_api_id) for a, team_api_id
                 in heapq.nlargest(n_teams_returned, h, lambda x: x[0])[:n_teams_returned]]
        return top_k
示例#13
0
    def get_training_ranking(self,
                             season,
                             stage_to_predict,
                             stages_to_train,
                             home=None):
        """

        :param season:
        :param stage_to_predict:
        :param stages_to_train:
        :param home:
        :return:
        """
        import src.application.Domain.Team as Team

        matches = self.get_training_matches(season, stage_to_predict,
                                            stages_to_train)
        teams = self.get_teams(season=season)
        ranking = {team.id: 0 for team in teams}
        num_matches = {team.id: 0 for team in teams}
        for m in matches:
            try:
                result, winner = m.get_winner()
                if result == 0:
                    winner_id = None
                else:
                    winner_id = winner.id

                home_id = m.get_home_team().id
                away_id = m.get_away_team().id
            except AttributeError:
                continue

            if util.is_None(home):
                # total ranking
                util.increase_dict_entry(home_id, num_matches)
                util.increase_dict_entry(away_id, num_matches)

                if result != 0:
                    util.increase_dict_entry(winner_id, ranking, 3)
                else:
                    util.increase_dict_entry(home_id, ranking, 1)
                    util.increase_dict_entry(away_id, ranking, 1)

            elif home:
                # home ranking
                util.increase_dict_entry(home_id, num_matches)
                if result == 1:
                    util.increase_dict_entry(winner_id, ranking, 3)
                elif result == 0:
                    util.increase_dict_entry(home_id, ranking, 1)

            else:
                # away ranking
                util.increase_dict_entry(away_id, num_matches)
                if result == 2:
                    util.increase_dict_entry(winner_id, ranking, 3)
                elif result == 0:
                    util.increase_dict_entry(away_id, ranking, 1)

        # divide the overall point by the number of matches done
        norm_ranking = dict()
        for team_id, points in ranking.items():
            if num_matches[team_id] > 0:
                norm_ranking[team_id] = points / num_matches[team_id]
            else:
                norm_ranking[team_id] = 0

        # order the normalized rank
        ranking_ret = []
        for team, p in sorted(norm_ranking.items(),
                              key=operator.itemgetter(1))[::-1]:
            ranking_ret.append((p, Team.read_by_id(team)))

        return ranking_ret