コード例 #1
0
class AmazonScraper(object):
    def __init__(self, access_key, secret_key, associate_tag, *args, **kwargs):
        self.api = AmazonAPI(access_key, secret_key, associate_tag, *args, **kwargs)

    def reviews(self, ItemId=None, URL=None):
        return Reviews(self, ItemId, URL)

    def review(self, Id=None, URL=None):
        return Review(self, Id, URL)

    def lookup(self, URL=None, **kwargs):
        if URL:
            kwargs['ItemId'] = extract_asin(URL)

        result = self.api.lookup(**kwargs)
        if isinstance(result, (list, tuple)):
            result = [Product(p) for p in result]
        else:
            result = Product(result)
        return result

    def similarity_lookup(self, **kwargs):
        for p in self.api.similarity_lookup(**kwargs):
            yield Product(p)

    def browse_node_lookup(self, **kwargs):
        return self.api.browse_node_lookup(**kwargs)

    def search(self, **kwargs):
        for p in self.api.search(**kwargs):
            yield Product(p)

    def search_n(self, n, **kwargs):
        for p in self.api.search_n(n, **kwargs):
            yield Product(p)
コード例 #2
0
ファイル: scrape.py プロジェクト: jasonb5/cins370
def generate_csv():
  # initialize amazon api with access key, secret key, and associate tag
  amazon = AmazonAPI('AKIAJPT5M67Z5DB6R3XA', 'P0ekhRiDVDC2xeJa4fZz1P5qHY/B2Qig71G6wZB3', 'thedeepdark-20')

  print("Querying amazon API")
  # returns available book subjects
  subjects = amazon.browse_node_lookup(BrowseNodeId=1000)

  f = open('data.csv', 'wb')
  writer = csv.writer(f)

  print("\tReturned with " + str(len(subjects[0].children)) + " subjects")

  # creates books and author lists
  for subject in subjects:
    for genre in subject.children:
      # skip calendar entries
      if genre.name.text == 'Calendars': continue

      # returns first 1000 entries in each subject
      # Amazons api limits the number of return pages to 10
      # with 10 items on each for a maximum of 100 items 
      books = amazon.search_n(100, Condition='All', BrowseNode=genre.id, SearchIndex='Books', MaxQPS=0.9)

      print("Queried " + genre.name + ", returned " + str(len(books)) + " books")

      failed = 0

      for book in books:
        b_isbn = book.isbn
        b_title = book.title
        b_pub_date = str(book.publication_date)
        b_genre = genre.name
        b_publisher = book.publisher
        b_list_price = book.list_price[0]
        b_price = book.price_and_currency[0]

        if len(book.authors) == 0:
          break

        book_item = [b_isbn, b_title, book.authors[0], b_pub_date, b_publisher, b_genre, b_list_price, b_price]

        for x in range(len(book_item)):
         if isinstance(book_item[x], str):
           book_item[x] = unicode(book_item[x], 'utf-8')
  
        try:
          writer.writerow(book_item)
        except UnicodeEncodeError:
          failed += 1

      print("\tDone processing books, failed to convert unicode characters " + str(failed) + " times")

      time.sleep(5)
コード例 #3
0
class AmazonScraper(object):
    def __init__(self, access_key, secret_key, associate_tag, *args, **kwargs):
        self.api = AmazonAPI(access_key, secret_key, associate_tag, *args, **kwargs)

    def reviews(self, ItemId=None, URL=None):
        return Reviews(self, ItemId, URL)

    def review(self, Id=None, URL=None):
        return Review(self, Id, URL)

    def reviewer(self, url):
        return Reviewer(url)

    def lookup(self, URL=None, **kwargs):
        if URL:
            kwargs['ItemId'] = extract_asin(URL)

        result = self.api.lookup(**kwargs)
        if isinstance(result, (list, tuple)):
            result = [Product(p) for p in result]
        else:
            result = Product(result)
        return result

    def similarity_lookup(self, **kwargs):
        for p in self.api.similarity_lookup(**kwargs):
            yield Product(p)

    def browse_node_lookup(self, **kwargs):
        return self.api.browse_node_lookup(**kwargs)

    def search(self, **kwargs):
        for p in self.api.search(**kwargs):
            yield Product(p)

    def search_n(self, n, **kwargs):
        for p in self.api.search_n(n, **kwargs):
            yield Product(p)
