forked from public/pyceo
Fixed Library: Added signing to AWS requests.
This commit is contained in:
parent
0413dcaaa4
commit
c931a6bedb
|
@ -3,6 +3,10 @@
|
||||||
from xml.dom import minidom, Node
|
from xml.dom import minidom, Node
|
||||||
import urllib
|
import urllib
|
||||||
import time
|
import time
|
||||||
|
import datetime
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
import hmac
|
||||||
|
|
||||||
class PyMazonError(Exception):
|
class PyMazonError(Exception):
|
||||||
"""Holds information about an error that occured during a pymazon request"""
|
"""Holds information about an error that occured during a pymazon request"""
|
||||||
|
@ -70,20 +74,37 @@ class PyMazonBook:
|
||||||
|
|
||||||
class PyMazon:
|
class PyMazon:
|
||||||
"""A method of looking up book information on Amazon."""
|
"""A method of looking up book information on Amazon."""
|
||||||
def __init__(self, accesskey):
|
def __init__(self, accesskey, secretkey):
|
||||||
self.__key = accesskey
|
self.__key = accesskey
|
||||||
|
self.__secret = secretkey
|
||||||
self.__last_query_time = 0
|
self.__last_query_time = 0
|
||||||
|
|
||||||
def __form_request(self, isbn):
|
def __form_request(self, isbn):
|
||||||
return 'http://webservices.amazon.com/onca/xml?' + \
|
content = {}
|
||||||
'Service=AWSECommerceService' + \
|
dstamp = datetime.datetime.utcfromtimestamp(time.time())
|
||||||
'&Version=2008-08-19' + \
|
content['Timestamp'] = dstamp.strftime('%Y-%m-%dT%H:%M:%S.000Z')
|
||||||
'&AWSAccessKeyId=' + self.__key + \
|
content['Service'] = 'AWSECommerceService'
|
||||||
'&Operation=ItemLookup' + \
|
content['Version'] = '2008-08-19'
|
||||||
'&ResponseGroup=ItemAttributes' + \
|
content['Operation'] = 'ItemLookup'
|
||||||
'&IdType=ISBN' + \
|
content['ResponseGroup'] = 'ItemAttributes'
|
||||||
'&SearchIndex=Books' + \
|
content['IdType'] = 'ISBN'
|
||||||
'&ItemId=' + isbn
|
content['SearchIndex'] = 'Books'
|
||||||
|
content['ItemId'] = isbn
|
||||||
|
content['AWSAccessKeyId'] = self.__key
|
||||||
|
|
||||||
|
URI_String = []
|
||||||
|
|
||||||
|
for key, value in sorted(content.items()):
|
||||||
|
URI_String.append('%s=%s' % (key, urllib.quote(value)))
|
||||||
|
|
||||||
|
req = '&'.join(URI_String)
|
||||||
|
to_sign_req = 'GET\necs.amazonaws.com\n/onca/xml\n' + req
|
||||||
|
|
||||||
|
h = hmac.new(self.__secret, to_sign_req, hashlib.sha256)
|
||||||
|
sig = base64.b64encode(h.digest())
|
||||||
|
req += '&Signature=%s' % urllib.quote(sig)
|
||||||
|
|
||||||
|
return 'http://ecs.amazonaws.com/onca/xml?' + req
|
||||||
|
|
||||||
def __elements_text(self, element, name):
|
def __elements_text(self, element, name):
|
||||||
result = []
|
result = []
|
||||||
|
|
|
@ -21,7 +21,7 @@ def configure():
|
||||||
"""
|
"""
|
||||||
Load configuration
|
Load configuration
|
||||||
"""
|
"""
|
||||||
cfg_fields = [ "aws_account_key" ]
|
cfg_fields = [ "aws_account_key", "aws_secret_key" ]
|
||||||
temp_cfg = conf.read(CONFIG_FILE)
|
temp_cfg = conf.read(CONFIG_FILE)
|
||||||
conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg)
|
conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg)
|
||||||
cfg.update(temp_cfg)
|
cfg.update(temp_cfg)
|
||||||
|
@ -127,7 +127,7 @@ class BookAddPage(WizardPanel):
|
||||||
isbn = self.isbn.get_edit_text()
|
isbn = self.isbn.get_edit_text()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pymazon = PyMazon(cfg["aws_account_key"])
|
pymazon = PyMazon(cfg["aws_account_key"], cfg["aws_secret_key"])
|
||||||
book = pymazon.lookup(isbn)
|
book = pymazon.lookup(isbn)
|
||||||
|
|
||||||
currents = lib.Book.select(lib.Book.q.isbn==isbn)
|
currents = lib.Book.select(lib.Book.q.isbn==isbn)
|
||||||
|
|
|
@ -2,3 +2,4 @@
|
||||||
|
|
||||||
library_connect_string = "postgres://librarian:PWPWPWPWPWPWPWPWPWPW@127.0.0.1/library"
|
library_connect_string = "postgres://librarian:PWPWPWPWPWPWPWPWPWPW@127.0.0.1/library"
|
||||||
aws_account_key = "KEYKEYKEYKEYKEYKEYKY"
|
aws_account_key = "KEYKEYKEYKEYKEYKEYKY"
|
||||||
|
aws_secret_key = "KEYKEYKEYKEYKEYKEYKY"
|
||||||
|
|
Loading…
Reference in New Issue