From ef89daf1bea40114430b65d2cace90b7078a4f6b Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Fri, 27 Jun 2014 23:32:57 +0200 Subject: [PATCH 1/2] test_art: add unit test the test checks that the keyword priority (related to its position in the keywords list) is considered when selecting image filename amongst several candidates --- test/test_art.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test_art.py b/test/test_art.py index 6d81496cc..30723ae7a 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -78,6 +78,12 @@ class FSArtTest(_common.TestCase): fn = fetchart.art_in_path(self.dpath, ('art',), True) self.assertEqual(fn, None) + def test_precedence_amongst_correct_files(self): + _common.touch(os.path.join(self.dpath, 'back.jpg')) + _common.touch(os.path.join(self.dpath, 'front.jpg')) + _common.touch(os.path.join(self.dpath, 'front-cover.jpg')) + fn = fetchart.art_in_path(self.dpath, ('cover', 'front', 'back'), False) + self.assertEqual(fn, os.path.join(self.dpath, 'front-cover.jpg')) class CombinedTest(_common.TestCase): ASIN = 'xxxx' From e16beb8d40a00467511c65e3be4c83f3ecac0de9 Mon Sep 17 00:00:00 2001 From: Fabrice Laporte Date: Fri, 27 Jun 2014 23:44:12 +0200 Subject: [PATCH 2/2] sort image names before checking for matches images that contain substring that comes first in the cover_names option list are prioritized --- beetsplug/fetchart.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 3658d4afb..ac282179c 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -153,6 +153,11 @@ def google_art(album): # Art from the filesystem. +def sort_by_keywords_priority(image, cover_names): + """Sorting function for image names. Return indexes of cover names found + in the image filename.""" + return [idx for (idx, x) in enumerate(cover_names) if x in image] + def art_in_path(path, cover_names, cautious): """Look for album art files in a specified directory.""" @@ -167,6 +172,8 @@ def art_in_path(path, cover_names, cautious): images.append(fn) # Look for "preferred" filenames. + images = sorted(images, key=lambda x: sort_by_keywords_priority(x, + cover_names)) cover_pat = r"(\b|_)({0})(\b|_)".format('|'.join(cover_names)) for fn in images: if re.search(cover_pat, os.path.splitext(fn)[0], re.I):