diff options
author | jerous <jerous@gmail.com> | 2008-06-16 02:41:45 +0200 |
---|---|---|
committer | jerous <jerous@gmail.com> | 2008-06-16 02:42:21 +0200 |
commit | 6a5e4dccb37a30e3dbc99983bbc6512447e5793d (patch) | |
tree | 82e57b988705ed4de3f3a661caad14f58274b4e5 /plugins/amazon_cover_fetcher.py | |
parent | e94520bf970684506c795dd46a436d340e5a16d6 (diff) |
move amazon_cover_fetcher to plugins folder
don't load amazon_cover_fetcher in __init__
Diffstat (limited to 'plugins/amazon_cover_fetcher.py')
-rw-r--r-- | plugins/amazon_cover_fetcher.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/plugins/amazon_cover_fetcher.py b/plugins/amazon_cover_fetcher.py new file mode 100644 index 0000000..2de0181 --- /dev/null +++ b/plugins/amazon_cover_fetcher.py @@ -0,0 +1,57 @@ +# Thank you, http://www.semicomplete.com/scripts/albumcover.py +import re +import urllib + +AMAZON_AWS_ID = "0525E2PQ81DD7ZTWTK82" + +class AmazonAlbumImage(object): + awsurl = "http://ecs.amazonaws.com/onca/xml" + def __init__(self, artist, album): + self.artist = artist + self.album = album + + def fetch(self): + url = self._GetResultURL(self._SearchAmazon()) + if not url: + return None + img_re = re.compile(r'''registerImage\("original_image", "([^"]+)"''') + prod_data = urllib.urlopen(url).read() + m = img_re.search(prod_data) + if not m: + return None + img_url = m.group(1) + return img_url + + def _SearchAmazon(self): + data = { + "Service": "AWSECommerceService", + "Version": "2005-03-23", + "Operation": "ItemSearch", + "ContentType": "text/xml", + "SubscriptionId": AMAZON_AWS_ID, + "SearchIndex": "Music", + "ResponseGroup": "Small", + } + + data["Artist"] = self.artist + data["Keywords"] = self.album + + fd = urllib.urlopen("%s?%s" % (self.awsurl, urllib.urlencode(data))) + + return fd.read() + + def _GetResultURL(self, xmldata): + url_re = re.compile(r"<DetailPageURL>([^<]+)</DetailPageURL>") + m = url_re.search(xmldata) + return m and m.group(1) + +if __name__ == "__main__": + import sys + if len(sys.argv) < 3: + print "usage: %s <artist> <album>" % argv[0] + sys.exit(1) + + artist = sys.argv[1] + album = sys.argv[2] + + print AmazonAlbumImage(artist, album).fetch() |