コード例 #1
0
ファイル: controllers.py プロジェクト: kmagai/papylus
def search_item():
    query = request.args.get('q')
    category = request.args.get('c')
    ## replace confidencial to secret file and put it in gitignore
    from amazon.api import AmazonAPI
    amazon = AmazonAPI(os.environ['AWS_ACCESS_KEY'], os.environ['AWS_SECRET_KEY'], 'papylus-22', region="JP")

    if not category:
        category = 'All'

    try:
        products = amazon.search_n(5, Keywords=query, SearchIndex=category)
    except:
        # in case 503 Error occurs without any reason
        import time
        for i in range(3):
            print '503 Error'
            time.sleep(1)
            products = amazon.search_n(5, Keywords=query, SearchIndex=category)
            if products:
                break

    items = []
    for i in products:
        pub_date = i.publication_date.strftime(u'%Y-%m-%d') if i.publication_date != None else ''
        items.append({'name': i.title, 'url': i.offer_url, 'img': i.medium_image_url, 'publisher': i.publisher, 'pub_date': pub_date})
    return json.dumps(items)
コード例 #2
0
ファイル: models.py プロジェクト: phrac/partflux
 def asin_search(self):
     try:
         amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG)
         products = amazon.search_n(10, Keywords="%s %s" % (self.company.name, self.number), SearchIndex="All")
     except:
         products = None
     return products
コード例 #3
0
    def ProcessQueryAmazon(keys, query):
        try:
            amazon = AmazonAPI(keys[0][0], keys[0][1], keys[0][2])
            products = amazon.search_n(100, Keywords=query, SearchIndex='All')

            tempList = []
            if products is not None:
                for item in products:
                    tempDict = {}
                    tempDict[CStaticConsts.siteName] = CStaticConsts.Amazon
                    tempDict[CStaticConsts.title] = item.title

                    # Some of the amazon price/currency are null
                    # so just populate the field with NA
                    if not item.price_and_currency[1]:
                        tempDict[CStaticConsts.currencyType] = 'NA'
                    else:
                        tempDict[CStaticConsts.
                                 currencyType] = item.price_and_currency[1]
                    if not item.price_and_currency[0]:
                        tempDict[CStaticConsts.itemPrice] = 'NA'
                    else:
                        tempDict[CStaticConsts.
                                 itemPrice] = item.price_and_currency[0]
                    tempDict[CStaticConsts.productUrl] = item.offer_url

                    tempList.append(tempDict)

            return tempList

        except urllib.error.HTTPError as e:
            print('Error getamazonlistings: ', e)
        except Exception as e:
            print('Error: getamazonlistings threw exception. Message: ', e)
コード例 #4
0
class Item(object):
	
	def __init__(self, product):
		self.product = product
		self.amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

	def prod_search(self):
		products_found = self.amazon.search_n(1, Keywords= self.product, SearchIndex= "All")
		return products_found


	def prod_item(self):

		products_found = self.prod_search()
		try: 
			return products_found[0].title
		except IndexError:
			raise Exception('No product found')		

	def prod_asin(self):


		products_found = self.prod_search()
		try: 
			return products_found[0].asin
		except IndexError:
			raise Exception('No Asin found')	

	def prod_price(self):

		product_asin = self.prod_asin()
		the_product = self.amazon.lookup(ItemId='' + product_asin + '')
		found_product_price = the_product.price_and_currency
		print the_product.image
		return found_product_price
コード例 #5
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)
コード例 #6
0
 def get_queryset(self):  # pragma: no cover
     # Not covering this as we have no good way to mock a request to
     # the amazon api as they use request signatures. - Devon Bleibtrey
     amazon = AmazonAPI(settings.AMAZON_PROMOTION_API_KEY,
                        settings.AMAZON_PROMOTION_API_SECRET_KEY,
                        settings.AMAZON_ASSOCIATE_TAG)
     queryset = []
     query_param = self.request.query_params.get("query", "")
     if query_param:
         try:
             products = amazon.search_n(n=15,
                                        Keywords=query_param,
                                        SearchIndex="All")
         except SearchException:
             raise ValidationError("Sorry, we found no products "
                                   "matching your query.")
         for product in products:
             price, currency = product.price_and_currency
             has_reviews, iframe = product.reviews
             queryset.append({
                 "title": product.title,
                 "image": product.large_image_url,
                 "price": price,
                 "currency": currency,
                 "asin": product.asin,
                 "url": product.offer_url,
                 "has_reviews": has_reviews,
                 "iframe": iframe
             })
     return queryset
コード例 #7
0
ファイル: main.py プロジェクト: gegbo/lowest_current_deals
    def get(self):
        search = self.request.get('search')
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) #initiates a new Amazon API
        amazon_results = amazon.search_n(15, Keywords=search, SearchIndex='All')

        # returns in JSON name, salePrice, and URL of user's search from BestBuy
        best_buy_url = 'http://api.remix.bestbuy.com/v1/products(search='+ search.replace(' ', '&search=')+')?format=json&show=sku,name,salePrice,url,image,upc&pageSize=15&page=5&apiKey=24ta6vtsr78a22fmv8ngfjet'
        best_buy_results = json.load(urllib2.urlopen(best_buy_url)).get('products')

        walmart_url = "http://api.walmartlabs.com/v1/search?query=%s&format=json&apiKey=cz9kfm3vuhssnk6hn33zg86k&responseGroup=base" % search.replace(' ','+')
        walmart_results = json.load(urllib2.urlopen(walmart_url)).get('items')

        results = []
        for product in amazon_results:
            results += [(product.title, product.price_and_currency[0], product.offer_url, product.medium_image_url, 'Amazon','/wishlist?type=amazon&id=%s'%product.asin,"/compare?upc=%s" %str(product.upc), '/email?producturl=%s'%product.offer_url)]
            #How to retrive asin for amazon products and decrease image size
        for product in best_buy_results:
            results += [(product.get('name'), product.get('salePrice'), product.get('url'), product.get('image'), 'Best Buy','/wishlist?type=bestbuy&id=%s'%product.get('sku'),"/compare?upc=%s" %str(product.get('upc')), '/email?producturl=%s'%product.get('url'))]
        for product in walmart_results:
            results += [(product.get('name'), product.get('salePrice'), product.get('productUrl'), product.get('thumbnailImage'), 'Walmart','/wishlist?type=walmart&id=%s'%product.get('itemId'),"/compare?upc=%s" %str(product.get('upc')),'/email?producturl=%s'%product.get('productUrl'))]

        results = sorted(results,key=lambda x: x[1])

        for item in results:
            i = results.index(item)
            if str(item[1])[len(str(item[1]))-2] == '.':
                item = list(item)
                item[1] = str(item[1]) + '0'
                item = tuple(item)
                results[i] = item
        template_variables={"user_search":search, 'results':results, 'user': users.get_current_user(),  'logout' : users.create_logout_url('/'), 'login' : users.create_login_url('/')}
        template=jinja_environment.get_template('/templates/results.html')
        self.response.write(template.render(template_variables))
