This adds support for international amazon sites to pyamazon --md@hudora.de --- amazon.py.orig Mon May 5 21:13:02 2003 +++ amazon.py Sun Oct 19 21:18:38 2003 @@ -21,6 +21,14 @@ - a file called ".amazonkey" in the same directory as amazon.py - a file called "amazonkey.txt" in the same directory as amazon.py +We also try to find your associate ID by checking the associate_id +argument, module-level ASSOCIATE_ID variable (call setAssociateId once +to set it), AMAZON_ASSOCIATE_ID environment variable and the .amazonid +and amazonid.txt files in the same locations as above. + +If you want to access amazon.co.uk, amazon.de or amazon.jp you can +call setLocale once to set your locale. + Sample usage: >>> import amazon >>> amazon.setLicense('...') # must get your own key! @@ -50,16 +58,19 @@ - All functions can take type="lite" to get less detail in results - All functions can take page=N to get second, third, fourth page of results - All functions can take license_key="XYZ", instead of setting it globally +- All functions can take associate_id="XYZ", instead of setting it globally +- All functions can take locale="XY", instead of setting it globally - All functions can take http_proxy="http://x/y/z" which overrides your system setting """ __author__ = "Mark Pilgrim (f8dy@diveintomark.org)" -__version__ = "0.61" +__version__ = "0.61-intl" __cvsversion__ = "$Revision: 750 $"[11:-2] __date__ = "$Date: 2005-08-13 22:28:39 +0200 (Sat, 13 Aug 2005) $"[7:-2] __copyright__ = "Copyright (c) 2002 Mark Pilgrim" __license__ = "Python" # Powersearch and return object type fix by Joseph Reagle +# international support bei Max Dornseif from xml.dom import minidom import os, sys, getopt, cgi, urllib @@ -70,6 +81,8 @@ pass LICENSE_KEY = None +LOCALE = None +ASSOCIATE_ID = None HTTP_PROXY = None # don't touch the rest of these constants @@ -89,6 +102,20 @@ (lambda key: _contentsOf(_getScriptDir(), _amazonfile2), '%s in the amazon.py directory' % _amazonfile2) ) +_amazonidfile1 = ".amazonid" +_amazonidfile2 = "amazonid.txt" +_associateLocations = ( + (lambda key: key, 'passed to the function in associate_id variable'), + (lambda key: ASSOCIATE_ID, 'module-level ASSOCIATE_ID variable (call setAssociateId to set it)'), + (lambda key: os.environ.get('AMAZON_ASSOCIATE_ID', None), 'an environment variable called AMAZON_ASSOCIATE_ID'), + (lambda key: _contentsOf(os.getcwd(), _amazonidfile1), '%s in the current directory' % _amazonidfile1), + (lambda key: _contentsOf(os.getcwd(), _amazonidfile2), '%s in the current directory' % _amazonidfile2), + (lambda key: _contentsOf(os.environ.get('HOME', ''), _amazonidfile1), '%s in your home directory' % _amazonidfile1), + (lambda key: _contentsOf(os.environ.get('HOME', ''), _amazonidfile2), '%s in your home directory' % _amazonidfile2), + (lambda key: _contentsOf(_getScriptDir(), _amazonidfile1), '%s in the amazon.py directory' % _amazonidfile1), + (lambda key: _contentsOf(_getScriptDir(), _amazonidfile2), '%s in the amazon.py directory' % _amazonidfile2) + ) + ## administrative functions def version(): print """PyAmazon %(__version__)s @@ -112,6 +139,29 @@ if rc: return rc raise NoLicenseKey, 'get a license key at http://www.amazon.com/webservices' +def setLocale(locale): + """set locale""" + global LOCALE + LOCALE = locale + +def getLocale(locale = None): + return locale or LOCALE + +def setAssociateId(associate_id): + """set associate id""" + global ASSOCIATE_ID + ASSOCIATE_ID = associate_id + +def getAssociateId(associate_id): + """get associate id + + associate id can come from any number of locations; + see module docs for search order""" + for get, location in _associateLocations: + rc = get(associate_id) + if rc: return rc + return "webservices-20" + def setProxy(http_proxy): """set HTTP proxy""" global HTTP_PROXY @@ -170,17 +220,24 @@ else: rc = "".join([e.data for e in element.childNodes if isinstance(e, minidom.Text)]) if element.tagName == 'SalesRank': - rc = int(rc.replace(',', '')) + rc = int(rc.replace(',', '').replace('.','')) return rc -def buildURL(search_type, keyword, product_line, type, page, license_key): - url = "http://xml.amazon.com/onca/xml?v=1.0&f=xml&t=webservices-20" +def buildURL(search_type, keyword, product_line, type, page, license_key, locale, associate_id): + if locale in ['de', 'uk']: + host = "xml-eu" + else: + host = "xml" + url = "http://%s.amazon.com/onca/xml?v=1.0&f=xml" % host + url += "&t=%s" % associate_id.strip() url += "&dev-t=%s" % license_key.strip() url += "&type=%s" % type if page: url += "&page=%s" % page if product_line: url += "&mode=%s" % product_line + if locale: + url += "&locale=%s" % locale url += "&%s=%s" % (search_type, urllib.quote(keyword)) return url @@ -189,7 +246,7 @@ def search(search_type, keyword, product_line, type="heavy", page=None, - license_key = None, http_proxy = None): + license_key = None, http_proxy = None, locale=None, associate_id=None): """search Amazon You need a license key to call this function; see @@ -208,6 +265,8 @@ DirectorSearch - in (dvd, vhs, video) ManufacturerSearch - in (electronics, kitchen, videogames, software, photo, pc-hardware) http_proxy (optional) - address of HTTP proxy to use for sending and receiving SOAP messages + locale (optional) - national amazon to access, currently 'uk', 'de', 'jp' are supported + associate_id (optional) - your amazon associate id Returns: list of Bags, each Bag may contain the following attributes: Asin - Amazon ID ("ASIN" number) of this item @@ -235,7 +294,9 @@ URL - URL of this item """ license_key = getLicense(license_key) - url = buildURL(search_type, keyword, product_line, type, page, license_key) + locale = getLocale(locale) + associate_id = getAssociateId(associate_id) + url = buildURL(search_type, keyword, product_line, type, page, license_key, locale, associate_id) proxies = getProxies(http_proxy) u = urllib.FancyURLopener(proxies) usock = u.open(url) @@ -251,51 +312,51 @@ else: return data.Details -def searchByKeyword(keyword, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None): - return search("KeywordSearch", keyword, product_line, type, page, license_key, http_proxy) +def searchByKeyword(keyword, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("KeywordSearch", keyword, product_line, type, page, license_key, http_proxy, locale, associate_id) -def browseBestSellers(browse_node, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None): - return search("BrowseNodeSearch", browse_node, product_line, type, page, license_key, http_proxy) +def browseBestSellers(browse_node, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("BrowseNodeSearch", browse_node, product_line, type, page, license_key, http_proxy, locale, associate_id) -def searchByASIN(ASIN, type="heavy", license_key=None, http_proxy=None): - return search("AsinSearch", ASIN, None, type, None, license_key, http_proxy) +def searchByASIN(ASIN, type="heavy", license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("AsinSearch", ASIN, None, type, None, license_key, http_proxy, locale, associate_id) -def searchByUPC(UPC, type="heavy", license_key=None, http_proxy=None): - return search("UpcSearch", UPC, None, type, None, license_key, http_proxy) +def searchByUPC(UPC, type="heavy", license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("UpcSearch", UPC, None, type, None, license_key, http_proxy, locale, associate_id) -def searchByAuthor(author, type="heavy", page=1, license_key=None, http_proxy=None): - return search("AuthorSearch", author, "books", type, page, license_key, http_proxy) +def searchByAuthor(author, type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("AuthorSearch", author, "books", type, page, license_key, http_proxy, locale, associate_id) -def searchByArtist(artist, product_line="music", type="heavy", page=1, license_key=None, http_proxy=None): +def searchByArtist(artist, product_line="music", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): if product_line not in ("music", "classical"): raise AmazonError, "product_line must be in ('music', 'classical')" - return search("ArtistSearch", artist, product_line, type, page, license_key, http_proxy) + return search("ArtistSearch", artist, product_line, type, page, license_key, http_proxy, locale, associate_id) -def searchByActor(actor, product_line="dvd", type="heavy", page=1, license_key=None, http_proxy=None): +def searchByActor(actor, product_line="dvd", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): if product_line not in ("dvd", "vhs", "video"): raise AmazonError, "product_line must be in ('dvd', 'vhs', 'video')" - return search("ActorSearch", actor, product_line, type, page, license_key, http_proxy) + return search("ActorSearch", actor, product_line, type, page, license_key, http_proxy, locale, associate_id) -def searchByDirector(director, product_line="dvd", type="heavy", page=1, license_key=None, http_proxy=None): +def searchByDirector(director, product_line="dvd", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): if product_line not in ("dvd", "vhs", "video"): raise AmazonError, "product_line must be in ('dvd', 'vhs', 'video')" - return search("DirectorSearch", director, product_line, type, page, license_key, http_proxy) + return search("DirectorSearch", director, product_line, type, page, license_key, http_proxy, locale, associate_id) -def searchByManufacturer(manufacturer, product_line="pc-hardware", type="heavy", page=1, license_key=None, http_proxy=None): +def searchByManufacturer(manufacturer, product_line="pc-hardware", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): if product_line not in ("electronics", "kitchen", "videogames", "software", "photo", "pc-hardware"): raise AmazonError, "product_line must be in ('electronics', 'kitchen', 'videogames', 'software', 'photo', 'pc-hardware')" - return search("ManufacturerSearch", manufacturer, product_line, type, page, license_key, http_proxy) + return search("ManufacturerSearch", manufacturer, product_line, type, page, license_key, http_proxy, locale, associate_id) -def searchByListMania(listManiaID, type="heavy", page=1, license_key=None, http_proxy=None): - return search("ListManiaSearch", listManiaID, None, type, page, license_key, http_proxy) +def searchByListMania(listManiaID, type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("ListManiaSearch", listManiaID, None, type, page, license_key, http_proxy, locale, associate_id) -def searchSimilar(ASIN, type="heavy", page=1, license_key=None, http_proxy=None): - return search("SimilaritySearch", ASIN, None, type, page, license_key, http_proxy) +def searchSimilar(ASIN, type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("SimilaritySearch", ASIN, None, type, page, license_key, http_proxy, locale, associate_id) -def searchByWishlist(wishlistID, type="heavy", page=1, license_key=None, http_proxy=None): - return search("WishlistSearch", wishlistID, None, type, page, license_key, http_proxy) +def searchByWishlist(wishlistID, type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("WishlistSearch", wishlistID, None, type, page, license_key, http_proxy, locale, associate_id) -def searchByPower(keyword, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None): - return search("PowerSearch", keyword, product_line, type, page, license_key, http_proxy) +def searchByPower(keyword, product_line="books", type="heavy", page=1, license_key=None, http_proxy=None, locale=None, associate_id=None): + return search("PowerSearch", keyword, product_line, type, page, license_key, http_proxy, locale, associate_id) # >>> RecentKing = amazon.searchByPower('author:Stephen King and pubdate:2003') # >>> SnowCrash = amazon.searchByPower('title:Snow Crash')