~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to plugins/amazoncovers/_ecs.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Adam Olsen
2
 
#
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)
6
 
# any later version.
7
 
#
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.
12
 
#
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.
16
 
 
17
 
import urllib
18
 
import sys
19
 
import hmac
20
 
import hashlib
21
 
import base64
22
 
import datetime
23
 
import re
24
 
from xl.nls import gettext as _
25
 
import logging
26
 
 
27
 
logger = logging.getLogger(__name__)
28
 
 
29
 
class AmazonSearchError(Exception): pass
30
 
 
31
 
def generate_timestamp():
32
 
    ret = datetime.datetime.utcnow()
33
 
    return ret.isoformat() + 'Z'
34
 
 
35
 
def generate_signature(key, params):
36
 
 
37
 
    param_string = urllib.urlencode(params)
38
 
    params = param_string.split('&')
39
 
 
40
 
    params.sort()
41
 
    param_string = '&'.join(params)
42
 
 
43
 
    hm = hmac.new(str(key), 
44
 
        "GET\nwebservices.amazon.com\n/onca/xml\n%s" %
45
 
        param_string, hashlib.sha256)
46
 
 
47
 
    h = urllib.quote(base64.b64encode(hm.digest())) 
48
 
    return (param_string, h)
49
 
 
50
 
def search_covers(search, api_key, secret_key):
51
 
    ts = generate_timestamp()
52
 
 
53
 
    params = {
54
 
        'Operation': 'ItemSearch',
55
 
        'Keywords': search,
56
 
        'Version': '2009-01-06',
57
 
        'Timestamp': ts,
58
 
        'AWSAccessKeyId': api_key,
59
 
        'SearchIndex': 'Music',
60
 
        'Service': 'AWSECommerceService',
61
 
        'ResponseGroup': 'ItemAttributes,Images',
62
 
        }
63
 
 
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()
68
 
 
69
 
    # check for an error message
70
 
    m = re.search(r'<Message>(.*)</Message>', data, re.DOTALL)
71
 
    if m:
72
 
        logger.warning('Amazon Covers Search Error: %s' % m.group(1))
73
 
        raise AmazonSearchError(m.group(1)) 
74
 
 
75
 
    # check for large images
76
 
    regex = re.compile(r'<LargeImage><URL>([^<]*)', re.DOTALL)
77
 
    items = regex.findall(data)
78
 
 
79
 
    return items