コード例 #8
0
class GiftlyAmazonAPI:

    amazon_product_details = ['api', 'asin', 'author', 'authors',
                              'aws_associate_tag', 'binding', 'brand',
                              'browse_nodes', 'color', 'creators', 'ean',
                              'edition', 'editorial_review', 'editorial_reviews',
                              'eisbn', 'features', 'get_attribute',
                              'get_attribute_details', 'get_attributes',
                              'get_parent', 'images', 'isbn', 'label', 'languages',
                              'large_image_url', 'list_price', 'manufacturer',
                              'medium_image_url', 'model', 'mpn', 'offer_url',
                              'pages', 'parent', 'parent_asin', 'parsed_response',
                              'part_number', 'price_and_currency', 'publication_date',
                              'publisher', 'region', 'release_date', 'reviews', 'sales_rank',
                              'sku', 'small_image_url', 'tiny_image_url', 'title',
                              'to_string', 'upc']


    amazon_search_index = ['All','Apparel','Appliances','ArtsAndCrafts','Automotive', 'Baby',
                           'Beauty','Blended','Books','Classical','Collectibles','DVD',
                           'DigitalMusic','Electronics', 'GiftCards','GourmetFood','Grocery',
                           'HealthPersonalCare','HomeGarden','Industrial','Jewelry', 'KindleStore',
                           'Kitchen','LawnAndGarden','Marketplace','MP3Downloads','Magazines','Miscellaneous',
                           'Music','MusicTracks','MusicalInstruments','MobileApps','OfficeProducts','OutdoorLiving',
                           'PCHardware', 'PetSupplies','Photo','Shoes','Software','SportingGoods',
                           'Tools','Toys','UnboxVideo','VHS','Video', 'VideoGames','Watches','Wireless','WirelessAccessories']

    def __init__(self, secret_key, access_key, assoc_tag):
        self.amazon = AmazonAPI(access_key, secret_key, assoc_tag)

    #Keywords is a comma-separated string
    #Returns a dictionary of products mapped as ASIN:TITLE
    #Can Android parse for keys? We'll find out...
    def get_similar_items(self, keywords, numitems=None, category=None):
        keywords = keywords if keywords else None
        numitems = numitems if numitems else 10
        category = category if category else 'All'

        print "%d items found with keywords %s in the %s category" % (numitems, keywords, category)

        products = self.amazon.search_n(numitems, Keywords=keywords, SearchIndex=category)
        product_dict = {}
        for product in products:
            product_dict[product.asin] = product.title
        return product_dict

    def get_item_by_asin(self, asin):
        product = self.amazon.lookup(ItemId=asin)
        product = AmazonProduct(product.asin, product.title)
        return product.get_product()

    #asin_list is a list of individual asin strings
    #they are joined together as one large string
    def get_items_by_asin(self, asin_list):
        product_list = []
        products = self.amazon.lookup(ItemId=(asin_list))
        for product in products:
            product_list.append(AmazonProduct(product.asin, product.title).get_product())
        print product_list
        return product_list
コード例 #9
0
ファイル: amazon_service.py プロジェクト: jeremyrea/caterblu
class AmazonService:

    __API_VERSION = '2013-08-01'

    def __init__(self, title, country):
        self.title = title
        self.country = country

        env_vars = self.get_amazon_env_variables()
        self.amazon = AmazonAPI(env_vars['access_key'],
                                env_vars['secret_key'],
                                env_vars['associates_tag'],
                                Region=self.country,
                                Version=self.__API_VERSION)

    def get_price(self):
        products = self.amazon.search_n(10,
                                        Keywords=self.title + ' [Blu-ray]',
                                        SearchIndex='All')

        ranked_products = self.rank_products(products)
        product = ranked_products[0]

        price = Price()
        price.price = list(product.price_and_currency)
        price.list_price = list(product.list_price)
        price.link = product.offer_url

        return price

    def rank_products(self, products):
        ranked_products = sorted(products, key=lambda x: self.product_score(x), reverse=True)

        return ranked_products

    def product_score(self, p):
        title = str(p.title)
        title = title.replace('[Blu-ray]', '')
        title = title.replace('(Bilingual)', '')
        title = title.replace('(Region Free)', '')
        title = title.strip(' ')
        size_diff = abs(len(title) - len(self.title))

        p_score = 1 if size_diff == 0 else 0
        p_score += 1 if re.search('[Blu\-ray]', p.title) else p_score

        return p_score

    def get_amazon_env_variables(self):
        access_key = os.environ.get('AWS_ACCESS_KEY_ID')
        secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
        associates_tag = os.environ.get('AWS_ASSOCIATES_TAG')

        env_variables = {'access_key': access_key,
                         'secret_key': secret_key,
                         'associates_tag': associates_tag}

        return env_variables
コード例 #10
0
ファイル: getItem.py プロジェクト: angerhang/junctionHack
def obtain_products(name, num):
    AMAZON_ACCESS_KEY = 'AKIAIGKWJJGRKVVHTNFA'
    AMAZON_SECRET_KEY = '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'
    AMAZON_ASSOC_TAG = '5566399-20'

    amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    # get the images from top 10 from this categor
    products = amazon.search_n(num, Keywords=name, SearchIndex='All')

    return products
コード例 #11
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)
コード例 #12
0
class AWSClient(object):
    """
    """
    def __init__(self,
                 region=None,
                 access_key=None,
                 secret_access_key=None,
                 tag=None):
        self.region = 'us-east-1'
        self.root_access_key = 'AKIAJB4BJYPJKV5YACXQ'
        self.root_secret_access_key = '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'
        self.search_access_key = 'AKIAIS2HFIM7UBM2H5CA'
        self.search_secret_access_key = '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'
        self.associate_tag = "msucapstone0a-20"
        self.create_comprehend_client()
        self.create_search_client()

    def create_comprehend_client(self):
        """
        """
        self.comprehend_client = boto3.client(
            'comprehend',
            region_name=self.region,
            aws_access_key_id=self.root_access_key,
            aws_secret_access_key=self.root_secret_access_key)

    def create_search_client(self):
        self.search_client = AmazonAPI(self.search_access_key,
                                       self.search_secret_access_key,
                                       self.associate_tag)

    def run_transcribe_job(self):
        pass

    def comprehend_entities(self, text_input):
        response = self.comprehend_client.detect_entities(Text=text_input,
                                                          LanguageCode='en')

        return response

    def comprehend_key_phrases(self, text_input):
        response = self.comprehend_client.detect_key_phrases(Text=text_input,
                                                             LanguageCode='en')

        return response

    def comprehend_sentiment(self, text_input):
        pass

    def search_n(self, keywords, index, n):
        return self.search_client.search_n(n,
                                           Keywords=keywords,
                                           SearchIndex=index)
コード例 #13
0
def func(product):
	#insert amazon web services credentials
	AMAZON_ACCESS_KEY = 'AKIAJGEKFL3UEU6QMCPQ'
	AMAZON_SECRET_KEY = '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'
	#associate TAG must be updated every 180 days, make new amazon associates account to get new tag
	AMAZON_ASSOC_TAG = 'ignacio0ba-20'

	amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

	products = amazon.search_n(1,Keywords= product, SearchIndex= "All")

	print products
