def amazon_fetch(self):
        """ 
        attempts to fetch album cover art from Amazon Web Services and 
        calls save_to_disk() to save the largest available image permanently
        to avoid subsequent lookups.  first tries to fetch the artist + album
        but falls back to artist search only if the album art isn't found
        """

        if g.tc.awskey == '':
            raise NoArtError

      
        artist_safe = urllib2.quote(self.artist)
        album_safe = urllib2.quote(self.album)

        urls = []

        date = datetime.datetime.utcnow()
        date = date.replace(microsecond=0)
        timestamp = date.isoformat()
        query_string = {'Service': 'AWSECommerceService',
                        'AWSAccessKeyId': g.tc.awskey,
                        'Operation': 'ItemSearch',
                        'SearchIndex': 'Music',
                        'Version': '2009-10-01',
                        'ResponseGroup': 'Images',
                        'Artist': artist_safe,
                        'Title': album_safe,
                        'Timestamp': timestamp + 'Z'}

        query_string_sorted = '&'.join(['='.join((k,query_string[k])) for k in sorted(query_string.iterkeys())])

        urls.append({'verb': 'GET',
                     'protocol': 'http://',
                     'host': 'ecs.amazonaws.com',
                     'request_uri': '/onca/xml',
                     'query_string': query_string_sorted.replace(':','%3A')})

        del query_string['Title']
        query_string_sorted = '&'.join(['='.join((k,query_string[k])) for k in sorted(query_string.iterkeys())])

        urls.append({'verb': 'GET',
                     'protocol': 'http://',
                     'host': 'ecs.amazonaws.com',
                     'request_uri': '/onca/xml',
                     'query_string': query_string_sorted.replace(':','%3A')})

        for url in urls:
            encode_string = '\n'.join((url['verb'],url['host'],url['request_uri'],url['query_string']))
            h = hmac.new(str(g.tc.aws_secret), str(encode_string), hashlib.sha256)
            hmac_string = h.digest()
            signature = base64.b64encode(hmac_string).replace('+','%2B').replace('=','%3D')
            real_url = url['protocol'] + url['host'] + url['request_uri'] + '?' + url['query_string'] + '&Signature=%s' % signature
            try:
                self.log('Fetching Amazon album image: %s' % real_url)
                urlfile = urllib2.urlopen(real_url)
            except urllib2.URLError:
                # there are probably other exceptions that need to be caught here.. 
                self.log('Error fetching Amazon XML')
                raise NoArtError

            doc = xml.dom.minidom.parse(urlfile)
            urlfile.close()
            imgnodes = doc.getElementsByTagName('LargeImage')
            if len(imgnodes) > 0:
                node = imgnodes[0]
                self.amazonurl =  node.firstChild.firstChild.nodeValue
                self.log('Found album art: %s' % self.amazonurl)
                break

        if not self.amazonurl:
            raise NoArtError

        self.save_to_disk()
dataset/ETHPy150Open ralfonso/theory/theory/model/albumart.py/AlbumArt.amazon_fetch/original