1
# Copyright (C) 2006 Adam Olsen
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 1, or (at your option)
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
from xl.nls import gettext as _
27
logger = logging.getLogger(__name__)
29
class AmazonSearchError(Exception): pass
31
def generate_timestamp():
32
ret = datetime.datetime.utcnow()
33
return ret.isoformat() + 'Z'
35
def generate_signature(key, params):
37
param_string = urllib.urlencode(params)
38
params = param_string.split('&')
41
param_string = '&'.join(params)
43
hm = hmac.new(str(key),
44
"GET\nwebservices.amazon.com\n/onca/xml\n%s" %
45
param_string, hashlib.sha256)
47
h = urllib.quote(base64.b64encode(hm.digest()))
48
return (param_string, h)
50
def search_covers(search, api_key, secret_key):
51
ts = generate_timestamp()
54
'Operation': 'ItemSearch',
56
'Version': '2009-01-06',
58
'AWSAccessKeyId': api_key,
59
'SearchIndex': 'Music',
60
'Service': 'AWSECommerceService',
61
'ResponseGroup': 'ItemAttributes,Images',
64
(param_string, hmac) = generate_signature(secret_key, params)
65
data = urllib.urlopen(
66
'https://webservices.amazon.com/onca/xml?%s&Signature=%s'
67
% (param_string, hmac)).read()
69
# check for an error message
70
m = re.search(r'<Message>(.*)</Message>', data, re.DOTALL)
72
logger.warning('Amazon Covers Search Error: %s' % m.group(1))
73
raise AmazonSearchError(m.group(1))
75
# check for large images
76
regex = re.compile(r'<LargeImage><URL>([^<]*)', re.DOTALL)
77
items = regex.findall(data)