-
Notifications
You must be signed in to change notification settings - Fork 2
/
amazon.py
59 lines (58 loc) · 2.19 KB
/
amazon.py
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
import urllib
import time
import hmac
import base64
import datetime
import urllib
import hashlib
import logging
from xml.dom import minidom
from google.appengine.api import urlfetch
def productSearch(keywords):
# Amazon is kind of obnoxious with the API and there's very
# little available online to help.
# their page is like a maze.
# hopefully this won't have to change any time soon - Seth
# I agree - Harrison.
pairs = [
"Service=AWSECommerceService",
"AWSAccessKeyId=AKIAJIXECWA77X5XX4DQ",
"AssociateTag=w0c3d-20",
"Operation=ItemSearch",
"Keywords=" + keywords,
"ResponseGroup=Images%2CTracks%2CItemAttributes",
"SearchIndex=Music",
"Version=2011-08-01",
"Timestamp=" + urllib.quote_plus(datetime.datetime.now()
.strftime("%Y-%m-%dT%H:%M:%SZ")),
]
# It's been broken for the past few months because I didn't realize
# that you have to sort the pairs first. Don't make the same mistake
# - Harrison
pairs.sort()
string = "&".join(pairs)
hashstring = "GET\nwebservices.amazon.com\n/onca/xml\n" + string
dig = hmac.new("6oYjAsiXTz8xZzpKZC8zkqXnkYV72CNuCRh9hUsQ",
msg=hashstring,
digestmod=hashlib.sha256).digest()
coded = dig.encode("base64").strip()
finalurl = ("http://webservices.amazon.com/onca/xml?" + string +
"&Signature=" + urllib.quote_plus(coded))
xmldata = urlfetch.fetch(unicode(finalurl)).content
xmldoc = minidom.parseString(xmldata)
items = xmldoc.getElementsByTagName("Item")
# makes sure we only look at items with images,
# otherwise bad things can happen
items = filter(lambda i: len(i.getElementsByTagName("SmallImage")) > 0, items)
# same with medium image
items = filter(lambda i: len(i.getElementsByTagName("MediumImage")) > 0,
items)
# same with large image
items = filter(lambda i: len(i.getElementsByTagName("LargeImage")) > 0, items)
# and track
items = filter(lambda i: len(i.getElementsByTagName("Track")) > 0, items)
# and artist
items = filter(lambda i: len(i.getElementsByTagName("Artist")) > 0, items)
# and title
items = filter(lambda i: len(i.getElementsByTagName("Title")) > 0, items)
return items