コード例 #14
0
ファイル: amazonapi.py プロジェクト: seanziegler/amazonvenv
def keywordSearch(keywords):
	'''Searches for top 50 results on Amazon of keywords entered by user'''
	amazon = AmazonAPI(amazonAccessKey, amazonSecretKey, amazonAssocTag)
	products = amazon.search_n(50, Keywords=keywords, SearchIndex='All')
	title = ['i', 'i']
	image = ['']
	listPrice = ['']
	for counter, product in enumerate(products):
		title.insert(counter, product.title)
		image.insert(counter, product.large_image_url)
		listPrice.insert(counter, product.price_and_currency)
	return title, image, listPrice
コード例 #15
0
ファイル: bot.py プロジェクト: kevincianfarini/reddit-bot
def get_amazon_order(item):
    amazon = AmazonAPI(AWS['AMAZON_KEY'], AWS['SECRET_KEY'], AWS['ASSOCIATE_TAG'])
    if len(item) > 0:
        try:
            product = amazon.search_n(1, Keywords=item, SearchIndex='All')
            try:
                return product[0]
            except IndexError:
                return None
        except AmazonException:
            return None
    else:
        return None
コード例 #16
0
def amazon_product(asin, keyword, marketplace='US'):
    try:
        amazon = AmazonAPI(settings.AWS_KEY,
                           settings.AWS_SECRET,
                           settings.AWS_API,
                           region=marketplace)
        search_item = asin + ' ' + keyword
        products = amazon.search_n(1, Keywords=search_item, SearchIndex='All')
        if len(products) != 0:
            return True
        return False
    except:
        return False
コード例 #17
0
	def prod_search(self):

		#insert amazon web services credentials
		#associate TAG must be updated every 180 days, make new amazon associates account to get new tag

		amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

		products_found = amazon.search_n(1,Keywords= self.product, SearchIndex= "All")

		try: 
			return products_found[0].title
		except IndexError:
			return 'No such item available'
コード例 #18
0
def amazon_api_request():
    item_id = request.json.get('item_id')
    brand = request.json.get('brand')
    model = request.json.get('model')

    item = Item.query.filter_by(id=item_id).first()

    amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    browse_modes = [
        "679270011", "679350011", "679307011", "679380011", "679380011",
        "679280011", "679357011"
    ]

    if item:
        # loop through browseNodes
        for node in browse_modes:
            try:
                products = amazon.search_n(
                    1,
                    Brand=brand,
                    Title=model,
                    SearchIndex='SportingGoods',
                    ResponseGroup='VariationSummary, Large',
                    BrowseNode=node)

                product_info = {
                    'title': products[0].title,
                    'small_image_url': products[0].small_image_url,
                    'medium_image_url': products[0].medium_image_url,
                    'asin': products[0].asin,
                }

                item.small_image_url = products[0].small_image_url
                item.medium_image_url = products[0].medium_image_url
                item.offer_url = products[0].offer_url

                if item.asin and products[0].asin not in item.asin:
                    item.asin = item.asin + ',' + products[0].asin
                else:
                    item.asin = products[0].asin
                item.thumbnail_cache = datetime.utcnow()
                db.session.commit()

                return json.dumps(product_info)
            except:
                pass
        else:
            # error
            error = {'error': 'amazon api exception'}
            return json.dumps(error)
コード例 #19
0
def get_amazon_order(item):
    amazon = AmazonAPI(AWS['AMAZON_KEY'], AWS['SECRET_KEY'],
                       AWS['ASSOCIATE_TAG'])
    if len(item) > 0:
        try:
            product = amazon.search_n(1, Keywords=item, SearchIndex='All')
            try:
                return product[0]
            except IndexError:
                return None
        except AmazonException:
            return None
    else:
        return None
コード例 #20
0
ファイル: views.py プロジェクト: juleskt/CS411Team5Project
def getAmazonResultsForModal(request):
    if request.method == 'POST':
        ingredient = request.POST.get('ingredientName')

        amazon = AmazonAPI(SecretConfigs.awsAccessKey(),
                           SecretConfigs.awsSecretKey(),
                           SecretConfigs.awsAssociateTag())
        products = amazon.search_n(
            10, Keywords=ingredient,
            SearchIndex='GourmetFood')  #, ItemPage ='1')

        jsonProducts = []
        #jsonProducts['ingredient_name'] = ingredient
        productData = {}

        try:
            for i, product in enumerate(products):
                if product.offer_id is not None:
                    productData['result_number'] = i
                    productData['product_title'] = product.title
                    productData['product_asin'] = product.asin
                    productData[
                        'product_medium_image'] = product.large_image_url
                    productData['product_list_price'] = str(product.list_price)
                    productData['product_brand'] = product.brand
                    productData[
                        'product_formatted_price'] = product.formatted_price
                    productData['detail_page_url'] = product.detail_page_url
                    productData['product_offer_id'] = product.offer_id
                    productData['product_reviews'] = product.reviews[1]

                    jsonProducts.append(productData)
                    productData = {}

            jsonWrapper = {}
            jsonWrapper['products'] = jsonProducts
            jsonWrapper['ingredient_name'] = ingredient

            print(jsonWrapper)

            return render(request, 'shoppingListModal.html', {
                'products': jsonWrapper['products'],
                'ingredient_name': ingredient
            })

        except:
            return render(request, 'shoppingListModal.html', {'error': True})
    else:
        return Http404()
コード例 #21
0
ファイル: nes_extras.py プロジェクト: thebmo/NESRandomizer
def fetch_from_amazon(game):
    title = ''.join(('NES ', game.title))
    
    AMAZON_ACCESS = os.environ['AMAZON_ACCESS']
    AMAZON_SECRET = os.environ['AMAZON_SECRET']
    AMAZON_AWS = os.environ['AMAZON_AWS']
    
    amazon = AmazonAPI(AMAZON_ACCESS, AMAZON_SECRET,AMAZON_AWS)
    try:
        products = amazon.search_n(1, Keywords=title, Condition='Used', SearchIndex = 'VideoGames')
        return amazon.lookup(ItemId=products[0].asin, Condition='Used')
    
    # if above fails the template handles the error
    except:
        pass
コード例 #22
0
ファイル: Amazon.py プロジェクト: marcosorive/torky-api
	def get_price(self):
		url=self.get_url()
		try:	
			amazon = AmazonAPI("", "", "", region="")
			region_options = bottlenose.api.SERVICE_DOMAINS.keys()
			products = amazon.search_n(1,Keywords=self.query, SearchIndex='VideoGames')
			price=products[0].price_and_currency
			if price[0]==None:
				return ("No se ha podido conseguir el precio",url,self.query)
			else:
				price=str(price[0]).replace(".",",") + "€"

			return (price,products[0].offer_url,products[0].title)
		except Exception as e:
			return ("No se ha podido conseguir el precio",url,self.query)
コード例 #23
0
	def get_price_amazon(self,search):
		url=self.get_url("amazon",search)
		try:	
			amazon = AmazonAPI("Your amazon key", "Your amazon secret", "Your amazon vendor user", region="ES")
			region_options = bottlenose.api.SERVICE_DOMAINS.keys()
			products = amazon.search_n(1,Keywords=search, SearchIndex='VideoGames')
			price=products[0].price_and_currency
			if price[0]==None:
				return ("No se ha podido conseguir el precio",url,search)
			else:
				price=str(price[0]).replace(".",",") + "€"

			return (price,products[0].offer_url,products[0].title)
		except Exception as e:
			print("Error precio amazon: --- ",e)
			return ("No se ha podido conseguir el precio",url,search)
