From e64e20cc87366b5da6ea589b7d8e7570181fda43 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 24 Jun 2011 21:41:25 -0700 Subject: [PATCH] copy album art from filesystem based on filename heuristics (#72) --- NEWS | 2 ++ beets/autotag/art.py | 9 +++++++-- beets/importer.py | 2 +- test/test_art.py | 42 +++++++++++++++++++++++++++++++----------- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 6bc065ac0..aa1cdb71c 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,7 @@ 1.0b9 ----- +* Album art is now automatically discovered and copied from the + imported directories when available. * Prompts in the importer interface are now colorized for easy reading. The default option is always highlighted. * The "list" command now accepts a "-p" switch that causes it to show diff --git a/beets/autotag/art.py b/beets/autotag/art.py index eaefeeda4..a6a37128c 100644 --- a/beets/autotag/art.py +++ b/beets/autotag/art.py @@ -81,10 +81,15 @@ def art_in_path(path): # Main interface. -def art_for_album(album): +def art_for_album(album, path): """Given an album info dictionary from MusicBrainz, returns a path to downloaded art for the album (or None if no art is found). """ + if isinstance(path, basestring): + out = art_in_path(path) + if out: + return out + if album['asin']: log.debug('Fetching album art for ASIN %s.' % album['asin']) return art_for_asin(album['asin']) @@ -101,7 +106,7 @@ if __name__ == '__main__': if not album: print 'album not found' else: - fn = art_for_album(album) + fn = art_for_album(album, None) if fn: print fn print len(open(fn).read())/1024 diff --git a/beets/importer.py b/beets/importer.py index 240715923..1840d984e 100644 --- a/beets/importer.py +++ b/beets/importer.py @@ -505,7 +505,7 @@ def fetch_art(config): continue if task.should_fetch_art(): - artpath = beets.autotag.art.art_for_album(task.info) + artpath = beets.autotag.art.art_for_album(task.info, task.path) # Save the art if any was found. if artpath: diff --git a/test/test_art.py b/test/test_art.py index bcf0ee297..f8bb82b68 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -44,17 +44,6 @@ class AmazonArtTest(unittest.TestCase): artpath = art.art_for_asin('xxxx') self.assertEqual(artpath, 'somepath') - def test_main_interface_returns_amazon_art(self): - art.urllib.urlretrieve = MockUrlRetrieve('anotherpath', 'image/jpeg') - album = {'asin': 'xxxx'} - artpath = art.art_for_album(album) - self.assertEqual(artpath, 'anotherpath') - - def test_main_interface_returns_none_for_missing_asin(self): - album = {'asin': None} - artpath = art.art_for_album(album) - self.assertEqual(artpath, None) - class FSArtTest(unittest.TestCase): def setUp(self): self.dpath = os.path.join(_common.RSRC, 'arttest') @@ -78,6 +67,37 @@ class FSArtTest(unittest.TestCase): fn = art.art_in_path(self.dpath) self.assertEqual(fn, None) +class CombinedTest(unittest.TestCase): + def setUp(self): + self.dpath = os.path.join(_common.RSRC, 'arttest') + os.mkdir(self.dpath) + def tearDown(self): + shutil.rmtree(self.dpath) + + def test_main_interface_returns_amazon_art(self): + art.urllib.urlretrieve = MockUrlRetrieve('anotherpath', 'image/jpeg') + album = {'asin': 'xxxx'} + artpath = art.art_for_album(album, None) + self.assertEqual(artpath, 'anotherpath') + + def test_main_interface_returns_none_for_missing_asin_and_path(self): + album = {'asin': None} + artpath = art.art_for_album(album, None) + self.assertEqual(artpath, None) + + def test_main_interface_gives_precedence_to_fs_art(self): + _common.touch(os.path.join(self.dpath, 'a.jpg')) + art.urllib.urlretrieve = MockUrlRetrieve('anotherpath', 'image/jpeg') + album = {'asin': 'xxxx'} + artpath = art.art_for_album(album, self.dpath) + self.assertEqual(artpath, os.path.join(self.dpath, 'a.jpg')) + + def test_main_interface_falls_back_to_amazon(self): + art.urllib.urlretrieve = MockUrlRetrieve('anotherpath', 'image/jpeg') + album = {'asin': 'xxxx'} + artpath = art.art_for_album(album, self.dpath) + self.assertEqual(artpath, 'anotherpath') + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)