예제 #1
0
    def item_lookup(self, item_ids):
        """
        Lookup of the item with the given id on Amazon. Returns it values or None if something went wrong.
        :param item_ids: the item ids
        :type item_ids: list
        :return: the values of the item
        :rtype: dict
        """
        logger.info('starting lookup for ASINs %s', ', '.join(item_ids))
        item_response = self.lookup_at_amazon(item_ids)

        if getattr(item_response, 'items') is None:
            logger.error(
                'Request for item lookup (ResponseGroup: %s, ASINs: %s) returned nothing',
                app_settings.PRICE_MONITOR_PA_RESPONSE_GROUP,
                ', '.join(item_ids),
            )
            return dict()

        if item_response.items.request.isvalid.string == 'True':

            # the dict that will contain a key for every ASIN and as value the parsed values
            product_values = dict()

            for item_node in item_response.find_all(['item']):

                # parse the values
                item_values = {
                    'asin': item_node.asin.string,
                    'title': item_node.itemattributes.title.string,
                    'artist': item_node.itemattributes.artist.string if item_node.itemattributes.artist is not None else None,
                    'isbn': self.__get_item_attribute(item_node, 'isbn'),
                    'eisbn': self.__get_item_attribute(item_node, 'eisbn'),
                    'binding': item_node.itemattributes.binding.string,
                    'date_publication': self.format_datetime(self.__get_item_attribute(item_node, 'publicationdate')),
                    'date_release': self.format_datetime(self.__get_item_attribute(item_node, 'releasedate')),
                    'large_image_url': item_node.largeimage.url.string if item_node.largeimage.url is not None else None,
                    'medium_image_url': item_node.mediumimage.url.string if item_node.mediumimage.url is not None else None,
                    'small_image_url': item_node.smallimage.url.string if item_node.smallimage.url is not None else None,
                    'offer_url': utils.get_offer_url(item_node.asin.string),
                    'audience_rating': self.__get_item_attribute(item_node, 'audiencerating'),
                }

                # check if there are offers, if so add price
                if item_node.offers is not None and int(item_node.offers.totaloffers.string) > 0:
                    item_values['price'] = float(int(item_node.offers.offer.offerlisting.price.amount.string) / 100)
                    item_values['currency'] = item_node.offers.offer.offerlisting.price.currencycode.string

                # insert into main dict
                product_values[item_values['asin']] = item_values

            # check if all ASINs are included, if not write error message to log
            failed_asins = []
            for asin in item_ids:
                if asin not in product_values.keys():
                    failed_asins.append(asin)

            if failed_asins:
                logger.error('Lookup for the following ASINs failed: %s', ', '.join(failed_asins))

            # if there is at least a single ASIN in the list, return the list, else None
            return dict() if len(product_values) == 0 else product_values

        else:
            logger.error(
                'Request for item lookup (ResponseGroup: %s, ASINs: %s) was not valid',
                app_settings.PRICE_MONITOR_PA_RESPONSE_GROUP,
                ', '.join(item_ids),
            )
            return dict()
예제 #2
0
    def item_lookup(self, item_ids):
        """
        Lookup of the item with the given id on Amazon. Returns it values or None if something went wrong.
        :param item_ids: the item ids
        :type item_ids: list
        :return: the values of the item
        :rtype: dict
        """
        logger.info('starting lookup for ASINs %s', ', '.join(item_ids))
        item_response = self.lookup_at_amazon(item_ids)

        if getattr(item_response, 'items') is None:
            logger.error(
                'Request for item lookup (ResponseGroup: %s, ASINs: %s) returned nothing',
                app_settings.PRICE_MONITOR_PA_RESPONSE_GROUP,
                ', '.join(item_ids),
            )
            return dict()

        if item_response.items.request.isvalid.string == 'True':

            # the dict that will contain a key for every ASIN and as value the parsed values
            product_values = dict()

            for item_node in item_response.find_all(['item']):

                # parse the values
                try:
                    isbn = self.__get_item_attribute(item_node, 'isbn')
                    eisbn = self.__get_item_attribute(item_node, 'eisbn')
                    if eisbn is None and isbn is not None:
                        if len(isbn) == 13:
                            eisbn = isbn
                            isbn = None

                    item_values = {
                        'asin':
                        item_node.asin.string,
                        'title':
                        item_node.itemattributes.title.string,
                        'artist':
                        item_node.itemattributes.artist.string if
                        item_node.itemattributes.artist is not None else None,
                        'isbn':
                        isbn,
                        'eisbn':
                        eisbn,
                        'binding':
                        item_node.itemattributes.binding.string,
                        'date_publication':
                        self.format_datetime(
                            self.__get_item_attribute(item_node,
                                                      'publicationdate')),
                        'date_release':
                        self.format_datetime(
                            self.__get_item_attribute(item_node,
                                                      'releasedate')),
                        'large_image_url':
                        item_node.largeimage.url.string
                        if item_node.largeimage.url is not None else None,
                        'medium_image_url':
                        item_node.mediumimage.url.string
                        if item_node.mediumimage.url is not None else None,
                        'small_image_url':
                        item_node.smallimage.url.string
                        if item_node.smallimage.url is not None else None,
                        'offer_url':
                        utils.get_offer_url(item_node.asin.string),
                        'audience_rating':
                        self.__get_item_attribute(item_node, 'audiencerating'),
                    }

                    # check if there are offers, if so add price
                    if item_node.offers is not None and int(
                            item_node.offers.totaloffers.string) > 0:
                        item_values['price'] = float(
                            int(item_node.offers.offer.offerlisting.price.
                                amount.string) / 100)
                        item_values[
                            'currency'] = item_node.offers.offer.offerlisting.price.currencycode.string

                    # insert into main dict
                    product_values[item_values['asin']] = item_values
                except AttributeError:
                    raise
                    logger.error(
                        'fetching item values from returned XML for ASIN %s failed',
                        item_node.asin)

            # check if all ASINs are included, if not write error message to log
            failed_asins = []
            for asin in item_ids:
                if asin not in product_values.keys():
                    failed_asins.append(asin)

            if failed_asins:
                logger.error('Lookup for the following ASINs failed: %s',
                             ', '.join(failed_asins))

            # if there is at least a single ASIN in the list, return the list, else None
            return dict() if len(product_values) == 0 else product_values

        else:
            logger.error(
                'Request for item lookup (ResponseGroup: %s, ASINs: %s) was not valid',
                app_settings.PRICE_MONITOR_PA_RESPONSE_GROUP,
                ', '.join(item_ids),
            )
            return dict()
예제 #3
0
 def test_get_offer_url(self):
     self.assertEqual('http://www.amazon.de/dp/X1234567890/?tag=sample-assoc-tag', utils.get_offer_url('X1234567890'))
예제 #4
0
 def test_get_offer_url(self):
     """Test the offer url function"""
     self.assertEqual('http://www.amazon.de/dp/X1234567890/?tag=sample-assoc-tag', utils.get_offer_url('X1234567890'))