コード例 #24
0
	def prod_asin(self):

		#insert amazon web services credentials
		AMAZON_ACCESS_KEY = 'AKIAJGEKFL3UEU6QMCPQ'
		AMAZON_SECRET_KEY = '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'
		#associate TAG must be updated every 180 days, make new amazon associates account to get new tag
		AMAZON_ASSOC_TAG = 'ignacio0ba-20'

		amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

		products_found = amazon.search_n(1,Keywords= self.product, SearchIndex= "All")

		
		try: 
			return products_found[0].asin
		except IndexError:
			return 'No product available'	
コード例 #25
0
ファイル: main.py プロジェクト: gegbo/lowest_current_deals
    def get(self):
        upc_num = self.request.get('upc')

        items = []

        # Add equivalent bestbuy item to a list x
        bestbuy_item=("http://api.remix.bestbuy.com/v1/products(upc=%s)?show=sku,name,salePrice,upc,url,image&apiKey=24ta6vtsr78a22fmv8ngfjet&format=json" %upc_num)
        bestbuy_JSON_string=json.load(urllib2.urlopen(bestbuy_item))

        #checks if list is empty, if it is it wont add to items list
        if len(bestbuy_JSON_string['products']) >0:

            logging.info('Item added to list')
            items += [(bestbuy_JSON_string['products'][0]['name'], bestbuy_JSON_string['products'][0]['salePrice'], bestbuy_JSON_string['products'][0]['url'], bestbuy_JSON_string['products'][0]['image'], 'Best Buy','/wishlist?type=bestbuy&id=%s'%bestbuy_JSON_string['products'][0]['sku'])]

        #Adds equivalent walmart item to a list
        walmart_item=("http://api.walmartlabs.com/v1/items?apiKey=cz9kfm3vuhssnk6hn33zg86k&upc=%s&format=json" %upc_num)

        try:
            walmart_JSON_string=json.load(urllib2.urlopen(walmart_item))

            items += [(walmart_JSON_string['items'][0]['name'], walmart_JSON_string['items'][0]['salePrice'], walmart_JSON_string['items'][0]['productUrl'], walmart_JSON_string['items'][0]['thumbnailImage'], 'Walmart','/wishlist?type=walmart&id=%s'%walmart_JSON_string['items'][0]['itemId'])]
        except urllib2.HTTPError:
            pass

        #Adds equivalent amazon item to a list
        amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) #initiates a new Amazon API
        amazon_results = amazon.search_n(1,Keywords=upc_num, SearchIndex='All')


        if len(amazon_results) > 0:
            items += [(amazon_results[0].title,
            amazon_results[0].price_and_currency[0],amazon_results[0].offer_url,amazon_results[0].medium_image_url,'amazon','/wishlist?type=amazon&id=%s'%amazon_results[0].asin)]
        template=jinja_environment.get_template('/templates/compare.html')

        for item in items:
            i = items.index(item)
            if str(item[1])[len(str(item[1]))-2] == '.':
                item = list(item)
                item[1] = str(item[1]) + '0'
                item = tuple(item)
                items[i] = item

        template_variables={'results':items,   'user': users.get_current_user(),  'logout' : users.create_logout_url('/'), 'login' : users.create_login_url('/')}
        self.response.write(template.render(template_variables))
コード例 #26
0
ファイル: views.py プロジェクト: svsaraf/giftpier
def search(request):
    if request.method == 'POST':
        searchvalue = request.POST.get('searchbox', '')
        saved_or_not = request.POST.get('saved_or_not', '')
        amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOC_ID)
        products = amazon.search_n(20, Keywords=searchvalue, SearchIndex='All')
        context = {
            'search': searchvalue,
            'products': products
        }
        if saved_or_not == 'saved':
            for product in products:
                if product.title and product.offer_url and product.large_image_url and product.formatted_price and product.price_and_currency:
                    gift = Gift(name=product.title, link=product.offer_url, image_link=product.large_image_url, price_desc=product.formatted_price, price=product.price_and_currency[0])
                    gift.save()

    else:
        context = {}
    return render(request, 'azsearch/search.html', context)
コード例 #27
0
def search(request):
    if request.method == 'POST':
        searchvalue = request.POST.get('searchbox', '')
        saved_or_not = request.POST.get('saved_or_not', '')
        amazon = AmazonAPI(ACCESS_KEY, SECRET_KEY, ASSOC_ID)
        products = amazon.search_n(20, Keywords=searchvalue, SearchIndex='All')
        context = {'search': searchvalue, 'products': products}
        if saved_or_not == 'saved':
            for product in products:
                if product.title and product.offer_url and product.large_image_url and product.formatted_price and product.price_and_currency:
                    gift = Gift(name=product.title,
                                link=product.offer_url,
                                image_link=product.large_image_url,
                                price_desc=product.formatted_price,
                                price=product.price_and_currency[0])
                    gift.save()

    else:
        context = {}
    return render(request, 'azsearch/search.html', context)
コード例 #28
0
def fetch_random(term=None, num_results=5):
    """
    Build a random search term of 2-3 words and perform an Amazon product
    search.

    :return:
        A tuple containing an instance of :class:`amazon.api.AmazonSearch` and
        the search term used.
    """

    if not term:
        search_term = ''
        words = Word.objects.order_by('?')[:randint(2,3)]
        for word in words:
            search_term = "%s %s" % (word, search_term.strip())
    else:
        search_term = term
    print search_term
    amazon = AmazonAPI(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, settings.AWS_ASSOCIATE_TAG)
    products = amazon.search_n(num_results, Keywords="%s" % search_term, SearchIndex='All')
    return products, search_term
コード例 #29
0
ファイル: views.py プロジェクト: raccoonyy/amazoff
def book_search_in_amazon(request):
    if ("q" in request.GET) and (request.GET["q"] is not "") and request.GET["q"].strip():
        query_string = request.GET["q"]

        amazon = AmazonAPI(settings.AMAZON_ACCESS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_ASSOC_TAG)
        amazon_books = amazon.search_n(40, Keywords=query_string, SearchIndex="Books", BrowseNode="5", Sort="daterank")

        books = []
        for amazon_book in amazon_books:
            if amazon_book.isbn is None:
                continue
            book = Book.objects.trans_amazon_to_book(amazon_book)
            b = Book.objects.filter(isbn=amazon_book.isbn)
            if len(b) > 0:
                book["pk"] = b[0].pk
            else:
                book["pk"] = 0
            books.append(book)

        variables = {"books": books, "search_in_amazon": True}
    return variables
コード例 #30
0
def amazon_api_request_price():
    item_id = request.json.get('item_id')
    brand = request.json.get('brand')
    model = request.json.get('model')

    item = Item.query.filter_by(id=item_id).first()

    amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG)

    if item:
        try:
            products = amazon.search_n(1,
                                       Brand=brand,
                                       Title=model,
                                       SearchIndex='SportingGoods',
                                       ResponseGroup='VariationSummary')
            product_info = {
                'offer_url': products[0].offer_url,
                'variation_summary': {
                    'LowestPrice': {
                        'FormattedPrice':
                        products[0].parsed_response.VariationSummary.
                        LowestPrice["FormattedPrice"].text,
                        'Amount':
                        products[0].parsed_response.VariationSummary.
                        LowestPrice["Amount"].text
                    },
                    'HighestPrice': {
                        'FormattedPrice':
                        products[0].parsed_response.VariationSummary.
                        HighestPrice["FormattedPrice"].text
                    }
                }
            }

        except:
            error = {'error': 'amazon api exception'}
            return json.dumps(error)

    return json.dumps(product_info)
