• Stars
    star
    578
  • Rank 77,250 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 14 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Python wrapper for the Amazon Product Advertising API.

Version Version License Versions

Bottlenose is a thin, well-tested, maintained, and powerful Python wrapper over the Amazon Product Advertising API. There is practically no overhead, and no magic (unless you add it yourself).

Before you get started, make sure you have both Amazon Product Advertising and AWS accounts. AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_ASSOCIATE_TAG are all from your Amazon Associate Account.

Features

  • Compatible with Python versions 2.4 and up
  • Support for AU, BR, CA, CN, DE, ES, FR, IN, IT, JP, MX, UK, and US Amazon Product Advertising API endpoints
  • No requirements, except simplejson for Python versions before 2.6
  • Configurable query parsing
  • Configurable throttling for batches of queries
  • Configurable query caching
  • Configurable error handling and retries

Usage

pip

pip install bottlenose

or

python3 -m pip install bottlenose

Then, using your AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_ASSOCIATE_TAG:

import bottlenose
amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG)
response = amazon.ItemLookup(ItemId="B007OZNUCE")

You can then parse the response output to view item information.

Troubleshooting

  • If you need help or would like to ask a general question, use Stack Overflow. Apply the 'bottlenose' tag to your question to get help faster.
  • If you found a bug or have a feature request, open an issue.
  • If you want to contribute, submit a pull request. If it's a big change, please open an issue first to discuss implementation.

Advanced Usage

1. Available Search Methods

Region Endpoint

The default Region is set to US (webservices.amazon.com). To specify another endpoint, simply set the Region parameter with the request. For example, to specify the French endpoint (webservices.amazon.fr), set the Region parameter to FR:

amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG, Region='FR')

Supported values for the Region parameter are CA, CN, DE, ES, FR, IN, IT, JP, UK, and US (default).

Your Amazon Product Advertising account (AWS_ASSOCIATE_TAG) must exist for the given endpoint, otherwise, you'll get an HTTP 400 error ('Bad Request').

Search for a Specific Item
response = amazon.ItemLookup(ItemId="B007OZNUCE")
Search for Items by Keywords
response = amazon.ItemSearch(Keywords="Kindle 3G", SearchIndex="All")
Search for Images for an item
response = amazon.ItemLookup(ItemId="1449372422", ResponseGroup="Images")
Search for Similar Items
response = amazon.SimilarityLookup(ItemId="B007OZNUCE")

2. Available Shopping Related Methods

Required
amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG)
Create a cart
response = amazon.CartCreate(...)
Adding to a cart
response = amazon.CartAdd(CartId, ...)
Get a cart by ID
response = amazon.CartGet(CartId, ...)
Modifying a cart
response = amazon.CartModify(ASIN, CartId,...)
Clearing a cart
response = amazon.CartClear(CartId, ...)

3. Sample Code

import bottlenose
amazon = bottlenose.Amazon(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG)
response = amazon.ItemLookup(ItemId="0596520999", ResponseGroup="Images",
    SearchIndex="Books", IdType="ISBN")
print(response)
# <?xml version="1.0" ?><ItemLookupResponse xmlns="http://webservices.amazon...

Here is another example.

response = amazon.ItemSearch(Keywords="Kindle 3G", SearchIndex="All")
# <?xml version="1.0" ?><ItemSearchResponse xmlns="http://webservices.amazon...

Bottlenose can also read your credentials from the environment automatically; just set $AWS_ACCESS_KEY_ID, $AWS_SECRET_ACCESS_KEY and $AWS_ASSOCIATE_TAG.

Any valid API call from the following is supported (in addition to any others that may be added in the future). Just plug in appropriate request parameters for the operation you'd like to call, and you're good to go.

BrowseNodeLookup
CartAdd
CartClear
CartCreate
CartGet
CartModify
ItemLookup
ItemSearch
SimilarityLookup

You can refer here for a full listing of API calls to be made from Amazon.


For more information about these calls, please consult the Product Advertising API Developer Guide.

Parsing

By default, API calls return the response as a raw bytestring. You can change this with the Parser constructor argument. The parser is a callable that takes a single argument, the response as a raw bytestring, and returns the parsed response in a format of your choice.

