Skip to content

Commit f67c28b

Browse files
Fix #6302: musicbrainz: crash when releases lack the "track" key. (#6364)
## Description Fixes #6302 Todo: Test. I could not figure out how to add a test for this bug. <!-- - If you believe one of below checkpoints is not required for the change you are submitting, cross it out and check the box nonetheless to let us know. For example: - [x] ~Changelog~ - Regarding the changelog, often it makes sense to add your entry only once reviewing is finished. That way you might prevent conflicts from other PR's in that file, as well as keep the chance high your description fits with the latest revision of your feature/fix. - Regarding documentation, bugfixes often don't require additions to the docs. - Please remove the descriptive sentences in braces from the enumeration below, which helps to unclutter your PR description. --> - [x] Documentation. (If you've added a new command-line flag, for example, find the appropriate page under `docs/` to describe it.) - [x] Changelog. (Add an entry to `docs/changelog.rst` to the bottom of one of the lists near the top of the document.) - [x] Tests. (Very much encouraged but not strictly required.)
2 parents bc52682 + be8d25f commit f67c28b

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

beetsplug/musicbrainz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def album_info(self, release: JSONDict) -> beets.autotag.hooks.AlbumInfo:
477477
release["artist-credit"], include_join_phrase=False
478478
)
479479

480-
ntracks = sum(len(m["tracks"]) for m in release["media"])
480+
ntracks = sum(len(m.get("tracks", [])) for m in release["media"])
481481

482482
# The MusicBrainz API omits 'relations'
483483
# when the release has more than 500 tracks. So we use browse_recordings
@@ -512,7 +512,7 @@ def album_info(self, release: JSONDict) -> beets.autotag.hooks.AlbumInfo:
512512
if format in config["match"]["ignored_media"].as_str_seq():
513513
continue
514514

515-
all_tracks = medium["tracks"]
515+
all_tracks = medium.get("tracks", [])
516516
if (
517517
"data-tracks" in medium
518518
and not config["match"]["ignore_data_tracks"]

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Unreleased
1616
Bug fixes
1717
~~~~~~~~~
1818

19+
- :doc:`plugins/musicbrainz`: Fix crash when release mediums lack the ``tracks``
20+
key. :bug:`6302`
1921
- :doc:`plugins/musicbrainz`: Fix search terms escaping. :bug:`6347`
2022
- :doc:`plugins/musicbrainz`: Fix support for ``alias`` and ``tracks``
2123
:conf:`plugins.musicbrainz:extra_tags`.

test/plugins/test_musicbrainz.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,23 @@ def test_track_disambiguation(self):
691691
assert t[0].trackdisambig is None
692692
assert t[1].trackdisambig == "SECOND TRACK"
693693

694+
def test_missing_tracks(self):
695+
tracks = [
696+
self._make_track("TITLE ONE", "ID ONE", 100.0 * 1000.0),
697+
self._make_track(
698+
"TITLE TWO",
699+
"ID TWO",
700+
200.0 * 1000.0,
701+
disambiguation="SECOND TRACK",
702+
),
703+
]
704+
release = self._make_release(tracks=tracks)
705+
release["media"].append(release["media"][0])
706+
del release["media"][0]["tracks"]
707+
del release["media"][0]["data-tracks"]
708+
d = self.mb.album_info(release)
709+
assert d.mediums == 2
710+
694711

695712
class ArtistFlatteningTest(unittest.TestCase):
696713
def _credit_dict(self, suffix=""):

0 commit comments

Comments
 (0)