From e9289ca554fe0e9f755e8156175f76dc001bd536 Mon Sep 17 00:00:00 2001 From: Ignasi Fosch Date: Sun, 27 Jul 2014 16:34:28 +0200 Subject: [PATCH] Checks for musicbrainz album ID and sets images to a list containing the corresponding URL --- mopidy/audio/scan.py | 12 ++++++++++++ mopidy/local/commands.py | 3 ++- tests/audio/test_scan.py | 14 +++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mopidy/audio/scan.py b/mopidy/audio/scan.py index 6c23e954..2f6ac4c3 100644 --- a/mopidy/audio/scan.py +++ b/mopidy/audio/scan.py @@ -150,6 +150,18 @@ def _date(tags): return None +def add_musicbrainz_cover_art(track): + if track.album is not None and track.album.musicbrainz_id is not None: + base = "http://coverartarchive.org/release" + images = frozenset( + ["{}/{}/front".format( + base, + track.album.musicbrainz_id)]) + album = track.album.copy(images=images) + track = track.copy(album=album) + return track + + def audio_data_to_track(data): """Convert taglist data + our extras to a track.""" tags = data['tags'] diff --git a/mopidy/local/commands.py b/mopidy/local/commands.py index 1e7839a5..f2a7ec24 100644 --- a/mopidy/local/commands.py +++ b/mopidy/local/commands.py @@ -118,7 +118,8 @@ class ScanCommand(commands.Command): relpath = translator.local_track_uri_to_path(uri, media_dir) file_uri = path.path_to_uri(os.path.join(media_dir, relpath)) data = scanner.scan(file_uri) - track = scan.audio_data_to_track(data).copy(uri=uri) + track = scan.add_musicbrainz_cover_art( + scan.audio_data_to_track(data).copy(uri=uri)).copy(uri=uri) library.add(track) logger.debug('Added %s', track.uri) except exceptions.ScannerError as error: diff --git a/tests/audio/test_scan.py b/tests/audio/test_scan.py index 26caa422..1e352991 100644 --- a/tests/audio/test_scan.py +++ b/tests/audio/test_scan.py @@ -71,6 +71,11 @@ class TranslatorTest(unittest.TestCase): actual = scan.audio_data_to_track(self.data) self.assertEqual(expected, actual) + def check_local(self, expected): + actual = scan.add_musicbrainz_cover_art( + scan.audio_data_to_track(self.data)) + self.assertEqual(expected, actual) + def test_track(self): self.check(self.track) @@ -197,13 +202,20 @@ class TranslatorTest(unittest.TestCase): def test_missing_album_musicbrainz_id(self): del self.data['tags']['musicbrainz-albumid'] - album = self.track.album.copy(musicbrainz_id=None) + album = self.track.album.copy(musicbrainz_id=None, + images=[]) self.check(self.track.copy(album=album)) def test_multiple_album_musicbrainz_id(self): self.data['tags']['musicbrainz-albumid'].append('id') self.check(self.track) + def test_album_musicbrainz_id_cover(self): + album = self.track.album.copy( + images=frozenset( + ['http://coverartarchive.org/release/albumid/front'])) + self.check_local(self.track.copy(album=album)) + def test_missing_album_num_tracks(self): del self.data['tags']['track-count'] album = self.track.album.copy(num_tracks=None)