For example, to parse responses with BeautifulSoup:

import bottlenose
from bs4 import BeautifulSoup

amazon = bottlenose.Amazon(
    AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ASSOCIATE_TAG,
    Parser=lambda text: BeautifulSoup(text, 'xml')
)
results = amazon.ItemLookup(ItemId="0198596790", ResponseGroup="SalesRank")

print(results.find('SalesRank').string)
# 168088

Throttling/Batch Mode

Amazon strictly limits the query rate on its API (by default, one query per second per associate tag). If you have a batch of non-urgent queries, you can use the MaxQPS argument to limit them to no more than a certain rate; any faster, and bottlenose will sleep() until it is time to make the next API call.

Generally, you want to be just under the query limit, for example:

amazon = bottlenose.Amazon(MaxQPS=0.9)

If some other code is also querying the API with your associate tag (for example, a website backend), you'll want to choose an even lower value for MaxQPS.

Caching

You can often get a major speedup by caching API queries. Use the CacheWriter and CacheReader constructor arguments.

CacheWriter is a callable that takes two arguments, a cache url, and the raw response (a bytestring). It will only be called after successful queries.

CacheReader is a callable that takes a single argument, a cache url, and returns a (cached) raw response, or None if there is nothing cached.

The cache url is the actual query URL with authentication information removed. For example:

http://webservices.amazon.com/onca/xml?Keywords=vacuums&Operation=ItemSearch&Region=US&ResponseGroup=SearchBins&SearchIndex=All&Service=AWSECommerceService&Version=2013-08-01

Example code:

def write_query_to_db(cache_url, data):
    ...

def read_query_from_db(cache_url):
    ...

amazon = bottlenose.Amazon(CacheWriter=write_query_to_db,
                           CacheReader=read_query_from_db)

Note that Amazon's Product Advertising API Agreement only allows you to cache queries for up to 24 hours.

Error Handling

Sometimes the Amazon API returns errors; for example, if you have gone over your query limit, you'll get a 503. The ErrorHandler constructor argument gives you a way to keep track of such errors, and to retry queries when you receive a transient error.

ErrorHandler should be a callable that takes a single argument, a dictionary with these keys:

  • api_url: the actual URL used to call the API
  • cache_url: api_url minus authentication information
  • exception: the exception raised (usually an HTTPError or URLError)

If your ErrorHandler returns true, the query will be retried. Here's some example code that does exponential backoff after throttling:

import random
import time
from urllib2 import HTTPError

def error_handler(err):
    ex = err['exception']
    if isinstance(ex, HTTPError) and ex.code == 503:
        time.sleep(random.expovariate(0.1))
        return True

amazon = bottlenose.Amazon(ErrorHandler=error_handler)

License

Apache License, Version 2.0. See LICENSE for details.

More Repositories

1

pinboard.py

A full-featured Python wrapper (and command-line utility) for the Pinboard API. Built by the makers of Pushpin for Pinboard.
Python
342
star
2

openradar-mirror

A mirror of radars pulled from http://openradar.me/.
Python
246
star
3

django-pyodbc

An ODBC-powered MS SQL Server DB backend for Django 1.4+
Python
203
star
4

git-bigstore

Bigstore is a Git extension that helps you track big files in your repositories.
Python
183
star
5

ConcentricProgressRingView

Fully customizable circular progress bar written in Swift.
Swift
143
star
6

LHSKeyboardAdjusting

An easy-to-use Objective-C protocol that automatically resizes / adjusts views when a keyboard appears on iOS.
Objective-C
100
star
7

ASPinboard

A modern, fast, and flexible Objective-C library for Pinboard.in.
Objective-C
83
star
8

TipJarViewController

Easy, drop-in tip jar for iOS apps.
Swift
78
star
9

requests-cloudkit

Apple CloudKit Python library.
Python
70
star
10

django-template

A battle-tested Django 2.1 project template with configurations for AWS, Heroku, App Engine, and Docker.
Python
64
star
11

in_app_purchase_receipt_verifier

A simple, one-click deploy web app to simplify the process of validating In-App Purchase receipts on the App Store.
Python
58
star
12

python-harvest

A Python wrapper for the Harvest time-tracking API.
Python
55
star
13