コード例 #4
0
ファイル: allPythonContent.py プロジェクト: Mondego/pyreco
class TestAmazonApi(TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """
    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B007HCCNJU")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0814916017775')
        assert_equals(
            product.large_image_url,
            'http://ecx.images-amazon.com/images/I/41VZlVs8agL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '650', 'ItemDimensions.Height': '130'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_batch_lookup(self):
        """Test Batch Product Lookup.

        Tests that a batch product lookup request returns multiple results.
        """
        asins = ['B00AWH595M', 'B007HCCNJU', 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), 5)
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId="B0051QVF7A")
        assert_true(len(products) > 5)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 1)

    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B000FBJCJE")
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'From the opening line'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)
コード例 #5
0
class TestAmazonApi(TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """
    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                                AMAZON_ASSOC_TAG)

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B007HCCNJU")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0814916017775')
        assert_equals(product.large_image_url,
                      'http://ecx.images-amazon.com/images/I/41VZlVs8agL.jpg')
        assert_equals(product.get_attribute('Publisher'), 'Amazon')
        assert_equals(
            product.get_attributes(
                ['ItemDimensions.Width', 'ItemDimensions.Height']), {
                    'ItemDimensions.Width': '650',
                    'ItemDimensions.Height': '130'
                })
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_batch_lookup(self):
        """Test Batch Product Lookup.

        Tests that a batch product lookup request returns multiple results.
        """
        asins = [
            'B0051QVESA', 'B005DOK8NW', 'B005890G8Y', 'B0051VVOB2',
            'B005890G8O'
        ]
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), 5)
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(1,
                                        Keywords='kindle',
                                        SearchIndex='All')
        assert_equals(len(products), 1)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG)
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(AMAZON_ACCESS_KEY,
                           AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG,
                           region="UK")
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId="B0051QVF7A")
        assert_equals(len(products), 10)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)
コード例 #6
0
class GearTypeRetriever:
    """ Initialize retriever class with Amazon access configs. """
    def __init__(self, amazon_config, bot_db_configs, app_db_configs):
        self.logger = logging.getLogger('gearstack_rbt.{0}'.format(
            GearTypeRetriever.__name__))
        self.amazon_config = amazon_config
        self.geartype_retrieved_dao = GearTypeRetrievedDAO(
            bot_db_configs=bot_db_configs)
        self.geartype_dao = GearTypeDAO(app_db_configs=app_db_configs)
        self.amazon = AmazonAPI(
            aws_key=amazon_config['api_access_key'],
            aws_secret=amazon_config['api_secret_key'],
            aws_associate_tag=amazon_config['associate_tag'])
        self.cats_to_ignore = config_utils.fetch_data_config('ignore_cats')

    """ Uses Amazon PAAPI to perform a node lookup. """

    def node_lookup(self, node_id, retry_cnt=0):
        # Initialize amazon api instance to use
        self.logger.debug(
            'Performing node lookup for node id {0}'.format(node_id))
        try:
            result = self.amazon.browse_node_lookup(
                ResponseGroup="BrowseNodeInfo", BrowseNodeId=node_id)
            return result[0]
        except IndexError:
            return None
        except urllib2.HTTPError as http_err:
            if http_err.code == 503:
                # This exception indicates that I might be exceeding Amazon's request limit.
                # Need to execute some re-try logic here.
                if (retry_cnt < 5):
                    # Try one second retry a few times
                    self.logger.warning(
                        'Request limit. Amazon is throttling requests. '
                        'Retry count: {0}'.format(retry_cnt))
                    threading.Timer(
                        1.0,
                        self.node_lookup(node_id=node_id,
                                         retry_cnt=retry_cnt + 1))
                elif (retry_cnt >= 5 and retry_cnt < 10):
                    # If one second retries don't work, try 10 second retries a few times
                    self.logger.warning(
                        'Request limit. Amazon is throttling requests. '
                        'Retry count: {0}'.format(retry_cnt))
                    threading.Timer(10.0,
                                    self.node_lookup(node_id, retry_cnt + 1))
                else:
                    # If none of previous retries work, stop trying and raise error
                    self.logger.exception(
                        'Maximum retry count reached while attempting to lookup category!'
                    )
                    raise http_err
            else:
                self.logger.exception(
                    'Unexpected HTTP error encountered during category lookup!'
                )
                raise http_err

    """ Performs node lookup for each node in arg nodes. Then saves
        node and its ancestors to the database. Node_ids comes from
        the list of node_ids in data.yml config file. Function also
        saves GearType instances to gear_type table in gearstack app
        database if they don't already exist in gear_type_retrieved
        table in bot_database. Bot database tables exist so that app
        database tables don't need to be searched to determine if a 
        gear type already exists. """

    def save_geartypes(self, node_ids):
        for node_id in node_ids:
            if not self.geartype_retrieved_dao.fetch_geartype_retrieved_by_id(
                    node_id=node_id):
                # Lookup node
                node = self.node_lookup(node_id)

                if node:
                    # Add current node and all ancestors to nodes_to_persist
                    nodes_to_persist = []
                    nodes_to_persist.append(node)
                    nodes_to_persist.extend(node.ancestors)

                    # Reverse nodes_to_persist list to start with root node
                    nodes_to_persist.reverse()
                    for n in nodes_to_persist:
                        if n.id not in self.cats_to_ignore:
                            if not self.geartype_retrieved_dao.fetch_geartype_retrieved_by_id(
                                    node_id=n.id):
                                # First save GearType to app database
                                gear_type = GearType(
                                    amazon_node_id=n.id,
                                    name=str(n.name),
                                    parent_node_id=n.ancestor.id
                                    if n.ancestor and n.ancestor.id
                                    not in self.cats_to_ignore else None,
                                    is_leaf_node=True
                                    if n.id == node.id else False)
                                ret_type_id = self.geartype_dao.insert_geartype(
                                    gear_type=gear_type)

                                if ret_type_id is not None:
                                    # Log success
                                    self.logger.debug(
                                        'Saved gear type: {0} with id {1} to db!'
                                        .format(gear_type.name, ret_type_id))

                                    # Now save GearTypeRetrieved instance to bot database if GearType save
                                    # was successful
                                    gear_type_retrieved = GearTypeRetrieved(
                                        amazon_node_id=n.id)
                                    self.geartype_retrieved_dao.insert_geartype_retrieved(
                                        gear_type_retrieved=gear_type_retrieved
                                    )
                                    self.logger.debug(
                                        'Saved node {0} to db!'.format(
                                            str(n.id)))
                                else:
                                    # Log gear type save failure
                                    self.logger.debug(
                                        'Save unsuccessful for '
                                        'gear type {0} with id {1}!'.format(
                                            gear_type.name,
                                            gear_type.amazon_node_id))
                else:
                    self.logger.warning(
                        'Could not find node {0} via look up!'.format(node_id))
