summaryrefslogtreecommitdiff
path: root/amazon_cover_fetcher.py
blob: 2de0181bb2ebcbc555b6387003d7e7ff8919bc64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()