コード例 #31
0
    def handle(self, *args, **options):
        amazon = AmazonAPI(settings.AMAZON_ACCESS_KEY, settings.AMAZON_SECRET_KEY, settings.AMAZON_ASSOC_TAG)
        yesterday = date.today() - timedelta(days=1)
        # pubs = Publisher.objects.filter(last_updated__gte=yesterday)
        pubs = Publisher.objects.all()

        for pub in pubs:
            print "=== Check for publisher: \'%s\'" % pub
            amazon_books = amazon.search_n(30, SearchIndex='Books', Publisher=pub, BrowseNode='5', Power='pubdate:after 2012', Sort='daterank')

            for amazon_book in amazon_books:
                if amazon_book.isbn is None:
                    continue
                try:
                    dbbook = Book.objects.get(isbn__exact=amazon_book.isbn)
                    dbbook.update(amazon_book)
                except Book.DoesNotExist:
                    Book.objects.create_book(amazon_book)
            pub.save()
            time.sleep(4)

        print "=== Successful downloaded new books"
コード例 #32
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)
コード例 #33
0
ファイル: amazon_api_utils.py プロジェクト: Rositsazz/Movri
class AmazonAPIRequest:
    AMAZON_ACCESS_KEY = ''
    AMAZON_SECRET_KEY = ''
    AMAZON_ASSOC_TAG = ''

    def __init__(self):
        self.amazon = AmazonAPI(self.AMAZON_ACCESS_KEY,
                                self.AMAZON_SECRET_KEY,
                                self.AMAZON_ASSOC_TAG,
                                region='UK')

    def send_request(self, movie_name=''):
        try:
            movie = self.amazon.search_n(1,
                                         Keywords=movie_name,
                                         SearchIndex='DVD')
            print("Movie found in Amazon")
        except Exception:
            raise MovieDoesNotExistException(
                'This is not a movie name - {movie_name}'.format(
                    movie_name=movie_name))

        return movie[0]
コード例 #34
0
ファイル: AmazonController.py プロジェクト: BeeShall/Nora
class AmazonController:
    def __init__(self):
        cred = AWSCredentials.getAWSCredentials()
        self.amazon = AmazonAPI(cred['ACCESS_KEY'], cred['SECRET_KEY'],
                                cred['LOCALE'])

    def searchProduct(self, product):

        tokens = product['tokens']
        tags = product['tags']

        print("Searching for " + str(tokens))

        query = self.amazon.search_n(1, Keywords=tokens, SearchIndex="All")[0]

        result = {
            'title': query.title,
            'price': query.price_and_currency,
            'url': query.offer_url,
            'image': query.small_image_url
        }

        return result
コード例 #35
0
                ('192.168.2.255', 6001))
            s.sendto(
                "dash,{counter},keyword,{keyword}".format(counter=counter,
                                                          keyword=keyword), 0,
                ('192.168.2.255', 6001))
            s.sendto(
                "dash,{counter},keyword,{keyword}".format(counter=counter,
                                                          keyword=keyword), 0,
                ('192.168.2.255', 6001))
            counter += 1
        except:  # no keyword found
            continue

        # Try finding a product using our keyword, and jump to the next loop of there was none
        try:
            products = amazon.search_n(1, Keywords=keyword, SearchIndex='All')
            print products[0].asin
        except Exception, e:
            print e
            continue

        # Lookup info of our product, and jump to the next loop of there was none
        try:
            product = amazon.lookup(ItemId=products[0].asin)
            if product.large_image_url == None:
                continue
        except Exception, e:
            print "error lookup"
            print e
            continue
コード例 #36
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.price_and_currency, (109.0, 'USD'))
        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'})

    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")
