copy album art from filesystem based on filename heuristics (#72)

This commit is contained in:
Adrian Sampson 2011-06-24 21:41:25 -07:00
parent 846b85556d
commit e64e20cc87
4 changed files with 41 additions and 14 deletions

2
NEWS
View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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__)