def __init__(self, usr, owner, item_id='', price=''): """ Loads the shops inventory and grabs main item if necessary The initialization of this class takes multiple parameters for different scenarios. If a shop needs to be loaded (including all shop pages) then the `usr` and `owner` arguments should only be supplied. If a specific item in the shop is being sought after (like from a shop wizard result) then the item id and price should also be supplied. In the latter case only the first page of the user shop is loaded and if the item is found it will be appended to the very beginning of the inventory list. Arguments: | **usr**: The :class:`User` instance to use | **owner**: The username of the owner of the shop | **item_id**: The item id to search for | **price**: The price of the item to search for """ super().__init__(usr) # Load the appropriate page if item_id and price: pg = self._get_page('shop_item', (owner, item_id, str(price))) else: pg = self._get_page('shop', owner) # Get the details try: self.name = str(self._xpath('name', pg)[0]) self.keeper_img = self._xpath('keeper_img', pg)[0] self.keeper_name = str(self._xpath('keeper_text', pg)[0].split(' says')[0]) self.keeper_message = str(self._xpath('keeper_text', pg)[0].split('says ')[1].replace('\'', '')) except Exception: self._logger.exception('Failed to parse user front shop details') raise ParseException('Failed to parse user front shop details') # Load the inventory self.inventory = USFrontInventory(self._usr) if item_id and price: self.inventory.load(owner, pg) else: self.inventory.load(owner, pg, True) # If searching for a specific item, find it if item_id and price: try: td = self._xpath('main_item', pg)[0] item = USFrontItem('', self._usr) item.url = td.xpath('./a/@href')[0] item.name = str(td.xpath('./b/text()')[0]) item.stock = int(td.xpath('./text()[3]')[0].replace(' in stock', '')) item.price = int(self._remove_multi(td.xpath('./text()[4]')[0], [',', ' NP', 'Cost : '])) # Add it to the beginning of the inventory self.inventory.data = [item] + self.inventory.data except Exception: # This most likely means the item has already been bought pass
def _parse_page(self, pg): # Loop through rows of items for row in self._xpath('rows', pg): # Each td is an item for td in self._xpath('tds', row): item = USFrontItem('', self._usr) item.url = td.xpath('./a/@href')[0] item.name = str(td.xpath('./b/text()')[0]) item.stock = int(td.xpath('./text()[3]')[0].replace(' in stock', '')) item.price = int(self._remove_multi(td.xpath('./text()[4]')[0], [',', ' NP', 'Cost : '])) self.data.append(item)
def _parse_page(self, pg): # Loop through rows of items for row in self._xpath('rows', pg): # Each td is an item for td in self._xpath('tds', row): item = USFrontItem('', self._usr) item.url = td.xpath('./a/@href')[0] item.name = str(td.xpath('./b/text()')[0]) item.stock = int( td.xpath('./text()[3]')[0].replace(' in stock', '')) item.price = int( self._remove_multi( td.xpath('./text()[4]')[0], [',', ' NP', 'Cost : '])) self.data.append(item)
def __init__(self, usr, owner, item_id='', price=''): """ Loads the shops inventory and grabs main item if necessary The initialization of this class takes multiple parameters for different scenarios. If a shop needs to be loaded (including all shop pages) then the `usr` and `owner` arguments should only be supplied. If a specific item in the shop is being sought after (like from a shop wizard result) then the item id and price should also be supplied. In the latter case only the first page of the user shop is loaded and if the item is found it will be appended to the very beginning of the inventory list. Arguments: | **usr**: The :class:`User` instance to use | **owner**: The username of the owner of the shop | **item_id**: The item id to search for | **price**: The price of the item to search for """ super().__init__(usr) # Load the appropriate page if item_id and price: pg = self._get_page('shop_item', (owner, item_id, str(price))) else: pg = self._get_page('shop', owner) # Get the details try: self.name = str(self._xpath('name', pg)[0]) self.keeper_img = self._xpath('keeper_img', pg)[0] self.keeper_name = str( self._xpath('keeper_text', pg)[0].split(' says')[0]) self.keeper_message = str( self._xpath('keeper_text', pg)[0].split('says ')[1].replace('\'', '')) except Exception: self._logger.exception('Failed to parse user front shop details') raise ParseException('Failed to parse user front shop details') # Load the inventory self.inventory = USFrontInventory(self._usr) if item_id and price: self.inventory.load(owner, pg) else: self.inventory.load(owner, pg, True) # If searching for a specific item, find it if item_id and price: try: td = self._xpath('main_item', pg)[0] item = USFrontItem('', self._usr) item.url = td.xpath('./a/@href')[0] item.name = str(td.xpath('./b/text()')[0]) item.stock = int( td.xpath('./text()[3]')[0].replace(' in stock', '')) item.price = int( self._remove_multi( td.xpath('./text()[4]')[0], [',', ' NP', 'Cost : '])) # Add it to the beginning of the inventory self.inventory.data = [item] + self.inventory.data except Exception: # This most likely means the item has already been bought pass