Skip to content

Commit

Permalink
musicbrainz plugin: Add option to write labelid. Fixes #1929
Browse files Browse the repository at this point in the history
Also display it in the result list like we used to
  • Loading branch information
lazka committed Jul 24, 2016
1 parent e4b6d65 commit 44c4e65
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
1 change: 1 addition & 0 deletions quodlibet/quodlibet/ext/songsmenu/brainz/__init__.py
Expand Up @@ -50,6 +50,7 @@ def PluginPreferences(self, win):
('albumartist', _('Write "_albumartist" when needed')),
('artist_sort', _('Write sort tags for artist names')),
('standard', _('Write _standard MusicBrainz tags')),
('labelid2', _('Write "labelid" tag')),
]

vb = Gtk.VBox()
Expand Down
8 changes: 7 additions & 1 deletion quodlibet/quodlibet/ext/songsmenu/brainz/mb.py
Expand Up @@ -52,7 +52,8 @@ def _get_release(release_id):

return musicbrainzngs.get_release_by_id(
release_id,
includes=["recordings", "artists", "artist-credits"])["release"]
includes=["recordings", "artists", "artist-credits", "labels"]
)["release"]


class Artist(object):
Expand Down Expand Up @@ -114,6 +115,11 @@ class Release(object):
def __init__(self, mbrelease):
self._mbrelease = mbrelease

@property
def labelid(self):
label_list = self._mbrelease.get("label-info-list", [])
return label_list[0]["catalog-number"] if label_list else u""

@property
def id(self):
"""MusicBrainz release ID"""
Expand Down
1 change: 1 addition & 0 deletions quodlibet/quodlibet/ext/songsmenu/brainz/util.py
Expand Up @@ -18,6 +18,7 @@ def get_config():
defaults.set("albumartist", True)
defaults.set("artist_sort", False)
defaults.set("standard", True)
defaults.set("labelid2", False)

return pc

Expand Down
15 changes: 10 additions & 5 deletions quodlibet/quodlibet/ext/songsmenu/brainz/widgets.py
Expand Up @@ -84,7 +84,8 @@ def celldata(layout, cell, model, iter_, data):
extra_info = ", ".join(
filter(None, [util.escape(release.date),
util.escape(release.country),
util.escape(release.medium_format)]))
util.escape(release.medium_format),
util.escape(release.labelid)]))

artist_names = [a.name for a in release.artists]
disc_count = release.disc_count
Expand Down Expand Up @@ -260,8 +261,7 @@ def build_song_data(release, track):
meta["album"] = release.title
meta["date"] = release.date
meta["musicbrainz_albumid"] = release.id
# we did write labelid before ngs, albumid supersedes it
meta["labelid"] = ""
meta["labelid"] = release.labelid

if not release.is_single_artist and not release.is_various_artists:
artists = release.artists
Expand Down Expand Up @@ -292,7 +292,8 @@ def build_song_data(release, track):
return meta


def apply_options(meta, year_only, albumartist, artistsort, musicbrainz):
def apply_options(meta, year_only, albumartist, artistsort, musicbrainz,
labelid):
"""Takes the tags extracted from musicbrainz and adjusts them according
to the user preferences.
"""
Expand All @@ -312,6 +313,9 @@ def apply_options(meta, year_only, albumartist, artistsort, musicbrainz):
if key.startswith("musicbrainz_"):
meta[key] = u""

if not labelid:
meta["labelid"] = ""


def apply_to_song(meta, song):
"""Applies the tags to a AudioFile instance"""
Expand Down Expand Up @@ -422,11 +426,12 @@ def _save(self):
albumartist = pconfig.getboolean("albumartist")
artistsort = pconfig.getboolean("artist_sort")
musicbrainz = pconfig.getboolean("standard")
labelid = pconfig.getboolean("labelid2")

for release, track, song in self.result_treeview.iter_tracks():
meta = build_song_data(release, track)
apply_options(
meta, year_only, albumartist, artistsort, musicbrainz)
meta, year_only, albumartist, artistsort, musicbrainz, labelid)
apply_to_song(meta, song)

