mirror of
https://github.com/beetbox/beets.git
synced 2026-01-14 20:24:36 +01:00
add extension for proxied images
`urllib.urlretrieve` was using the correct extension in most cases -- I think when the URL ended with .jpg -- but not in every case. This was leading to files named just "cover" and not "cover.jpg" or something else sensible. In particular, proxied URLs don't have .jpg extensions. This generates the filename manually so the source image always has an extension.
This commit is contained in:
parent
45650a4b64
commit
d9974081a7
2 changed files with 26 additions and 11 deletions
|
|
@ -18,15 +18,18 @@ import urllib
|
|||
import re
|
||||
import logging
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.util.artresizer import ArtResizer
|
||||
from beets import importer
|
||||
from beets import ui
|
||||
from beets import util
|
||||
|
||||
IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg']
|
||||
COVER_NAMES = ['cover', 'front', 'art', 'album', 'folder']
|
||||
CONTENT_TYPES = ('image/jpeg',)
|
||||
DOWNLOAD_EXTENSION = '.jpg'
|
||||
|
||||
log = logging.getLogger('beets')
|
||||
|
||||
|
|
@ -36,19 +39,25 @@ def _fetch_image(url):
|
|||
actually be an image. If so, returns a path to the downloaded image.
|
||||
Otherwise, returns None.
|
||||
"""
|
||||
log.debug('Downloading art: %s' % url)
|
||||
# Generate a temporary filename with the correct extension.
|
||||
fd, fn = tempfile.mkstemp(suffix=DOWNLOAD_EXTENSION)
|
||||
os.close(fd)
|
||||
|
||||
log.debug(u'fetchart: downloading art: {0}'.format(url))
|
||||
try:
|
||||
fn, headers = urllib.urlretrieve(url)
|
||||
_, headers = urllib.urlretrieve(url, filename=fn)
|
||||
except IOError:
|
||||
log.debug('error fetching art')
|
||||
return
|
||||
|
||||
# Make sure it's actually an image.
|
||||
if headers.gettype() in CONTENT_TYPES:
|
||||
log.debug('Downloaded art to: %s' % fn)
|
||||
log.debug(u'fetchart: downloaded art to: {0}'.format(
|
||||
util.displayable_path(fn)
|
||||
))
|
||||
return fn
|
||||
else:
|
||||
log.debug('Not an image.')
|
||||
log.debug(u'fetchart: not an image')
|
||||
|
||||
|
||||
# ART SOURCES ################################################################
|
||||
|
|
@ -84,10 +93,10 @@ def aao_art(asin):
|
|||
# Get the page from albumart.org.
|
||||
url = '%s?%s' % (AAO_URL, urllib.urlencode({'asin': asin}))
|
||||
try:
|
||||
log.debug('Scraping art URL: %s' % url)
|
||||
log.debug(u'fetchart: scraping art URL: {0}'.format(url))
|
||||
page = urllib.urlopen(url).read()
|
||||
except IOError:
|
||||
log.debug('Error scraping art page')
|
||||
log.debug(u'fetchart: error scraping art page')
|
||||
return
|
||||
|
||||
# Search the page for the image URL.
|
||||
|
|
@ -96,7 +105,7 @@ def aao_art(asin):
|
|||
image_url = m.group(1)
|
||||
return image_url
|
||||
else:
|
||||
log.debug('No image found on page')
|
||||
log.debug('fetchart: no image found on page')
|
||||
|
||||
|
||||
# Art from the filesystem.
|
||||
|
|
@ -117,12 +126,16 @@ def art_in_path(path):
|
|||
for fn in images:
|
||||
for name in COVER_NAMES:
|
||||
if fn.lower().startswith(name):
|
||||
log.debug('Using well-named art file %s' % fn)
|
||||
log.debug('fetchart: using well-named art file {0}'.format(
|
||||
util.displayable_path(fn)
|
||||
))
|
||||
return os.path.join(path, fn)
|
||||
|
||||
# Fall back to any image in the folder.
|
||||
if images:
|
||||
log.debug('Using fallback art file %s' % images[0])
|
||||
log.debug('fetchart: using fallback art file {0}'.format(
|
||||
util.displayable_path(images[0])
|
||||
))
|
||||
return os.path.join(path, images[0])
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -45,8 +45,10 @@ preserved.
|
|||
|
||||
Beets can resize images using `PIL`_, `ImageMagick`_, or a server-side resizing
|
||||
proxy. If either PIL or ImageMagick is installed, beets will use those;
|
||||
otherwise, it falls back to the resizing proxy. Since server-side resizing can
|
||||
be slow, consider installing one of the two backends for better performance.
|
||||
otherwise, it falls back to the resizing proxy. If the resizing proxy is used,
|
||||
no resizing is performed for album art found on the filesystem---only downloaded
|
||||
art is resized. Server-side resizing can also be slower than local resizing, so
|
||||
consider installing one of the two backends for better performance.
|
||||
|
||||
.. _PIL: http://www.pythonware.com/products/pil/
|
||||
.. _ImageMagick: http://www.imagemagick.org/
|
||||
|
|
|
|||
Loading…
Reference in a new issue