コード例 #37
0
def main():
    #Read setup, Reddit, and Amazon configurations from config.ini
    config = ConfigParser.ConfigParser()
    config.read("config.ini")
    NUM_RETRIEVE = int(
        config.get("setup", "NUM_RETRIEVE")
    )  #Number of submissions to retrieve from "New" in each subreddit
    MIN_CONFIDENCE = int(config.get(
        "setup",
        "MIN_CONFIDENCE"))  #Minimum confidence to reply to keyword call
    SENTENCES_IN_REPLY = int(config.get(
        "setup", "SENTENCES_IN_REPLY"))  #Maximum number of sentences in reply
    SLEEP_TIME = int(config.get("setup",
                                "SLEEP_TIME"))  #Time between pulls, in seconds
    USERNAME = config.get("praw", "USERNAME")
    PASSWORD = config.get("praw", "PASSWORD")
    USER_AGENT = config.get("praw", "USER_AGENT")
    AMAZON_KEY = config.get("amazon", "AMAZON_KEY")
    AMAZON_SECRET = config.get("amazon", "AMAZON_SECRET")
    AMAZON_ASSOCIATE = config.get("amazon", "AMAZON_ASSOCIATE")

    #Initialize variables
    global keywords, c
    alreadyReplied = [
    ]  #Users and submissions that have already been replied to
    keywords = pandas.read_csv(
        'data.csv')  #Items, suggestives, blacklist, subreddits
    conn = sqlite3.connect(
        'templates.db')  #Sentence templates (brand, category, price, link)
    c = conn.cursor()
    subreddits = "+".join(
        [line.strip() for line in keywords['subreddits'].dropna()])

    #Connect to Reddit and Amazon
    r = praw.Reddit(USER_AGENT)
    r.login(USERNAME, PASSWORD, disable_warning=True)
    amazon = AmazonAPI(AMAZON_KEY, AMAZON_SECRET, AMAZON_ASSOCIATE)

    while True:
        submissions = r.get_subreddit(subreddits)
        #Get 'NUM_RETRIEVE' newest submissions from subreddits
        for i in submissions.get_new(limit=NUM_RETRIEVE):
            try:
                if str(i.author) in alreadyReplied:
                    raise ValueError('SKIPPING: ALREADY REPLIED TO AUTHOR')
                if 'reddit.com' not in i.url:
                    raise ValueError('SKIPPING: LINK SUBMISSION')

                #Amazon link in submission (self) text
                str_self = i.selftext.encode('utf-8').lower()
                if ('/dp/' in str_self) or ('/gp/' in str_self):
                    productData = find_in_amazon(
                        amazon, AMAZON_ASSOCIATE,
                        amazon.similarity_lookup(ItemId=get_ASIN(str_self))[0])
                    if type(productData) is dict:
                        print "FOUND", productData['link'], "IN SELF:", i.id
                        alreadyReplied.append(str(
                            i.author))  #Add username to cache
                        print ""
                        print generate_Comment(
                            SENTENCES_IN_REPLY, productData['link'],
                            productData['brand'],
                            str(productData['category']).lower(),
                            productData['price'], productData['features'],
                            productData['reviews'])
                        i.add_comment(
                            generate_Comment(
                                SENTENCES_IN_REPLY, productData['link'],
                                productData['brand'],
                                str(productData['category']).lower(),
                                productData['price'], productData['features'],
                                productData['reviews']))
                        print ""
                        raise ValueError('SKIPPING: DONE REPLYING')
                    elif type(productData) is str:
                        print productData  #Error

                #Amazon link in comment
                for comment in i.comments:
                    str_comment = str(comment).lower()
                    if (str(comment.author) not in alreadyReplied) and (
                        ('/dp/' in str_comment) or ('/gp/' in str_comment)):
                        productData = find_in_amazon(
                            amazon, AMAZON_ASSOCIATE,
                            amazon.similarity_lookup(
                                ItemId=str(get_ASIN(str_comment)))[0])
                        if type(productData) is dict:
                            print "FOUND", productData[
                                'link'], "IN COMMENT", comment.id
                            alreadyReplied.append(str(
                                i.author))  #Add username to cache
                            alreadyReplied.append(str(
                                comment.author))  #Add username to cache
                            print ""
                            print generate_amazonCommentReply(
                                SENTENCES_IN_REPLY, productData['link'],
                                productData['brand'],
                                str(productData['category']).lower(),
                                productData['price'], productData['features'],
                                productData['reviews'])
                            comment.reply(
                                generate_amazonCommentReply(
                                    SENTENCES_IN_REPLY, productData['link'],
                                    productData['brand'],
                                    str(productData['category']).lower(),
                                    productData['price'],
                                    productData['features'],
                                    productData['reviews']))
                            print ""
                            raise ValueError('SKIPPING: DONE REPLYING')
                        elif type(productData) is str:
                            print productData  #Error

                #Item keyword in title
                for word in keywords['items'].dropna(
                ):  #Scan matches between 'items' and title
                    if word.lower() in i.title.encode('utf-8').lower(
                    ):  #Only reply if match found and confidence is high that a suggestion is advised
                        if calculate_confidence(i) >= MIN_CONFIDENCE:
                            productData = find_in_amazon(
                                amazon, AMAZON_ASSOCIATE,
                                amazon.search_n(1,
                                                Keywords=word,
                                                SearchIndex='All')[0])
                            if type(productData) is dict:
                                print "FOUND", word, "IN TITLE", i.id
                                alreadyReplied.append(str(
                                    i.author))  #Add username to cache
                                productData[
                                    'category'] = word  #'word' is more relevant than the default category
                                print ""
                                print generate_Comment(
                                    SENTENCES_IN_REPLY, productData['link'],
                                    productData['brand'],
                                    str(productData['category']).lower(),
                                    productData['price'],
                                    productData['features'],
                                    productData['reviews'])
                                i.add_comment(
                                    generate_Comment(
                                        SENTENCES_IN_REPLY,
                                        productData['link'],
                                        productData['brand'],
                                        str(productData['category']).lower(),
                                        productData['price'],
                                        productData['features'],
                                        productData['reviews']))
                                print ""
                                raise ValueError('SKIPPING: DONE REPLYING')
                            elif type(productData) is str:
                                print productData  #Error
                #Let sprinkle some crack on'em and get outta here
                raise ValueError('SKIPPING: NOTHING FOUND')

            except KeyboardInterrupt:
                raise
            except ValueError as err:
                print err
            except:
                print sys.exc_info()[0]
        print 'SLEEPING FOR', SLEEP_TIME, 'SECONDS...'
        time.sleep(SLEEP_TIME)
コード例 #38
0
SECRET_KEY = "#+##/##"
ASSOC_TAG = "##-##"

import bottlenose
from lxml import objectify
from amazon.api import AmazonAPI
import amazon_scraper
import json
from pprint import pprint
from textblob import TextBlob
from nltk import sent_tokenize

amazon = AmazonAPI(ACCESS_KEY_ID, SECRET_KEY, ASSOC_TAG)
search_item=raw_input()
#search_item='iphone 6'
products = amazon.search_n(5, Keywords=search_item, SearchIndex='All')
#products=products[0:10]
asinlist=[]
title_list=[]
print("Scanning these products...obtaining results")
for product in products:
	asinlist.append(product.asin)
	print product.title
	title_list.append(product.title)

#amazon.call(asinlist[0])
amazon_scraper.ReadAsin(asinlist)

data=open('data.json','r')
reviews = json.loads(data.read())
data.close()
コード例 #39
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)
コード例 #40
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)
コード例 #41
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)
コード例 #42
0
class AWSClient(object):
    """
    """
    def __init__(
            self,
            region=None,
            root_access_key='AKIAJB4BJYPJKV5YACXQ',
            root_secret_access_key='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',
            tag=None):

        self.region = 'us-east-1'
        self.root_access_key = root_access_key
        self.root_secret_access_key = root_secret_access_key
        self.search_access_key = 'AKIAIMJ3KXAGVLAEFBNA'  #affiliate key
        self.search_secret_access_key = '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'  #affiliate key
        self.associate_tag = "msucapstone02-20"
        self.create_comprehend_client()
        self.create_search_client()
        self.create_transcribe_client()

    def create_client(self, service):
        """
        """
        return boto3.client(service,
                            region_name=self.region,
                            aws_access_key_id=self.root_access_key,
                            aws_secret_access_key=self.root_secret_access_key)

    def create_transcribe_client(self):
        """
        """
        self.transcribe_client = self.create_client('transcribe')

    def create_comprehend_client(self):
        """
        """
        self.comprehend_client = self.create_client('comprehend')

    def create_search_client(self):
        self.search_client = AmazonAPI(self.search_access_key,
                                       self.search_secret_access_key,
                                       self.associate_tag)

    def run_transcribe_job(self):
        pass

    def comprehend_entities(self, text_input):
        response = self.comprehend_client.detect_entities(Text=text_input,
                                                          LanguageCode='en')

        return response

    def comprehend_key_phrases(self, text_input):
        response = self.comprehend_client.detect_key_phrases(Text=text_input,
                                                             LanguageCode='en')

        return response

    def comprehend_sentiment(self, text_input):
        response = self.comprehend_client.detect_sentiment(Text=text_input,
                                                           LanguageCode='en')

        return response

    def search_n(self, keywords, index, n):
        return self.search_client.search_n(n,
                                           Keywords=keywords,
                                           SearchIndex=index)