コード例 #7
0
    def getProducts(self, request, format=None):
        amazon = AmazonAPI(settings.AMAZON_ACCESS_KEY,
                           settings.AMAZON_SECRET_KEY,
                           settings.AMAZON_ASSOC_TAG)
        data = {'result': []}
        tags = request.GET.get('tags', '')
        range = request.GET.get('range', '')

        for tag in tags.split(','):
            if range == 'low':
                print("range", range)

                products = amazon.search(Keywords='{} shirt'.format(tag),
                                         SearchIndex='Apparel',
                                         BrowseNode="9056987011",
                                         ResponseGroup='Large',
                                         Sort='salesrank')

                nodes = amazon.browse_node_lookup(ResponseGroup='TopSellers',
                                                  BrowseNodeId='9103696011')
                for n in nodes:
                    print(n)

            else:
                products = amazon.search(Keywords='{} shirt'.format(tag),
                                         SearchIndex='Apparel',
                                         BrowseNode="9103696011",
                                         ResponseGroup='Large')

            count = 0

            for it in products.iterate_pages():

                time.sleep(1)

                for product in products:
                    count += 1
                    print("Getting product", count, product.asin,
                          product.sales_rank)
                    nodes = []
                    product_type = 2

                    if not product.sales_rank:
                        continue

                    if range == 'low' and int(product.sales_rank) > 100000:
                        continue

                    if range == 'high' and product.sales_rank < 100000:
                        continue

                    for node in product.browse_nodes:
                        for n in node.ancestors:
                            nodes.append(n.name)
                        nodes.append(node.name)
                        if node.name == "Women" or "women" in product.title.lower(
                        ) or "woman" in product.title.lower():
                            product_type = 1
                        elif node.name == "Men" or "men" in product.title.lower(
                        ) or "man" in product.title.lower():
                            product_type = 0

                    if "Novelty" in nodes:
                        pass
                    else:
                        continue

                    data['result'].append({
                        'title':
                        product.title,
                        'sales_rank':
                        product.sales_rank,
                        'monthly_sales_estimate':
                        monthly_sales_estimate(product.sales_rank),
                        'asin':
                        product.asin,
                        'small_image_url':
                        product.large_image_url,
                        'reviews':
                        product.reviews,
                        'detail_page_url':
                        product.detail_page_url,
                        'features':
                        product.features,
                        'type':
                        product_type
                    })

            amazon_products.after_response(data)

        return data
