autotagger -r switch now fetches and places album art

This commit is contained in:
Adrian Sampson 2010-07-13 22:34:52 -07:00
parent 5bb064a860
commit a49ebc853a
3 changed files with 26 additions and 8 deletions

View file

@ -821,11 +821,12 @@ class Library(BaseLibrary):
for the item's album. `image` is the path of the image that
will be moved there (used for its extension).
"""
item_dir = os.path.dirname(self.destination(item))
item_dir = os.path.dirname(item.path)
_, ext = os.path.splitext(image)
dest = os.path.join(item_dir, self.art_filename + ext)
return dest
# Main interface.
def add(self, item, copy=False):

View file

@ -18,12 +18,15 @@ interface.
import os
import logging
import shutil
from beets import ui
from beets.ui import print_
from beets import autotag
from beets import library
from beets.mediafile import UnreadableFileError, FileTypeError
import beets.autotag.art
autotag.art = beets.autotag.art
# Global logger.
log = logging.getLogger('beets')
@ -147,12 +150,12 @@ def tag_log(logfile, status, items):
path = os.path.commonprefix([item.path for item in items])
print >>logfile, status, os.path.dirname(path)
def tag_album(items, lib, copy=True, write=True, logfile=None):
def tag_album(items, lib, copy=True, write=True, logfile=None, art=False):
"""Import items into lib, tagging them as an album. If copy, then
items are copied into the destination directory. If write, then
new metadata is written back to the files' tags. If logfile is
provided, then a log message will be added there if the album is
untaggable.
untaggable. If art, then try to download album art for the album.
"""
# Try to get candidate metadata.
search_artist, search_album = None, None
@ -228,14 +231,25 @@ def tag_album(items, lib, copy=True, write=True, logfile=None):
# locking while we do the copying and tag updates.
for item in items:
lib.add(item)
# Get album art.
if art:
artpath = autotag.art.art_for_album(info)
if artpath:
artdest = lib.art_path(items[0], artpath)
#fixme -- move if possible?
shutil.copy(artpath, artdest)
lib.albuminfo(items[0]).artpath = artdest
def import_files(lib, paths, copy=True, write=True, autot=True, logpath=None):
def import_files(lib, paths, copy=True, write=True, autot=True,
logpath=None, art=False):
"""Import the files in the given list of paths, tagging each leaf
directory as an album. If copy, then the files are copied into
the library folder. If write, then new metadata is written to the
files themselves. If not autot, then just import the files
without attempting to tag. If logpath is provided, then untaggable
albums will be logged there.
albums will be logged there. If art, then try to download album art
for each album.
"""
if logpath:
logfile = open(logpath, 'w')
@ -257,7 +271,7 @@ def import_files(lib, paths, copy=True, write=True, autot=True, logpath=None):
first = False
# Infer tags.
tag_album(album, lib, copy, write, logfile)
tag_album(album, lib, copy, write, logfile, art)
# Write the database after each album.
lib.save()
@ -316,6 +330,8 @@ import_cmd.parser.add_option('-A', '--noautotag', action='store_false',
help="don't infer tags for imported files (opposite of -a)")
import_cmd.parser.add_option('-l', '--log', dest='logpath',
help='file to log untaggable albums for later review')
import_cmd.parser.add_option('-r', '--art', action='store_true',
default=None, help="try to download album art")
def import_func(lib, config, opts, args):
copy = opts.copy if opts.copy is not None else \
ui.config_val(config, 'beets', 'import_copy',
@ -324,7 +340,7 @@ def import_func(lib, config, opts, args):
ui.config_val(config, 'beets', 'import_write',
DEFAULT_IMPORT_WRITE, bool)
autot = opts.autotag if opts.autotag is not None else DEFAULT_IMPORT_AUTOT
import_files(lib, args, copy, write, autot, opts.logpath)
import_files(lib, args, copy, write, autot, opts.logpath, opts.art)
import_cmd.func = import_func
default_commands.append(import_cmd)

View file

@ -251,6 +251,7 @@ class ArtDestinationTest(unittest.TestCase):
def setUp(self):
self.lib = beets.library.Library(':memory:')
self.i = item()
self.i.path = '/some/music/file.mp3'
self.lib.art_filename = 'artimage'
def test_art_filename_respects_setting(self):
@ -259,7 +260,7 @@ class ArtDestinationTest(unittest.TestCase):
def test_art_path_in_item_dir(self):
art = self.lib.art_path(self.i, 'something.jpg')
track = self.lib.destination(self.i)
track = self.i.path
self.assertEqual(os.path.dirname(art), os.path.dirname(track))
class MigrationTest(unittest.TestCase):