self.destroy()
Expand Down
63 changes: 48 additions & 15 deletions quodlibet/tests/plugin/test_brainz.py
Expand Up @@ -12,12 +12,41 @@

brainz = modules.get("MusicBrainz lookup", None)

TEST_SEARCH_RESULT = \
{'release-count': 1, 'release-list': [{'status': 'Official', 'artist-credit':
[{'artist': {'sort-name': 'Necks, The', 'id':
'51f8d454-f4a8-41e6-8bd7-a35921eeedd0', 'name': 'The Necks'}}],
'label-info-list': [{'catalog-number': 'FOM 0008', 'label': {'id':
'b887f682-e9e5-40d2-b4c7-cdbbcd2b3787', 'name': 'Fish of Milk'}}], 'title':
'Athenaeum, Homebush, Quay & Raab', 'country': 'AU', 'medium-count': 4,
'release-event-list': [{'date': '2002', 'area': {'sort-name': 'Australia',
'iso-3166-1-code-list': ['AU'], 'id': '106e0bec-b638-3b37-b731-f53d507dc00e',
'name': 'Australia'}}], 'medium-list': [{}, {'disc-list': [], 'format': 'CD',
'track-list': [], 'track-count': 1, 'disc-count': 1}, {'disc-list': [],
'format': 'CD', 'track-list': [], 'track-count': 1, 'disc-count': 1},
{'disc-list': [], 'format': 'CD', 'track-list': [], 'track-count': 1,
'disc-count': 1}, {'disc-list': [], 'format': 'CD', 'track-list': [],
'track-count': 1, 'disc-count': 1}], 'text-representation': {'language':
'eng', 'script': 'Latn'}, 'ext:score': '100', 'date': '2002',
'artist-credit-phrase': 'The Necks', 'release-group': {'secondary-type-list':
['Live'], 'type': 'Live', 'id': '88e47489-a3d0-3344-864d-4b09188ba9e0',
'primary-type': 'Album'}, 'id': '3663a8a9-1c67-41c2-82c8-6a241d1558f7',
'asin': 'B00007FKRD'}]}


TEST_DATA = \
{'status': 'Official', 'artist-credit': [{'artist': {'sort-name':
'Autechre', 'id': '410c9baf-5469-44f6-9852-826524b80c61', 'name':
'Autechre'}}, ' & ', {'artist': {'sort-name': 'Hafler Trio, The', 'id':
{'status': 'Official', 'artist-credit': [{'artist': {'sort-name': 'Autechre',
'id': '410c9baf-5469-44f6-9852-826524b80c61', 'name': 'Autechre'}}, ' & ',
{'artist': {'sort-name': 'Hafler Trio, The', 'id':
'146c01d0-d3a2-44c3-acb5-9208bce75e14', 'name': 'The Hafler Trio'}}],
'label-info-list': [{'catalog-number': 'pgram002', 'label': {'sort-name':
'Phonometrography', 'id': 'a0759efa-f583-49ea-9a8d-d5bbce55541c', 'name':
'Phonometrography'}}], 'title': u'\xe6\xb3o & h\xb3\xe6',
'release-event-count': 1, 'medium-count': 2, 'cover-art-archive': {'count':
'1', 'front': 'true', 'back': 'false', 'artwork': 'true'},
'release-event-list': [{'date': '2003-12-04', 'area': {'sort-name':
'United Kingdom', 'iso-3166-1-code-list': ['GB'], 'id':
'8a754a16-0027-3a29-b6d7-2b40ea0481ed', 'name': 'United Kingdom'}}],
'medium-list': [{'position': '1', 'title': u'\xe6\xb3o', 'track-list':
[{'artist-credit': [{'artist': {'sort-name': 'Autechre', 'id':
'410c9baf-5469-44f6-9852-826524b80c61', 'name': 'Autechre'}}, ' & ',
Expand Down Expand Up @@ -46,13 +75,8 @@
'id': '5aff6309-2e02-4a47-9233-32d7dcc9a960', 'title': u'h\xb3\xe6'},
'length': '922546', 'position': '1', 'id':
'5f2031a2-c67d-3bec-8ae5-8d22847ab0a5', 'track_or_recording_length':
'922546'}], 'track-count': 1, 'format': 'CD'}], 'title': u'\xe6\xb3o &'
u'h\xb3\xe6', 'release-event-count': 1, 'medium-count': 2, 'cover-art-archive':
{'count': '1', 'front': 'true', 'back': 'false', 'artwork': 'true'},
'release-event-list': [{'date': '2003-12-04', 'area': {'sort-name': 'United '
'Kingdom', 'iso-3166-1-code-list': ['GB'], 'id':
'8a754a16-0027-3a29-b6d7-2b40ea0481ed', 'name': 'United Kingdom'}}],
'text-representation': {'language': 'eng', 'script': 'Latn'}, 'country': 'GB',
'922546'}], 'track-count': 1, 'format': 'CD'}], 'text-representation':
{'language': 'eng', 'script': 'Latn'}, 'label-info-count': 1, 'country': 'GB',
'date': '2003-12-04', 'artist-credit-phrase': 'Autechre & The Hafler Trio',
'quality': 'normal', 'id': '59211ea4-ffd2-4ad9-9a4e-941d3148024a'}