コード例 #8
0
class TestAmazonApi(unittest.TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """

    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            CacheReader=cache_reader,
            CacheWriter=cache_writer,
            MaxQPS=0.5
        )

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B00I15SB16")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0848719039726')
        assert_equals(
            product.large_image_url,
            'http://ecx.images-amazon.com/images/I/51XGerXeYeL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '469', 'ItemDimensions.Height': '40'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_lookup_nonexistent_asin(self):
        """Test Product Lookup with a nonexistent ASIN.

        Tests that a product lookup for a nonexistent ASIN raises AsinNotFound.
        """
        assert_raises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")

    def test_bulk_lookup(self):
        """Test Baulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_lookup_bulk(self):
        """Test Bulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_lookup_bulk_empty(self):
        """Test Bulk Product Lookup With No Results.

        Tests that a bulk product lookup request with no results
        returns an empty list.
        """
        asins = ['not-an-asin', 'als-not-an-asin']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(type(products), list)
        assert_equals(len(products), 0)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    def test_search_no_results(self):
        """Test Product Search with no results.

        Tests that a product search with that returns no results throws a
        SearchException.
        """
        products = self.amazon.search(Title='no-such-thing-on-amazon',
                                      SearchIndex='Automotive')
        assert_raises(SearchException, (x for x in products).next)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId=TEST_ASIN)
        assert_true(len(products) > 5)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId=TEST_ASIN)
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 2)

    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B000FBJCJE")
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'From the opening line'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_region(self):
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG)
        assert_equals(amazon.region, 'US')

        # old 'region' parameter
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, region='UK')
        assert_equals(amazon.region, 'UK')

        # kwargs method
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, Region='UK')
        assert_equals(amazon.region, 'UK')

    def test_kwargs(self):
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, MaxQPS=0.7)

    def test_images(self):
        """Test images property

        Test that the images property has a value when using the
        Images ResponseGroup
        """
        product = self.amazon.lookup(ResponseGroup='Images',
                                     ItemId='B00TSVVNQC')
        assert_equals(type(product.images), list)
        assert_equals(len(product.images), 7)
コード例 #9
0
class TestAmazonApi(unittest.TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """

    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG,
            CacheReader=cache_reader,
            CacheWriter=cache_writer,
            MaxQPS=0.5
        )

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0848719083774')
        assert_equals(
            product.large_image_url,
            'https://images-na.ssl-images-amazon.com/images/I/51hrdzXLUHL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '450', 'ItemDimensions.Height': '36'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_nonexistent_asin(self):
        """Test Product Lookup with a nonexistent ASIN.

        Tests that a product lookup for a nonexistent ASIN raises AsinNotFound.
        """
        assert_raises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_bulk_lookup(self):
        """Test Baulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_bulk(self):
        """Test Bulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_bulk_empty(self):
        """Test Bulk Product Lookup With No Results.

        Tests that a bulk product lookup request with no results
        returns an empty list.
        """
        asins = ['not-an-asin', 'als-not-an-asin']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(type(products), list)
        assert_equals(len(products), 0)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_iterate_pages(self):
        products = self.amazon.search(Keywords='internet of things oreilly',
                                      SearchIndex='Books')
        assert_false(products.is_last_page)
        for product in products:
            if products.current_page < 8:
                assert_false(products.is_last_page)
            else:
                assert_true(products.is_last_page)

        assert_true(products.is_last_page)
        assert_true(products.current_page == 8)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_no_results(self):
        """Test Product Search with no results.

        Tests that a product search with that returns no results throws a
        SearchException.
        """
        products = self.amazon.search(Title='no-such-thing-on-amazon',
                                      SearchIndex='Automotive')
        assert_raises(SearchException, next, (x for x in products))

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId=TEST_ASIN)
        assert_true(len(products) > 5)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId=TEST_ASIN)
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 2)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B01HQA6EOC")
        expected = u'<p>Introducing an instant classic—master storyteller'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'<strong>An Amazon Best Book of February 2017:</strong>'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'<p>Introducing an instant classic—master storyteller'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_region(self):
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG)
        assert_equals(amazon.region, 'US')

        # old 'region' parameter
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, region='UK')
        assert_equals(amazon.region, 'UK')

        # kwargs method
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, Region='UK')
        assert_equals(amazon.region, 'UK')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_is_adult(self):
        product = self.amazon.lookup(ItemId="B01E7P9LEE")
        assert_true(product.is_adult is not None)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_group(self):
        product = self.amazon.lookup(ItemId="B01LXM0S25")
        assert_equals(product.product_group, 'DVD')

        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.product_group, 'Digital Music Album')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_type_name(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.product_type_name, 'DOWNLOADABLE_MUSIC_ALBUM')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_formatted_price(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.formatted_price, '$12.49')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_price_and_currency(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        price, currency = product.price_and_currency
        assert_equals(price, Decimal('12.49'))
        assert_equals(currency, 'USD')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_list_price(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        price, currency = product.list_price
        assert_equals(price, Decimal('12.49'))
        assert_equals(currency, 'USD')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_running_time(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.running_time, '774')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_studio(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.studio, 'Atlantic Records UK')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_is_preorder(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.is_preorder, '1')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_detail_page_url(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_true(product.detail_page_url.startswith('https://www.amazon.com/%C3%B7-Deluxe-Ed-Sheeran/dp/B01NBTSVDN'))

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability, 'Usually ships in 24 hours')

        product = self.amazon.lookup(ItemId="1491914254") # pre-order book
        assert_equals(product.availability, 'Not yet published')

        product = self.amazon.lookup(ItemId="B000SML2BQ") # late availability
        assert_true(product.availability is not None)

        product = self.amazon.lookup(ItemId="B01LTHP2ZK") # unavailable 
        assert_true(product.availability is None)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability_type(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability_type, 'now')

        product = self.amazon.lookup(ItemId="1491914254") # pre-order book
        assert_equals(product.availability_type, 'now')

        product = self.amazon.lookup(ItemId="B00ZV9PXP2") # late availability
        assert_equals(product.availability_type, 'now')

        product = self.amazon.lookup(ItemId="B01LTHP2ZK") # unavailable
        assert_true(product.availability_type is None)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability_min_max_hours(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability_min_hours, '0')
        assert_equals(product.availability_max_hours, '0')


    def test_kwargs(self):
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, MaxQPS=0.7)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_images(self):
        """Test images property

        Test that the images property has a value when using the
        Images ResponseGroup
        """
        product = self.amazon.lookup(ResponseGroup='Images',
                                     ItemId='B00TSVVNQC')
        assert_equals(type(product.images), list)
        assert_equals(len(product.images), 7)
コード例 #10
0
ファイル: tests.py プロジェクト: kumarremoa/Goferbot
class TestAmazonApi(unittest.TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """

    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            CacheReader=cache_reader,
            CacheWriter=cache_writer,
            MaxQPS=0.5
        )

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B00I15SB16")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0848719039726')
        assert_equals(
            product.large_image_url,
            'https://images-na.ssl-images-amazon.com/images/I/51XGerXeYeL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '469', 'ItemDimensions.Height': '40'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_lookup_nonexistent_asin(self):
        """Test Product Lookup with a nonexistent ASIN.

        Tests that a product lookup for a nonexistent ASIN raises AsinNotFound.
        """
        assert_raises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")

    def test_bulk_lookup(self):
        """Test Baulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_lookup_bulk(self):
        """Test Bulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_lookup_bulk_empty(self):
        """Test Bulk Product Lookup With No Results.

        Tests that a bulk product lookup request with no results
        returns an empty list.
        """
        asins = ['not-an-asin', 'als-not-an-asin']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(type(products), list)
        assert_equals(len(products), 0)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    def test_search_no_results(self):
        """Test Product Search with no results.

        Tests that a product search with that returns no results throws a
        SearchException.
        """
        products = self.amazon.search(Title='no-such-thing-on-amazon',
                                      SearchIndex='Automotive')
        assert_raises(SearchException, next, (x for x in products))

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId=TEST_ASIN)
        assert_true(len(products) > 5)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId=TEST_ASIN)
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 2)

    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B000FBJCJE")
        expected = u'<b>One of <i>Time</i>'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'From the opening line'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'<b>One of <i>Time</i>'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_region(self):
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG)
        assert_equals(amazon.region, 'US')

        # old 'region' parameter
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, region='UK')
        assert_equals(amazon.region, 'UK')

        # kwargs method
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, Region='UK')
        assert_equals(amazon.region, 'UK')

    def test_kwargs(self):
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG, MaxQPS=0.7)

    def test_images(self):
        """Test images property

        Test that the images property has a value when using the
        Images ResponseGroup
        """
        product = self.amazon.lookup(ResponseGroup='Images',
                                     ItemId='B00TSVVNQC')
        assert_equals(type(product.images), list)
        assert_equals(len(product.images), 7)
コード例 #11
0
import requests
from bs4 import BeautifulSoup

from banned_exception import BannedException

MONGO_HOST = "mongodb://*****:*****@ec2-52-87-161-70.compute-1.amazonaws.com/Amazondb"  # assuming you have mongoDB installed locally                                             # and a database called 'twitterdb'
client = MongoClient(MONGO_HOST)
db = client.Amazondb

AMAZON_ACCESS_KEY = 'AKIAJNNFMSX5V3HLZB6Q'
AMAZON_SECRET_KEY = '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'
AMAZON_ASSOC_TAG = 'tina31726-20'

amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

browse_id = amazon.browse_node_lookup(
    BrowseNodeId=11058281)  ##Makeup_node = 11058281
#Browse_node_mining(browse_id)

#parent = [a for a in browse_id[0].ancestors]
Makeup_dir = defaultdict(int)
child = [a for a in browse_id[0].children]
len(child)
for n in child:
    print(n.name, n.id)
    if n.name != 'Body':
        Makeup_dir[n.name] = n.id


def Browse_node_mining(i):
    browse_id = amazon.browse_node_lookup(BrowseNodeId=i)
    try:
コード例 #12
0
class TestAmazonApi(TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """
    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B007HCCNJU")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0814916017775')
        assert_equals(
            product.large_image_url,
            'http://ecx.images-amazon.com/images/I/41VZlVs8agL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '650', 'ItemDimensions.Height': '130'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_batch_lookup(self):
        """Test Batch Product Lookup.

        Tests that a batch product lookup request returns multiple results.
        """
        asins = ['B0051QVESA', 'B005DOK8NW', 'B005890G8Y',
                 'B0051VVOB2', 'B005890G8O']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), 5)
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            AMAZON_ACCESS_KEY,
            AMAZON_SECRET_KEY,
            AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId="B0051QVF7A")
        assert_equals(len(products), 10)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)