コード例 #43
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)
コード例 #44
0
ファイル: reffit.py プロジェクト: winsonluk/reffit
def main():
    # Configure variables from config.ini.
    config = configparser.ConfigParser()
    config.read('config.ini')
    NUM_RETRIEVE = int(config.get('setup', 'NUM_RETRIEVE'))
    MIN_CONFIDENCE = int(config.get('setup', 'MIN_CONFIDENCE'))
    SENTENCES_IN_REPLY = int(config.get('setup', 'SENTENCES_IN_REPLY'))
    SLEEP_TIME = int(config.get('setup', 'SLEEP_TIME'))
    USER_AGENT = config.get('praw', 'USER_AGENT')
    AMAZON_KEY = config.get('amazon', 'AMAZON_KEY')
    AMAZON_SECRET = config.get('amazon', 'AMAZON_SECRET')
    AMAZON_ASSOCIATE = config.get('amazon', 'AMAZON_ASSOCIATE')

    # Initialize variables.
    global keywords, c
    alreadyReplied = []  # Tracks users whom the bot has replied to.
    keywords = pandas.read_csv('data.csv')
    conn = sqlite3.connect('templates.db')
    c = conn.cursor()
    subreddits = '+'.join([line for line in keywords['subreddits'].dropna()])

    # Connect to Reddit and Amazon.
    r = praw.Reddit(USER_AGENT)
    o = OAuth2Util.OAuth2Util(r)
    amazon = AmazonAPI(AMAZON_KEY, AMAZON_SECRET, AMAZON_ASSOCIATE)

    while True:
        o.refresh()
        submissions = r.get_subreddit(subreddits)
        posts = submissions.get_new(limit=NUM_RETRIEVE)
        for i in posts:
            try:
                if str(i.author) in alreadyReplied:
                    raise ValueError('SKIPPING: ALREADY REPLIED TO AUTHOR')
                if 'reddit.com' not in i.url:
                    raise ValueError('SKIPPING: LINK SUBMISSION')

                # Amazon link is found in submission (self) text.
                selfStr = i.selftext.encode('ascii', 'ignore').lower()
                if ('/dp/' in selfStr) or ('/gp/' in selfStr):
                    productData = find_in_amazon(
                        amazon, AMAZON_ASSOCIATE,
                        amazon.similarity_lookup(ItemId=get_asin(selfStr))[0])
                    if type(productData) is dict:
                        print('FOUND', productData['link'], 'IN SELF:', i.id)
                        alreadyReplied.append(str(i.author))
                        print('')
                        print(
                            generate_comment(
                                SENTENCES_IN_REPLY, productData['link'],
                                productData['brand'],
                                str(productData['category']).lower(),
                                productData['price'], productData['features'],
                                productData['reviews']))
                        #comment.reply(
                        #    generate_comment(
                        #        SENTENCES_IN_REPLY, productData['link'],
                        #        productData['brand'],
                        #        str(productData['category']).lower(),
                        #        productData['price'],
                        #        productData['features'],
                        #        productData['reviews']
                        #        )
                        #    )
                        print('')
                        raise ValueError('SKIPPING: DONE REPLYING')
                    elif type(productData) is str:
                        print(productData)  # Error message

                # Amazon link is found in comment.
                for comment in i.comments:
                    commentStr = str(comment).encode('ascii', 'ignore').lower()
                    if ((str(comment.author) not in alreadyReplied) and
                        (('/dp/' in commentStr) or ('/gp/' in commentStr))):
                        productData = find_in_amazon(
                            amazon, AMAZON_ASSOCIATE,
                            amazon.similarity_lookup(
                                ItemId=str(get_asin(commentStr)))[0])
                        if type(productData) is dict:
                            print(('FOUND', productData['link'], 'IN COMMENT',
                                   comment.id))
                            alreadyReplied.append(str(i.author))
                            alreadyReplied.append(str(comment.author))
                            print('')
                            print(
                                generate_comment_with_reply(
                                    SENTENCES_IN_REPLY, productData['link'],
                                    productData['brand'],
                                    str(productData['category']).lower(),
                                    productData['price'],
                                    productData['features'],
                                    productData['reviews']))
                            #comment.reply(
                            #    generate_comment_with_reply(
                            #        SENTENCES_IN_REPLY, productData['link'],
                            #        productData['brand'],
                            #        str(productData['category']).lower(),
                            #        productData['price'],
                            #        productData['features'],
                            #        productData['reviews']
                            #        )
                            #    )
                            print('')
                            raise ValueError('SKIPPING: DONE REPLYING')
                        elif type(productData) is str:
                            print(productData)  # Error message

                # Item keyword is found in title.
                for word in keywords['items'].dropna():
                    if (word.lower() in i.title.encode('ascii',
                                                       'ignore').lower()):
                        if calculate_confidence(i) >= MIN_CONFIDENCE:
                            productData = find_in_amazon(
                                amazon, AMAZON_ASSOCIATE,
                                amazon.search_n(1,
                                                Keywords=word,
                                                SearchIndex='All')[0])
                            if type(productData) is dict:
                                print('FOUND', word, 'IN TITLE', i.id)
                                alreadyReplied.append(str(i.author))
                                productData['category'] = word
                                print('')
                                print(
                                    generate_comment(
                                        SENTENCES_IN_REPLY,
                                        productData['link'],
                                        productData['brand'],
                                        str(productData['category']).lower(),
                                        productData['price'],
                                        productData['features'],
                                        productData['reviews']))
                                #comment.reply(
                                #    generate_comment(
                                #        SENTENCES_IN_REPLY,
                                #        productData['link'],
                                #        productData['brand'],
                                #        str(productData['category']).lower(),
                                #        productData['price'],
                                #        productData['features'],
                                #        productData['reviews']
                                #        )
                                #    )
                                print('')
                                raise ValueError('SKIPPING: DONE REPLYING')
                            elif type(productData) is str:
                                print(productData)  # Error message

                raise ValueError('SKIPPING: NOTHING FOUND')
            except KeyboardInterrupt:
                raise
            except ValueError as err:
                print(err)
            except:
                print('ERROR: PROBLEM IN MAIN')
                print(sys.exc_info()[0])
        print('SLEEPING FOR', SLEEP_TIME, 'SECONDS...')
        time.sleep(SLEEP_TIME)
コード例 #45
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)
コード例 #46
0
ファイル: title_gore.py プロジェクト: aj-n/title_gore
def main(argv):
    # Configuration data
    config = parse_args(sys.argv)

    # Load up apis
    amazon = AmazonAPI(config['aws']['access_key'],
                       config['aws']['secret_key'],
                       config['aws']['associate_tag'])

    # Parse 'em
    items = []
    last_item = None
    unknowns = []
    with open(config['files']['input']) as f:
        for line in f:
            # Extract Author - Title
            author = None
            title = None
            for pattern in config['title_patterns']:
                matches = re.search(pattern, line)

                # No match -> try again
                if matches is None:
                    continue

                # Sanity checks on match
                grps = m.groups()
                if grps is None:
                    continue

                # Correct lengths
                if len(grps) != 2:
                    continue

                # Extract
                author = grps[0].strip()
                title = grps[1].strip()

            # Did we get a hit? If not, skip this line
            if matches is None or author is None or title is None:
                debug('No matches for:', line, end='')
                unknowns.append({
                    'line': line,
                    'author': author,
                    'title': title
                })
                continue

            debug('Lookup "', title, '" by ', author, sep='')
            product = None
            try:
                # Do that spicy lookup
                products = amazon.search_n(
                    1,
                    Power='author:{} and title:{}'.format(author, title),
                    SearchIndex='Books')
                if products is not None or len(products) > 0:
                    product = products[0]
                    continue

                # Sanity check
                if product is not None:
                    debug('\tRank = ',
                          product.sales_rank,
                          ', Pages = ',
                          product.pages,
                          ', Pubdate = ',
                          product.publication_date,
                          sep='')
                    items.append(product)
            except:
                debug('\tSearch failed')

            # Handle not found
            if product is None:
                debug('\tEmpty match')
                unknowns.append({
                    'line': line,
                    'author': author,
                    'title': title
                })

    # Sort
    items.sort(key=lambda x: x.sales_rank)

    # Output
    w = None
    if config['files']['output'] is not None:
        w = open(config['files']['output'], 'w')
    else:
        w = sys.stdout

    # Spit ranked items first, then not founds
    for item in items:
        w.write(
            'rank={}, asin={}, title={}, pages={}, published={}\r\n'.format(
                item.sales_rank, item.asin, item.tile, item.pages,
                item.publication__date))
    for unknown in unknowns:
        w.write('rank={}, author={}, title={}, line={}\r\n'.format(
            None, None, unknown['author'], unknown['title'], unknown['line']))
