Login

Amazon product-data interface class for Django-friendly PyAWS queries

Author:
fish2000
Posted:
January 25, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

I am not sure what to say about the state of PyAWS, or its future, what with the multiple forks available and lack of recent updates. The best version I've found is http://github.com/IanLewis/pyaws, a spiffed-up version of 0.2.2 by Ian Lewis. I wrote this class on top of PyAWS so I could have more pythonic/django-y calling conventions, and to isolate the calls in case I have to swap libraries or versions down the road.

You may want to familiarize yourself with PyAWS before using this. You'll definately need Amazon web service login credentials and keys -- they're available here for free.

personally I use it with these monkeypatching and decorator-decorators -- at the top of my personal version of the file containing this snippet I use the two (non-silly) examples from that snippet, to make the PyAWS internal Bag collection class work for me.

EXAMPLE USE:

# search Amazon's product database (returns a list of nested dicts)
from amazon import aws
books = aws.search(q='raymond carver')
lenses = aws.search(q='leica summicron', idx='Photo')

# get the data  for a specific ASIN/ISBN/EAN/etc ID number
what_we_talk_about_when_we_talk_about_love = aws.fetch(qid='0679723056', idtype='ASIN')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
from decorator import decorator
from monkeypatch import monkeypatch
from pyaws import ecs

# ins and outs
# these can be modified fairly extensively,
# to control exactly what the Amazon API pukes out
rs_fetch = """Request,ItemIds,Images,Tracks,Accessories,Small,Medium,Large,Variations,VariationImages,VariationMinimum,VariationSummary,TagsSummary,Tags,VariationMatrix,VariationOffers,ItemAttributes,SalesRank,Subjects,Reviews,EditorialReview,Collections,ShippingCharges,BrowseNodes"""  ## holy shit
rs_search = """Request, ItemIds, Small, Medium, Large, ItemAttributes, Tracks, EditorialReview, SalesRank, Images"""

class Amazon:
	
	auth_init = False
	
	def __init__(self, ecsref=None):
		self.ecs = ecsref
	
	@decorator
	def ecsauth(f, *args, **kwargs):
		self = args[0]
		if not self.auth_init:
			self.ecs.setLicenseKey("YOUR_AWS_KEY")
			self.ecs.setSecretAccessKey("YOUR_SUPER_SECRET_AWS_PRIVATE_KEY")
			self.auth_init = True
		return f(self, *args, **kwargs)
	
	@ecsauth
	def search(*args, **kwargs):
		self = args[0] # ugh
		search = kwargs.get('q', None)
		searchidx = kwargs.get('idx', 'Books')
		rsgroup = kwargs.get('rs', rs_search)
		aq = self.ecs.ItemSearch(search, SearchIndex=searchidx, ResponseGroup=rsgroup)
		aqout = []
		i = e = 0
		while i < 25:
			try:
				a = aq.next()
				i += 1
			except IndexError:
				e += 1
			except StopIteration:
				break
			else:
				aqout += [a]
		return aqout

	@ecsauth
	def fetch(self, *args, **kwargs):
		qid = '%s' % kwargs.get('qid', "-1")
		idtype = kwargs.get('idtype', 'ASIN')
		rsgroup = kwargs.get('rg', rs_fetch)
		idx = kwargs.get('idx', 'Books')
		aqout = []

		if idtype == "ASIN":
			idx = None
		qid = qid.replace('-', '')

		aq = self.ecs.ItemLookup("%s" % qid, IdType=idtype, SearchIndex=idx, ResponseGroup=rsgroup)

		i = e = 0
		while True:
			try:
				a = aq.next()
				i += 1
			except IndexError:
				e += 1
			except StopIteration:
				break
			else:
				aqout += [a]
		return aqout

aws = Amazon(ecs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

fish2000 (on January 25, 2010):

also: the IndexError except blocks are there because of underlying PyAWS glitches.

#

Please login first before commenting.