コード例 #13
0
ファイル: api.py プロジェクト: kalvinter/apas
class APIHandler:
    HEADER = "{0: <20}".format("(APIHandler):")

    AMAZON_NAMESPACE = {
        "owl": "http://webservices.amazon.com/AWSECommerceService/2013-08-01"
    }
    RETRY_LIMIT = 5
    COOLDOWN_TIME_FOR_RETRY = 10
    AMAZON_API = None

    AMAZON_ACCESS_KEY = None
    AMAZON_SECRET_KEY = None
    AMAZON_ASSOC_TAG = None
    RETRY_ERROR_MESSAGE = f"{HEADER} ERROR: Could not reach API!"

    def __init__(self, verbose: bool = False):
        self.verbose = verbose

    def create_api_connection(self, config_dict: dict, secrets_dict: dict):
        try:
            self.AMAZON_ACCESS_KEY = secrets_dict["AMAZON_ACCESS_KEY"]
            self.AMAZON_SECRET_KEY = secrets_dict["AMAZON_SECRET_KEY"]
            self.AMAZON_ASSOC_TAG = secrets_dict["AMAZON_ASSOC_TAG"]

        except Exception as e:
            raise Exception(
                f"{self.HEADER} FATAL: Could not read necessary access-, secret-key and association "
                f"tag from config. Error-Msg: {str(e)}")

        try:
            self.RETRY_LIMIT = int(config_dict["RETRY_LIMIT"])
            self.COOLDOWN_TIME_FOR_RETRY = int(
                config_dict["COOLDOWN_TIME_FOR_RETRY"])

        except Exception as e:
            LogHandler.log_message(
                f"{self.HEADER} WARNING: Could not read API-Handler-Values from config. Using default "
                f"values instead. Error-Msg: {str(e)}")

        self.RETRY_ERROR_MESSAGE = (
            f"{self.HEADER} ERROR: Could not reach API after {self.RETRY_LIMIT} retries!"
        )

        try:
            self.AMAZON_API = AmazonAPI(
                self.AMAZON_ACCESS_KEY,
                self.AMAZON_SECRET_KEY,
                self.AMAZON_ASSOC_TAG,
                region="DE",
                MaxQPS=0.5,
            )

        except TypeError as e:
            LogHandler.log_message(
                f"{self.HEADER} ERROR: Could not initialize Amazon-API-Connection! Have you "
                f"provided correct values in your secrets.json-File? Msg.: {str(e)}"
            )

    def api_call_with_retry(self, function, function_params: dict) -> list:
        time.sleep(1)
        result = None
        counter = 1
        while counter <= self.RETRY_LIMIT and not result:

            try:
                result = function(**function_params)

            except HTTPError:
                LogHandler.log_message(
                    f"{self.HEADER} WARNING (Nr. {counter}): API Call failed. Retrying "
                    f"in {self.COOLDOWN_TIME_FOR_RETRY} seconds ...")
                counter += 1
                time.sleep(self.COOLDOWN_TIME_FOR_RETRY)

        if not result:
            raise Exception(self.RETRY_ERROR_MESSAGE)

        return result

    def browse_node_lookup(self, **kwargs):
        return self.AMAZON_API.browse_node_lookup(**kwargs)

    def lookup(self, **kwargs):
        return self.AMAZON_API.lookup(**kwargs)