SuperLayout

SuperLayout is a Swift library that makes using Auto Layout a breeze.
Swift
52
star
14

django-on-appengine-archive

Jumpstart Django development on Google Appengine.
Python
49
star
15

pwnedpasswords

A Python Library and CLI for the Pwned Passwords v2 API
Python
47
star
16

KeyboardAdjuster

A Swift library that automatically resizes and adjusts views to scroll when a keyboard appears.
Swift
40
star
17

objectifier

Objectify your Python objects.
Python
37
star
18

TimeTracker-Linux

A Harvest client for Ubuntu Linux.
Python
21
star
19

Notchy

Notchy is an iOS app that makes pretty, shareable screenshots for the iPhone X and the iPhone XS.
Swift
20
star
20

LionheartExtensions

An invaluable collection of Swift extensions and utilities for iOS.
Swift
14
star
21

django-pwnedpasswords-validator

Django password validator that checks for passwords in known data breaches.
Python
13
star
22

python-onfleet

A full-featured Python wrapper for the Onfleet API.
Python
11
star
23

TableViewPopoverPresenting

Display popovers on taps over your table views cells. It was hard. Now it's easy.
Swift
9
star
24

QuickTableView

UITableView toolset for Swift.
Swift
9
star
25

python-leafly

Python
8
star
26

django-lionheart-helpers

A small collection of utilities for use with Django.
Python
8
star
27

django-statictastic

A Django app that makes it easy to sync static files to your storage backend of choice.
Python
8
star
28

python-restmapper

RestMapper takes the pain out of integrating with RESTful APIs.
Python
8
star
29

LionheartCurrencyTextField

A drop-in replacement for UITextField that displays currency values the way you’d expect it to.
Swift
8
star
30

LHSFontSelectionViewController

Objective-C
7
star
31

WelcomeViewController

A welcome view in the style of Apple's built-in apps.
Swift
7
star
32

LHSColorPickerView

Objective-C
4
star
33

fastlane-plugin-submit_to_beta_app_review

A simple plugin that submits an iTunes Connect build to Beta App Review.
Ruby
4
star
34

Health-XML-Splitter

A macOS utility that splits Health XML files into smaller chunks.
Swift
4
star
35

pushpin-localizations

Translations for Pushpin, a Pinboard client for iOS.
Shell
3
star
36

LHSDelicious

An easy-to-use Objective-C wrapper for the Delicious API.
Objective-C
3
star
37

Xcode-Templates

Makefile
3
star
38

LHSCategoryCollection

A collection of helpful categories for use in iOS projects.
Objective-C
3
star
39

style-guide

2
star
40

LHSCategoriesFlowView

Objective-C
2
star
41

iOSTipsTricks

Objective-C
2
star
42

IAPReceiptVerifier

Companion iOS library to https://github.com/lionheart/in_app_purchase_receipt_verifier
Swift
2
star
43

LHSKippt

not much to see here, move along
Objective-C
2
star
44

milestonemaker

Just a quick and dirty script that creates weekly milestones in GitHub issues.
Python
2
star
45

UncommonCrypto

Pure Swift wrapper for CommonCrypto.
HTML
2
star
46

LHSDiigo

Objective-C
2
star
47

LionheartOtherAppsViewController

A view controller that showcases all the apps you currently have on the App Store.
Swift
2
star
48

LHSTableViewCells

A Pod that allows you to make tableViewCells without subclassing them
Objective-C
1
star
49

LHSTwitterFollowUtility

A utility that allows your users to follow a specific twitter account.
Objective-C
1
star
50

mixpanel-people-delete

A one-click Heroku app that resets your Mixpanel People usage.
Python
1
star
51

github-issues-tools

Python
1
star
52

LHSCustomTransitionCollection

A collection of custom transitions for use in iOS 7+
Objective-C
1
star
53

releasemaker

Python
1
star
54

LionheartAlamofireClient

A simple class that helps manage interaction with remote APIs using Alamofire.
Swift
1
star
55

LHSOtherAppsViewController

A view controller to show your users the other apps that you have developed.
Objective-C
1
star
56

lionheart-tumblr-theme

The Lionheart Software Tumblr Blog Theme
CSS
1
star