Expand Down Expand Up @@ -172,7 +196,7 @@ def test_release(self):
self.assertEqual(release.disc_count, 2)
self.assertEqual(release.track_count, 2)
self.assertEqual(len(release.tracks), 2)
self.assertEqual(release.title, u'\xe6\xb3o &h\xb3\xe6')
self.assertEqual(release.title, u'\xe6\xb3o & h\xb3\xe6')
self.assertTrue(release.is_single_artist)
self.assertFalse(release.is_various_artists)
self.assertTrue(release.artists)
Expand All @@ -190,6 +214,12 @@ def test_release_tracks(self):
self.assertEqual(track.track_count, 1)
self.assertEqual(track.disctitle, u"\xe6\xb3o")

def test_labelid(self):
Release = brainz.mb.Release

release = Release(TEST_SEARCH_RESULT["release-list"][0])
self.assertEqual(release.labelid, u"FOM 0008")

def test_release_artist(self):
Release = brainz.mb.Release

Expand All @@ -212,15 +242,17 @@ def test_build_metadata(self):
meta = build_song_data(release, track)
self.assertEqual(meta["tracknumber"], "1/1")
self.assertEqual(meta["discnumber"], "2/2")
self.assertEqual(meta["labelid"], "pgram002")

apply_options(meta, True, False, False, False)
apply_options(meta, True, False, False, False, False)
dummy = AudioFile()
apply_to_song(meta, dummy)
self.assertEqual(dummy("album"), u'\xe6\xb3o &h\xb3\xe6')
self.assertEqual(dummy("album"), u'\xe6\xb3o & h\xb3\xe6')
self.assertEqual(dummy("date"), u'2003')
self.assertEqual(dummy("title"), u'h\xb3\xe6')
self.assertEqual(dummy("pgram002"), u'')

def test_build_mbids(self):
def test_build_mbids_labelid(self):
Release = brainz.mb.Release
build_song_data = brainz.widgets.build_song_data
apply_options = brainz.widgets.apply_options
Expand All @@ -229,7 +261,7 @@ def test_build_mbids(self):
release = Release(TEST_DATA)
track = release.tracks[1]
meta = build_song_data(release, track)
apply_options(meta, True, False, False, True)
apply_options(meta, True, False, False, True, True)
dummy = AudioFile()
apply_to_song(meta, dummy)

Expand All @@ -239,6 +271,7 @@ def test_build_mbids(self):
dummy.list("musicbrainz_artistid"),
[u'410c9baf-5469-44f6-9852-826524b80c61',
u'146c01d0-d3a2-44c3-acb5-9208bce75e14'])
self.assertEqual(dummy("labelid"), u"pgram002")

def test_pregap(self):
Release = brainz.mb.Release
Expand Down

0 comments on commit 44c4e65

Please sign in to comment.