Пример #1
0
    def update_player_prices(self, queue=None, console="PS4"):
        """
        Gets updated price information for all the players on the list/db
        Input: None
        Output: None  -  the player price information is updated
        """

        # Iterate through player list and get updated prices
        total_players = len(self.db)
        for idx, player_info in enumerate(self.db):
            player = Player(player_info)
            self.db[idx]["price"] = player.get_price(console)

            # Display number of pages completed
            if queue is not None:
                queue.put((idx + 1, total_players))
            print "%d of %d players updated" % (idx + 1, total_players)
Пример #2
0
    def download(self, queue=None, get_prices=True, console="PLAYSTATION"):
        """
        Download the database from the EA FIFA website
        Input: None
        Output: The downloaded database which is also already saved in the object
        """

        url_db_start = 'https://www.easports.com/fifa/ultimate-team/api/fut/item?jsonParamObject={"page":"'
        url_db_end = '"}'
        del self.db[:]  # Empty existing DB list
        players_found = []

        try:
            # Create session
            sess = requests.Session()

            # Get total number of pages of data
            page_data = sess.get("%s1%s" % (url_db_start, url_db_end), verify=False).content  # Get first page data
            total_pages = json.loads(page_data)["totalPages"]  # Get total number of pages

            # Iterate through all pages
            for page_num in range(1, total_pages + 1):
                while True:
                    try:
                        # Get first page data
                        page_data = json.loads(sess.get("%s%s%s" % (url_db_start, page_num, url_db_end)).content)
                        break
                    except Exception:
                        print "JSON Loading Error. Retrying Load."

                # Get the number of players on the page
                total_players = page_data["count"]

                # Iterate through players on page
                for player_num in range(total_players):
                    # Get player data and create player
                    player_data = page_data["items"][player_num]
                    temp_player = Player(player_data)

                    # If player is a legend and console is PS4, skip
                    if console == "PLAYSTATION" and temp_player.playerType.lower() == "legend":
                        continue

                    # Add player to database and avoid duplicates
                    if players_found.count(temp_player.id) == 0:
                        if get_prices:
                            temp_player.get_price(console)
                        self.db.append(temp_player.__dict__)
                        players_found.append(Player(player_data).id)

                # Display number of pages completed
                if queue is not None:
                    queue.put((page_num, total_pages))
                print "%d of %d pages downloaded" % (page_num, total_pages)

        except requests.ConnectionError:
            print "Not connected to Internet. Cannot download player db."
            queue.put(("Not connected to Internet. Cannot download player db.",))

        except Exception as err:
            print err.__str__()
            queue.put((err.__str__(),))

        return self.db