コード例 #14
0
class TestAmazonApi(TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """
    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                                AMAZON_ASSOC_TAG)

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B007HCCNJU")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0814916017775')
        assert_equals(product.large_image_url,
                      'http://ecx.images-amazon.com/images/I/41VZlVs8agL.jpg')
        assert_equals(product.get_attribute('Publisher'), 'Amazon')
        assert_equals(
            product.get_attributes(
                ['ItemDimensions.Width', 'ItemDimensions.Height']), {
                    'ItemDimensions.Width': '650',
                    'ItemDimensions.Height': '130'
                })
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    def test_batch_lookup(self):
        """Test Batch Product Lookup.

        Tests that a batch product lookup request returns multiple results.
        """
        asins = [
            'B00AWH595M', 'B007HCCNJU', 'B00BWYQ9YE', 'B00BWYRF7E',
            'B00D2KJDXA'
        ]
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), 5)
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(1,
                                        Keywords='kindle',
                                        SearchIndex='All')
        assert_equals(len(products), 1)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG)
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(AMAZON_ACCESS_KEY,
                           AMAZON_SECRET_KEY,
                           AMAZON_ASSOC_TAG,
                           region="UK")
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId="B0051QVF7A")
        assert_true(len(products) > 5)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 1)

    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B000FBJCJE")
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'From the opening line'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'Only once in a great'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)
コード例 #15
0
class TestAmazonApi(TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """

    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        assert_equals(product.title, 'Kindle, Wi-Fi, 6" E Ink Display - for international shipment')
        assert_equals(product.ean, "0814916014354")
        assert_equals(product.large_image_url, "http://ecx.images-amazon.com/images/I/411H%2B731ZzL.jpg")
        assert_equals(product.get_attribute("Publisher"), "Amazon Digital Services, Inc")
        assert_equals(
            product.get_attributes(["ItemDimensions.Width", "ItemDimensions.Height"]),
            {"ItemDimensions.Width": "450", "ItemDimensions.Height": "34"},
        )
        assert_true(len(product.browse_nodes) > 0)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, "eBook Readers")

    def test_batch_lookup(self):
        """Test Batch Product Lookup.

        Tests that a batch product lookup request returns multiple results.
        """
        asins = ["B0051QVESA", "B005DOK8NW", "B005890G8Y", "B0051VVOB2", "B005890G8O"]
        products = self.amazon.lookup(ItemId=",".join(asins))
        assert_equals(len(products), 5)
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords="kindle", SearchIndex="All")
        for product in products:
            assert_true(hasattr(product, "title"))
            break
        else:
            assert_true(False, "No search results returned.")

    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(1, Keywords="kindle", SearchIndex="All")
        assert_equals(len(products), 1)

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)
        assert_equals(amazon.api.Region, "US")

    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region="UK")
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords="Kindle", SearchIndex="All")
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = "GBP" in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId="B0051QVF7A")
        assert_equals(len(products), 10)

    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId="B0051QVF7A")
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, "eBook Readers")
        assert_equals(bn.is_category_root, False)
