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

 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. safe(r) monkeypatching scheme for django testing by showell 4 years, 5 months ago
  2. Effective content caching for mass-load site using redirect feature by nnseva 2 years, 9 months ago
  3. Decorator to execute a method only once by atodorov-otb 4 months, 2 weeks ago
  4. Filter on Multiple M2M Objects Simultaneously by axiak 7 years ago
  5. A basic view for working with jQuery dataTables plugin by yuvi 3 months, 1 week ago

Comments

fish2000 (on January 25, 2010):

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

#

(Forgotten your password?)