コード例 #47
0
    def get(self, request):
        """
        タイトルからAmazon本を取得
        ---
        parameters:
                -   name: title
                    type: string
                    paramType: query
                -   name: original_title
                    type: string
                    paramType: query
                -   name: isbn
                    type: string
                    paramType: query
        """
        title = request.GET.get("title", "")
        original_title = request.GET.get("original_title", "")
        isbn = request.GET.get("isbn", None)
        amazon = AmazonAPI(AMAZON_ACCESS_KEY,
                           AMAZON_ACCESS_SECRET,
                           AMAZON_ACCOS_TAG,
                           region="JP",
                           ErrorHandler=self.error_handler)
        if isbn:
            keyword = isbn.replace("-", "")
        else:
            keyword = u'"' + title + u'"|"' + original_title + u'"'

        try:
            products = amazon.search_n(20,
                                       Keywords=keyword,
                                       SearchIndex=u'All')
        except UnicodeEncodeError:
            return Response({
                "isbn_product": None,
                "translated_product": None,
                "original_product": None,
                "other_books": []
            })

        logging.info(products)
        products = self.remove(products)

        isbn_product = None
        if isbn:
            isbn_product = products[0]

        original_product = self.most_similarity_product(products,
                                                        original_title,
                                                        is_englsih=True)
        if original_product:
            products.remove(original_product)
        translated_product = self.most_similarity_product(products, title)
        if translated_product:
            products.remove(translated_product)

        results = {
            "isbn_product":
            self.serialize_product(isbn_product) if isbn_product else {},
            "translated_product":
            self.serialize_product(translated_product)
            if translated_product else {},
            "original_product":
            self.serialize_product(original_product)
            if original_product else {},
            "other_books":
            [self.serialize_product(product) for product in products]
        }
        return Response(results)
コード例 #48
0
ファイル: app.py プロジェクト: aiweithesushigirl/Price-comp
class amazon_search(object):
    """docstring for boqi_search"""
    def __init__(self, search_word):
        self.search_word = search_word
        self.AMAZON_ACCESS_KEY = "AKIAJBVSXYPBD6LUXTSQ"
        self.AMAZON_SECRET_KEY = "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"
        self.AMAZON_ASSOC_TAG = "wawwawwaw1204-20"
        self.amazon = AmazonAPI(self.AMAZON_ACCESS_KEY, self.AMAZON_SECRET_KEY,
                                self.AMAZON_ASSOC_TAG)
        self.AsinList = []
        self.extracted_data = []

    def get_searchword_from_user(self):

        self.products = self.amazon.search_n(20,
                                             Keywords=self.search_word,
                                             SearchIndex='All')
        for i, self.product in enumerate(self.products):
            print(self.product.asin)
            self.AsinList.append(self.product.asin)

    def AmazonParser(self, url):
        headers = {
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36'
        }
        page = requests.get(url, headers=headers)

        while True:
            sleep(3)
            try:
                doc = html.fromstring(page.content)

                XPATH_NAME = '//h1[@id="title"]//text()'
                # XPATH_SALE_PRICE = '//span[contains(@id,"ourprice") or contains(@id,"saleprice")]/text()'
                XPATH_ORIGINAL_PRICE = '//td[contains(text(),"List Price") or contains(text(),"M.R.P") or contains(text(),"Price")]/following-sibling::td/text()'
                XPATH_SIZE = '//*[@id="variation_size_name"]/div/span/text()'

                RAW_NAME = doc.xpath(XPATH_NAME)
                # RAW_SALE_PRICE = doc.xpath(XPATH_SALE_PRICE)
                RAW_SIZE = doc.xpath(XPATH_SIZE)
                RAW_ORIGINAL_PRICE = doc.xpath(XPATH_ORIGINAL_PRICE)
                #RAw_AVAILABILITY = doc.xpath(XPATH_AVAILABILITY)

                NAME = ' '.join(
                    ''.join(RAW_NAME).split()) if RAW_NAME else None
                # SALE_PRICE = ' '.join(''.join(RAW_SALE_PRICE).split()).strip() if RAW_SALE_PRICE else None
                SIZE = ' > '.join([i.strip()
                                   for i in RAW_SIZE]) if RAW_SIZE else None
                ORIGINAL_PRICE = ''.join(
                    RAW_ORIGINAL_PRICE).strip() if RAW_ORIGINAL_PRICE else None

                if page.status_code != 200:
                    raise ValueError('captha')
                data = {
                    'NAME': NAME,
                    # 'SALE_PRICE':SALE_PRICE,
                    'SIZE': SIZE,
                    'ORIGINAL_PRICE': ORIGINAL_PRICE,
                }

                return data
            except Exception as e:
                print(e)

    def ReadAsin(self):
        self.csv_columns = [
            'NAME',
            # 'SALE_PRICE',
            'SIZE',
            'ORIGINAL_PRICE',
            #'AVAILABILITY',
            'URL'
        ]

        for asin in self.AsinList:
            self.url_search = "http://www.amazon.com/dp/" + asin

            self.extracted_data.append(self.AmazonParser(self.url_search))
            sleep(5)

        with open(self.search_word + '_csv', 'w') as csvfile:
            self.writer = csv.DictWriter(csvfile, fieldnames=self.csv_columns)
            self.writer.writeheader()
            for data in self.extracted_data:
                self.writer.writerow(data)
コード例 #49
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)
コード例 #50
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)
コード例 #51
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)
コード例 #52
0
from amazon.api import AmazonAPI

amazon = AmazonAPI("AKIAJEXNJSWHTI5YMVDQ",
                   "iOiwcsWeR0+Fu4SH2EGlqv7T6MKBD2mOsqMrNcJ6", "getpic-20")
products = amazon.search_n(1, Keywords='kindle', SearchIndex='All')

print(len(products))
コード例 #53
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)
コード例 #54
0
import django
import urllib2
import json
import time

django.setup()

from main.models import *
from amazon.api import AmazonAPI

#Access Key, Secret Key, Assoc Tag
amazon = AmazonAPI('AKIAJWBKRIZ6TNTNDR7A', 'HU67Mum3v7obUhfl7bdB24uxV497eqj7L4g4vQaW', 'hac078-20')

for book in Textbook.objects.all():
        #Input any keywords here
        keywordInput = book.isbn
        try:
                products = amazon.search_n(1, Keywords=keywordInput, SearchIndex='All')
                if len(products) > 0:
                        book.amazon_price = products[0].price_and_currency[0]
                        print "Found Book: ", book.id, book.amazon_price
                        book.cover_url = products[0].large_image_url
                        book.save()
        except:
                pass