From c20ce11a8c1cb660bf5eec28797214a529293fcd Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 21 Apr 2011 00:01:34 -0700 Subject: [PATCH] update album art embed plugin for new API --HG-- rename : beetsplug/embedcoverart.py => beetsplug/embedart.py --- beets/autotag/art.py | 4 ++++ beetsplug/embedart.py | 34 ++++++++++++++++++++++++++++ beetsplug/embedcoverart.py | 45 -------------------------------------- 3 files changed, 38 insertions(+), 45 deletions(-) create mode 100755 beetsplug/embedart.py delete mode 100755 beetsplug/embedcoverart.py diff --git a/beets/autotag/art.py b/beets/autotag/art.py index f9e92c81c..c2f96300d 100644 --- a/beets/autotag/art.py +++ b/beets/autotag/art.py @@ -35,6 +35,7 @@ def art_for_asin(asin): # Fetch the image. url = AMAZON_URL % (asin, index) try: + log.debug('Downloading art: %s' % url) fn, headers = urllib.urlretrieve(url) except IOError: log.debug('error fetching art at URL %s' % url) @@ -42,6 +43,7 @@ def art_for_asin(asin): # Make sure it's actually an image. if headers.gettype() == AMAZON_CONTENT_TYPE: + log.debug('Downloaded art to: %s' % fn) return fn @@ -52,8 +54,10 @@ def art_for_album(album): to downloaded art for the album (or None if no art is found). """ if album['asin']: + log.debug('Fetching album art for ASIN %s.' % album['asin']) return art_for_asin(album['asin']) else: + log.debug('No ASIN available: no art found.') return None diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py new file mode 100755 index 000000000..4f256fec7 --- /dev/null +++ b/beetsplug/embedart.py @@ -0,0 +1,34 @@ +import logging +from email.mime.image import MIMEImage +from beets.plugins import BeetsPlugin +from beets import mediafile + +log = logging.getLogger('beets') + +class EmbedCoverArtPlugin(BeetsPlugin): + """Allows albumart to be embedded into the actual files.""" + def configure(self, config): + pass + +@EmbedCoverArtPlugin.listen('album_imported') +def album_imported(lib, album): + if album.artpath: + data = open(album.artpath, 'rb').read() + img = MIMEImage(data) + mime_img = img.get_content_subtype() + + if mime_img == 'jpeg': + kind = mediafile.imagekind.JPEG + elif mime_img == 'png': + kind = mediafile.imagekind.PNG + else: + log.error('A file of type %s is not allowed as coverart.' + % mime_img) + return + + # Add art to each file. + log.debug('Embedding album art.') + for item in album.items(): + f = mediafile.MediaFile(item.path) + f.art = (data, kind) + f.save() diff --git a/beetsplug/embedcoverart.py b/beetsplug/embedcoverart.py deleted file mode 100755 index 400f19a50..000000000 --- a/beetsplug/embedcoverart.py +++ /dev/null @@ -1,45 +0,0 @@ -from beets.plugins import BeetsPlugin -from beets import mediafile - -import logging - -from email.mime.image import MIMEImage - -log = logging.getLogger('beets') - - -class EmbedCoverArtPlugin(BeetsPlugin): - '''Allows albumart to be embedded into the actual files.''' - - def __init__(self): - self.register_listener('loaded', self.loaded) - self.register_listener('album_imported', self.album_imported) - - def configure(self, config): - pass - - def loaded(self): - pass - - def album_imported(self, lib, album): - albumart = album.artpath - ALLOWED_MIMES = ('jpeg','png') - - if albumart: - albumart_raw = open(albumart, 'rb').read() - img = MIMEImage(albumart_raw) - mime_img = img.get_content_subtype() - - if mime_img in ALLOWED_MIMES: - mime_type = 'image/%s' % mime_img - - for item in album.items(): - f = mediafile.MediaFile(item) - - if "mp3" in item.type: - f.albumart_mime = mime_type - - f.albumart = albumart_raw - f.save() - else: - log.error('Sorry, a file of type %s is not allowed as coverart.' % mime_img)