def __get_recent_trophies(self) -> List[Trophy]:
        if not self.__soup_profile.select("ul#recent-trophies"):
            return []

        return list(
            map(
                lambda trophy: Trophy.create_from_alternative_soup(
                    BeautifulSoupFactory.create_from_string(str(trophy))),
                self.__soup_profile.select("ul#recent-trophies")[0].find_all(
                    'li')))
    def __get_level_history(self) -> List[Level]:
        if not self.__soup_level_history.select("table.box.zebra"):
            self.__progress_bar.next()
            return []

        return list(
            map(
                lambda level: Level.create_from_soup(
                    BeautifulSoupFactory.create_from_string(str(level))),
                self.__soup_level_history.select("table.box.zebra")
                [0].find_all("tr")))
예제 #3
0
    def populate_details_from_soup(self, soup: BeautifulSoup):
        self.cover_uri = soup.select(
            "div.game-image-holder picture.game img")[0]["src"] if soup.select(
                "div.game-image-holder picture.game img") else ""
        self.background_uri = re.findall(
            r'url\(([^)]+)\)',
            soup.select("div#banner div.img")[1]["style"])[0] if soup.select(
                "div#banner div.img") else ""
        self.developers = soup.find("td", text=re.compile(
            "Developer.*")).find_next().text.split(', ') if soup.find(
                "td", text=re.compile("Developer.*")) else ""
        self.publishers = soup.find("td", text=re.compile(
            "Publisher.*")).find_next().text.split(', ') if soup.find(
                "td", text=re.compile("Publisher.*")) else ""
        self.genres = soup.find("td", text=re.compile(
            "Genre.*")).find_next().text.split(', ') if soup.find(
                "td", text=re.compile("Genre.*")) else ""
        self.themes = soup.find("td", text=re.compile(
            "Theme.*")).find_next().text.split(', ') if soup.find(
                "td", text=re.compile("Theme.*")) else ""
        self.modes = soup.find("td", text=re.compile(
            "Mode.*")).find_next().text.split(', ') if soup.find(
                "td", text=re.compile("Mode.*")) else ""

        self.trophy_count["platinum"] = soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.platinum"
        )[0].text if soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.platinum") else ""
        self.trophy_count["gold"] = soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.gold"
        )[0].text if soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.gold") else ""
        self.trophy_count["silver"] = soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.silver"
        )[0].text if soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.silver") else ""
        self.trophy_count["bronze"] = soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.bronze"
        )[0].text if soup.select(
            "div.col-xs-4 div.trophy-count li.icon-sprite.bronze") else ""

        if not soup.select("div.col-xs div.box.no-top-border table"):
            return

        # Append all trophy info to game.
        for table in soup.select("div.col-xs div.box.no-top-border table"):
            for row in table.find_all('tr'):
                if len(row.find_all('td')) < 6:
                    continue

                trophy = Trophy.create_from_game_detail_soup(
                    BeautifulSoupFactory.create_from_string(str(row)))
                self.trophies.append(trophy)
    def __get_milestones(self) -> List[Milestone]:
        if not self.__soup_profile.find("h3", text="Trophy Milestones"):
            self.__progress_bar.next()
            return []

        milestones_table = self.__soup_profile.find(
            "h3", text="Trophy Milestones").find_next()

        return list(
            map(
                lambda milestone: Milestone.create_from_soup(
                    BeautifulSoupFactory.create_from_string(str(milestone))),
                milestones_table.find_all("tr")))
    def __get_trophy_cabinet(self) -> List[Trophy]:
        if not self.__soup_profile.find("h3", text="Trophy Cabinet"):
            self.__progress_bar.next()
            return []

        if not self.__soup_profile.select("div.sidebar table.box.zebra"):
            self.__progress_bar.next()
            return []

        return list(
            map(
                lambda trophy: Trophy.create_from_soup(
                    BeautifulSoupFactory.create_from_string(str(trophy))),
                self.__soup_profile.select("div.sidebar table.box.zebra")
                [0].find_all("tr")))
    def __get_games(self) -> List[Game]:
        if not self.__soup_games.find("tr"):
            return []

        games = []
        for game in self.__soup_games.find_all("tr"):
            if not game.find("div", class_="small-info"):
                continue

            if len(game.find_all("div", class_="small-info")) != 2:
                continue

            games.append(
                Game.create_from_soup(
                    BeautifulSoupFactory.create_from_string(str(game))))

        return games
    def __get_rarest_trophies(self) -> List[Trophy]:
        if not self.__soup_profile.find("h3", text="Rarest Trophies"):
            self.__progress_bar.next()
            return []

        if not self.__soup_profile.select(
                'div.sidebar div.box.no-top-border table'):
            self.__progress_bar.next()
            return []

        return list(
            map(
                lambda trophy: Trophy.create_from_soup(
                    BeautifulSoupFactory.create_from_string(str(trophy))),
                self.__soup_profile.select(
                    'div.sidebar div.box.no-top-border table')[0].find_all(
                        "tr")))
    def get_profile(self, psn_name: str, detailed: bool) -> Profile:
        self.__progress_bar.update_message("Initializing")
        self.__psn_name = psn_name

        self.__soup_games = BeautifulSoupFactory.create_for_games(
            self.__psn_name)
        games = self.__get_games()

        if detailed:
            # Update max of progress bar and fetch each game separately.
            self.__progress_bar.max = self.__progress_bar.max + len(games)
            for game in games:
                self.__progress_bar.update_message("Fetching " + game.title)
                if not game.uri:
                    self.__progress_bar.next()
                    continue

                # Update game instance and populate with details.
                game.populate_details_from_soup(
                    BeautifulSoupFactory.create_from_link(game.uri))
                self.__progress_bar.next()

        # Fetch profile data.
        self.__progress_bar.update_message("Fetching profile")
        self.__soup_profile = BeautifulSoupFactory.create_for_profile(
            self.__psn_name)
        self.__progress_bar.next()

        self.__soup_level_history = BeautifulSoupFactory.create_for_level_history(
            self.__psn_name)
        self.__progress_bar.next()

        self.__soup_stats = BeautifulSoupFactory.create_for_stats(
            self.__psn_name)
        self.__progress_bar.next()

        country = ""
        if self.__soup_profile.select("img#bar-country"):
            country = BeautifulSoupFactory.create_from_string(
                self.__soup_profile.select("img#bar-country")[0]["title"]).text

        # Extract profile summary.
        profile_summary = ProfileSummary.create_from_soup(
            BeautifulSoupFactory.create_from_string(
                str(self.__soup_profile) + str(self.__soup_stats)))
        self.__progress_bar.next()

        # Extract recent trophies.
        recent_trophies = self.__get_recent_trophies()
        self.__progress_bar.next()

        # Extract rarest trophies.
        rarest_trophies = self.__get_rarest_trophies()
        self.__progress_bar.next()

        # Extract milestones.
        milestones = self.__get_milestones()
        self.__progress_bar.next()

        # Extract trophy cabinet.
        trophy_cabinet = self.__get_trophy_cabinet()
        self.__progress_bar.next()

        # Extract level history.
        level_history = self.__get_level_history()
        self.__progress_bar.next()

        profile = Profile(
            self.__psn_name, country,
            self.__soup_profile.select("div.avatar img")[0]["src"]
            if self.__soup_profile.select("div.avatar img") else "",
            profile_summary, recent_trophies, rarest_trophies, milestones,
            games, trophy_cabinet, level_history)

        self.__progress_bar.update_message("Complete")
        self.__progress_bar.finish()
        return profile