Skip to content

Commit e326aaf

Browse files
authored
Allow selecting either tags or genres in the includes, defaulting to genres (#5874)
Genres is a filtered list based on what musicbrainz considers a genre, tags are all the user-submitted tags. [1] 1. https://musicbrainz.org/doc/MusicBrainz_API#:~:text=Since%20genres%20are,!).
2 parents f3da80e + 672bf0b commit e326aaf

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

beetsplug/musicbrainz.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def get_message(self):
9090
"isrcs",
9191
"url-rels",
9292
"release-rels",
93+
"genres",
9394
"tags",
9495
}
9596
& set(musicbrainzngs.VALID_INCLUDES["release"])
@@ -370,6 +371,10 @@ def _merge_pseudo_and_actual_album(
370371

371372

372373
class MusicBrainzPlugin(MetadataSourcePlugin):
374+
@cached_property
375+
def genres_field(self) -> str:
376+
return f"{self.config['genres_tag'].as_choice(['genre', 'tag'])}-list"
377+
373378
def __init__(self):
374379
"""Set up the python-musicbrainz-ngs module according to settings
375380
from the beets configuration. This should be called at startup.
@@ -382,6 +387,7 @@ def __init__(self):
382387
"ratelimit": 1,
383388
"ratelimit_interval": 1,
384389
"genres": False,
390+
"genres_tag": "genre",
385391
"external_ids": {
386392
"discogs": False,
387393
"bandcamp": False,
@@ -723,8 +729,8 @@ def album_info(self, release: JSONDict) -> beets.autotag.hooks.AlbumInfo:
723729

724730
if self.config["genres"]:
725731
sources = [
726-
release["release-group"].get("tag-list", []),
727-
release.get("tag-list", []),
732+
release["release-group"].get(self.genres_field, []),
733+
release.get(self.genres_field, []),
728734
]
729735
genres: Counter[str] = Counter()
730736
for source in sources:

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ been dropped.
1313
New features:
1414

1515
- :doc:`plugins/ftintitle`: Added argument for custom feat. words in ftintitle.
16+
- :doc:`plugins/musicbrainz`: Allow selecting tags or genres to populate the
17+
genres tag.
1618
- :doc:`plugins/ftintitle`: Added argument to skip the processing of artist and
1719
album artist are the same in ftintitle.
1820
- :doc:`plugins/play`: Added `$playlist` marker to precisely edit the playlist

docs/plugins/musicbrainz.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Default
3232
ratelimit_interval: 1.0
3333
extra_tags: []
3434
genres: no
35+
genres_tag: genre
3536
external_ids:
3637
discogs: no
3738
bandcamp: no
@@ -136,6 +137,12 @@ Default
136137
``beatport_album_id``, ``deezer_album_id``, ``tidal_album_id``). On re-imports
137138
existing data will be overwritten.
138139

140+
.. conf:: genres_tag
141+
:default: genre
142+
143+
Either ``genre`` or ``tag``. Specify ``genre`` to use just musicbrainz genre and
144+
``tag`` to use all user-supplied musicbrainz tags.
145+
139146
.. include:: ./shared_metadata_source_config.rst
140147

141148
.. _building search indexes: https://musicbrainz.org/doc/Development/Search_server_setup

test/plugins/test_musicbrainz.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def _make_release(
6565
],
6666
"date": "3001",
6767
"medium-list": [],
68+
"genre-list": [{"count": 1, "name": "GENRE"}],
69+
"tag-list": [{"count": 1, "name": "TAG"}],
6870
"label-info-list": [
6971
{
7072
"catalog-number": "CATALOG NUMBER",
@@ -515,6 +517,26 @@ def test_data_source(self):
515517
d = self.mb.album_info(release)
516518
assert d.data_source == "MusicBrainz"
517519

520+
def test_genres(self):
521+
config["musicbrainz"]["genres"] = True
522+
config["musicbrainz"]["genres_tag"] = "genre"
523+
release = self._make_release()
524+
d = self.mb.album_info(release)
525+
assert d.genre == "GENRE"
526+
527+
def test_tags(self):
528+
config["musicbrainz"]["genres"] = True
529+
config["musicbrainz"]["genres_tag"] = "tag"
530+
release = self._make_release()
531+
d = self.mb.album_info(release)
532+
assert d.genre == "TAG"
533+
534+
def test_no_genres(self):
535+
config["musicbrainz"]["genres"] = False
536+
release = self._make_release()
537+
d = self.mb.album_info(release)
538+
assert d.genre is None
539+
518540
def test_ignored_media(self):
519541
config["match"]["ignored_media"] = ["IGNORED1", "IGNORED2"]
520542
tracks = [

0 commit comments

Comments
 (0)