コード例 #16
0
class TestAmazonApi(unittest.TestCase):
    """Test Amazon API

    Test Class for Amazon simple API wrapper.
    """

    def setUp(self):
        """Set Up.

        Initialize the Amazon API wrapper. The following values:

        * AMAZON_ACCESS_KEY
        * AMAZON_SECRET_KEY
        * AMAZON_ASSOC_TAG

        Are imported from a custom file named: 'test_settings.py'
        """
        self.amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG,
            CacheReader=cache_reader,
            CacheWriter=cache_writer,
            MaxQPS=0.5
        )

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup(self):
        """Test Product Lookup.

        Tests that a product lookup for a kindle returns results and that the
        main methods are working.
        """
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_true('Kindle' in product.title)
        assert_equals(product.ean, '0848719083774')
        assert_equals(
            product.large_image_url,
            'https://images-na.ssl-images-amazon.com/images/I/51hrdzXLUHL.jpg'
        )
        assert_equals(
            product.get_attribute('Publisher'),
            'Amazon'
        )
        assert_equals(product.get_attributes(
            ['ItemDimensions.Width', 'ItemDimensions.Height']),
            {'ItemDimensions.Width': '450', 'ItemDimensions.Height': '36'})
        assert_true(len(product.browse_nodes) > 0)
        assert_true(product.price_and_currency[0] is not None)
        assert_true(product.price_and_currency[1] is not None)
        assert_equals(product.browse_nodes[0].id, 2642129011)
        assert_equals(product.browse_nodes[0].name, 'eBook Readers')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_nonexistent_asin(self):
        """Test Product Lookup with a nonexistent ASIN.

        Tests that a product lookup for a nonexistent ASIN raises AsinNotFound.
        """
        assert_raises(AsinNotFound, self.amazon.lookup, ItemId="ABCD1234")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_bulk_lookup(self):
        """Test Baulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_bulk(self):
        """Test Bulk Product Lookup.

        Tests that a bulk product lookup request returns multiple results.
        """
        asins = [TEST_ASIN, 'B00BWYQ9YE',
                 'B00BWYRF7E', 'B00D2KJDXA']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(len(products), len(asins))
        for i, product in enumerate(products):
            assert_equals(asins[i], product.asin)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_lookup_bulk_empty(self):
        """Test Bulk Product Lookup With No Results.

        Tests that a bulk product lookup request with no results
        returns an empty list.
        """
        asins = ['not-an-asin', 'als-not-an-asin']
        products = self.amazon.lookup_bulk(ItemId=','.join(asins))
        assert_equals(type(products), list)
        assert_equals(len(products), 0)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search(self):
        """Test Product Search.

        Tests that a product search is working (by testing that results are
        returned). And that each result has a title attribute. The test
        fails if no results where returned.
        """
        products = self.amazon.search(Keywords='kindle', SearchIndex='All')
        for product in products:
            assert_true(hasattr(product, 'title'))
            break
        else:
            assert_true(False, 'No search results returned.')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_n(self):
        """Test Product Search N.

        Tests that a product search n is working by testing that N results are
        returned.
        """
        products = self.amazon.search_n(
            1,
            Keywords='kindle',
            SearchIndex='All'
        )
        assert_equals(len(products), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_iterate_pages(self):
        products = self.amazon.search(Keywords='internet of things oreilly',
                                      SearchIndex='Books')
        assert_false(products.is_last_page)
        for product in products:
            pass
        assert_true(products.is_last_page)


    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_no_results(self):
        """Test Product Search with no results.

        Tests that a product search with that returns no results throws a
        SearchException.
        """
        products = self.amazon.search(Title='no-such-thing-on-amazon',
                                      SearchIndex='Automotive')
        assert_raises(SearchException, next, (x for x in products))

    def test_amazon_api_defaults_to_US(self):
        """Test Amazon API defaults to the US store."""
        amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG
        )
        assert_equals(amazon.api.Region, "US")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_search_amazon_uk(self):
        """Test Poduct Search on Amazon UK.

        Tests that a product search on Amazon UK is working and that the
        currency of any of the returned products is GBP. The test fails if no
        results were returned.
        """
        amazon = AmazonAPI(
            _AMAZON_ACCESS_KEY,
            _AMAZON_SECRET_KEY,
            _AMAZON_ASSOC_TAG,
            region="UK"
        )
        assert_equals(amazon.api.Region, "UK", "Region has not been set to UK")

        products = amazon.search(Keywords='Kindle', SearchIndex='All')
        currencies = [product.price_and_currency[1] for product in products]
        assert_true(len(currencies), "No products found")

        is_gbp = 'GBP' in currencies
        assert_true(is_gbp, "Currency is not GBP, cannot be Amazon UK, though")

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_similarity_lookup(self):
        """Test Similarity Lookup.

        Tests that a similarity lookup for a kindle returns 10 results.
        """
        products = self.amazon.similarity_lookup(ItemId=TEST_ASIN)
        assert_true(len(products) > 5)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_attributes(self):
        """Test Product Attributes.

        Tests that all product that are supposed to be accessible are.
        """
        product = self.amazon.lookup(ItemId=TEST_ASIN)
        for attribute in PRODUCT_ATTRIBUTES:
            getattr(product, attribute)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_browse_node_lookup(self):
        """Test Browse Node Lookup.

        Test that a lookup by Brose Node ID returns appropriate node.
        """
        bnid = 2642129011
        bn = self.amazon.browse_node_lookup(BrowseNodeId=bnid)[0]
        assert_equals(bn.id, bnid)
        assert_equals(bn.name, 'eBook Readers')
        assert_equals(bn.is_category_root, False)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_obscure_date(self):
        """Test Obscure Date Formats

        Test a product with an obscure date format
        """
        product = self.amazon.lookup(ItemId="0933635869")
        assert_equals(product.publication_date.year, 1992)
        assert_equals(product.publication_date.month, 5)
        assert_true(isinstance(product.publication_date, datetime.date))

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_single_creator(self):
        """Test a product with a single creator
        """
        product = self.amazon.lookup(ItemId="B00005NZJA")
        creators = dict(product.creators)
        assert_equals(creators[u"Jonathan Davis"], u"Narrator")
        assert_equals(len(creators.values()), 2)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_multiple_creators(self):
        """Test a product with multiple creators
        """
        product = self.amazon.lookup(ItemId="B007V8RQC4")
        creators = dict(product.creators)
        assert_equals(creators[u"John Gregory Betancourt"], u"Editor")
        assert_equals(creators[u"Colin Azariah-Kribbs"], u"Editor")
        assert_equals(len(creators.values()), 2)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_no_creators(self):
        """Test a product with no creators
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_false(product.creators)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_single_editorial_review(self):
        product = self.amazon.lookup(ItemId="1930846258")
        expected = u'In the title piece, Alan Turing'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        assert_equals(product.editorial_review, product.editorial_reviews[0])
        assert_equals(len(product.editorial_reviews), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_multiple_editorial_reviews(self):
        product = self.amazon.lookup(ItemId="B01HQA6EOC")
        expected = u'<p>Introducing an instant classic—master storyteller'
        assert_equals(product.editorial_reviews[0][:len(expected)], expected)
        expected = u'<strong>An Amazon Best Book of February 2017:</strong>'
        assert_equals(product.editorial_reviews[1][:len(expected)], expected)
        # duplicate data, amazon user data is great...
        expected = u'<p>Introducing an instant classic—master storyteller'
        assert_equals(product.editorial_reviews[2][:len(expected)], expected)

        assert_equals(len(product.editorial_reviews), 3)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_languages_english(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="1930846258")
        assert_true('english' in product.languages)
        assert_equals(len(product.languages), 1)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_languages_spanish(self):
        """Test Language Data

        Test an English product
        """
        product = self.amazon.lookup(ItemId="8420658537")
        assert_true('spanish' in product.languages)
        assert_equals(len(product.languages), 1)

    def test_region(self):
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG)
        assert_equals(amazon.region, 'US')

        # old 'region' parameter
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, region='UK')
        assert_equals(amazon.region, 'UK')

        # kwargs method
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, Region='UK')
        assert_equals(amazon.region, 'UK')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_is_adult(self):
        product = self.amazon.lookup(ItemId="B01E7P9LEE")
        assert_true(product.is_adult is not None)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_group(self):
        product = self.amazon.lookup(ItemId="B01LXM0S25")
        assert_equals(product.product_group, 'DVD')

        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.product_group, 'Digital Music Album')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_product_type_name(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.product_type_name, 'DOWNLOADABLE_MUSIC_ALBUM')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_formatted_price(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.formatted_price, '$12.49')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_price_and_currency(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        price, currency = product.price_and_currency
        assert_equals(price, Decimal('12.49'))
        assert_equals(currency, 'USD')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_list_price(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        price, currency = product.list_price
        assert_equals(price, Decimal('12.49'))
        assert_equals(currency, 'USD')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_running_time(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.running_time, '3567')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_studio(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.studio, 'Atlantic Records UK')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_is_preorder(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_equals(product.is_preorder , None)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_detail_page_url(self):
        product = self.amazon.lookup(ItemId="B01NBTSVDN")
        assert_true(product.detail_page_url.startswith('https://www.amazon.com/%C3%B7-Deluxe-Ed-Sheeran/dp/B01NBTSVDN'))

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability, 'Usually ships in 24 hours')

        product = self.amazon.lookup(ItemId="1491914254") # pre-order book
        assert_equals(product.availability, 'Not yet published')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability_type(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability_type, 'now')

        product = self.amazon.lookup(ItemId="1491914254") # pre-order book
        assert_equals(product.availability_type, 'now')

        product = self.amazon.lookup(ItemId="B00ZV9PXP2") # late availability
        assert_equals(product.availability_type, 'now')

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_availability_min_max_hours(self):
        product = self.amazon.lookup(ItemId="B00ZV9PXP2")
        assert_equals(product.availability_min_hours, '0')
        assert_equals(product.availability_max_hours, '0')


    def test_kwargs(self):
        amazon = AmazonAPI(_AMAZON_ACCESS_KEY, _AMAZON_SECRET_KEY,
                           _AMAZON_ASSOC_TAG, MaxQPS=0.7)

    @flaky(max_runs=3, rerun_filter=delay_rerun)
    def test_images(self):
        """Test images property

        Test that the images property has a value when using the
        Images ResponseGroup
        """
        product = self.amazon.lookup(ResponseGroup='Images',
                                     ItemId='B00TSVVNQC')
        assert_equals(type(product.images), list)
        assert_equals(len(product.images), 7)