mirror of
https://github.com/beetbox/beets.git
synced 2025-12-06 16:42:42 +01:00
update album art embed plugin for new API
--HG-- rename : beetsplug/embedcoverart.py => beetsplug/embedart.py
This commit is contained in:
parent
4a6b8274d8
commit
c20ce11a8c
3 changed files with 38 additions and 45 deletions
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
|||
34
beetsplug/embedart.py
Executable file
34
beetsplug/embedart.py
Executable file
|
|
@ -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()
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in a new issue