From 55035c9a727d4f24a76fbbce7f0bf3b7a662eade Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 12 Jul 2010 00:05:53 -0700 Subject: [PATCH] nascent album art fetcher (based on MB-reported Amazon IDs) --- beets/autotag/art.py | 41 +++++++++++++++++++++++++++++++++++++++++ beets/autotag/mb.py | 1 + 2 files changed, 42 insertions(+) create mode 100644 beets/autotag/art.py diff --git a/beets/autotag/art.py b/beets/autotag/art.py new file mode 100644 index 000000000..d1df76253 --- /dev/null +++ b/beets/autotag/art.py @@ -0,0 +1,41 @@ +import urllib +import sys +import logging + +from beets.autotag.mb import album_for_id + +log = logging.getLogger('beets') + +AMAZON_URL = 'http://images.amazon.com/images/P/%s.%02i.LZZZZZZZ.jpg' +AMAZON_INDICES = (1,2) +AMAZON_CONTENT_TYPE = 'image/jpeg' + +def art_for_album(album): + """Given an album info dictionary from MusicBrainz, returns a path + to art for the album (or None if no art is found). + """ + if album['asin']: + for index in AMAZON_INDICES: + # Fetch the image. + url = AMAZON_URL % (album['asin'], index) + try: + fn, headers = urllib.urlretrieve(url) + except IOError: + log.debug('error fetching art at URL %s' % url) + continue + + # Make sure it's actually an image. + if headers['Content-Type'] == AMAZON_CONTENT_TYPE: + return fn + else: + return None + +# Smoke test. +if __name__ == '__main__': + aid = sys.argv[1] + fn = art_for_album(album_for_id(aid)) + if fn: + print fn + print len(open(fn).read())/1024 + else: + print 'no art found' diff --git a/beets/autotag/mb.py b/beets/autotag/mb.py index 84a062ebe..32ba6fbca 100644 --- a/beets/autotag/mb.py +++ b/beets/autotag/mb.py @@ -108,6 +108,7 @@ def release_dict(release, tracks=None): 'album_id': release.id, 'artist': release.artist.name, 'artist_id': release.artist.id, + 'asin': release.asin, } # Release date.