Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of genres. #266

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions musicbrainzngs/mbxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ def parse_artist(artist):
"release-list": parse_release_list,
"release-group-list": parse_release_group_list,
"work-list": parse_work_list,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"genre-list": parse_tag_and_genre_list,
"user-genre-list": parse_tag_and_genre_list,
"rating": parse_rating,
"ipi-list": parse_element_list,
"isni-list": parse_element_list,
Expand All @@ -272,8 +274,8 @@ def parse_place(place):
inner_els = {"area": parse_area,
"coordinates": parse_coordinates,
"life-span": parse_lifespan,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"alias-list": parse_alias_list,
"relation-list": parse_relation_list,
"annotation": parse_annotation}
Expand All @@ -293,8 +295,8 @@ def parse_event(event):
inner_els = {"life-span": parse_lifespan,
"relation-list": parse_relation_list,
"alias-list": parse_alias_list,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"rating": parse_rating}

result.update(parse_attributes(attribs, event))
Expand All @@ -307,7 +309,7 @@ def parse_instrument(instrument):
attribs = ["id", "type", "ext:score"]
elements = ["name", "description", "disambiguation"]
inner_els = {"relation-list": parse_relation_list,
"tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"alias-list": parse_alias_list,
"annotation": parse_annotation}
result.update(parse_attributes(attribs, instrument))
Expand All @@ -326,8 +328,8 @@ def parse_label(label):
inner_els = {"area": parse_area,
"life-span": parse_lifespan,
"release-list": parse_release_list,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"rating": parse_rating,
"ipi-list": parse_element_list,
"alias-list": parse_alias_list,
Expand Down Expand Up @@ -410,8 +412,10 @@ def parse_release(release):
"label-info-list": parse_label_info_list,
"medium-list": parse_medium_list,
"release-group": parse_release_group,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"genre-list": parse_tag_and_genre_list,
"user-genre-list": parse_tag_and_genre_list,
"relation-list": parse_relation_list,
"annotation": parse_annotation,
"cover-art-archive": parse_caa,
Expand Down Expand Up @@ -478,8 +482,10 @@ def parse_release_group(rg):
"disambiguation"]
inner_els = {"artist-credit": parse_artist_credit,
"release-list": parse_release_list,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"genre-list": parse_tag_and_genre_list,
"user-genre-list": parse_tag_and_genre_list,
"secondary-type-list": parse_element_list,
"relation-list": parse_relation_list,
"rating": parse_rating,
Expand All @@ -498,8 +504,10 @@ def parse_recording(recording):
elements = ["title", "length", "user-rating", "disambiguation", "video"]
inner_els = {"artist-credit": parse_artist_credit,
"release-list": parse_release_list,
"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"genre-list": parse_tag_and_genre_list,
"user-genre-list": parse_tag_and_genre_list,
"rating": parse_rating,
"isrc-list": parse_external_id_list,
"relation-list": parse_relation_list,
Expand Down Expand Up @@ -541,8 +549,10 @@ def parse_work(work):
result = {}
attribs = ["id", "ext:score", "type"]
elements = ["title", "user-rating", "language", "iswc", "disambiguation"]
inner_els = {"tag-list": parse_tag_list,
"user-tag-list": parse_tag_list,
inner_els = {"tag-list": parse_tag_and_genre_list,
"user-tag-list": parse_tag_and_genre_list,
"genre-list": parse_tag_and_genre_list,
"user-genre-list": parse_tag_and_genre_list,
"rating": parse_rating,
"alias-list": parse_alias_list,
"iswc-list": parse_element_list,
Expand Down Expand Up @@ -707,12 +717,14 @@ def parse_track(track):
result["track_or_recording_length"] = track_or_recording
return result

def parse_tag_list(tl):
return [parse_tag(t) for t in tl]

def parse_tag(tag):
def parse_tag_and_genre_list(tl):
return [parse_tag_and_genre(t) for t in tl]


def parse_tag_and_genre(tag):
result = {}
attribs = ["count"]
attribs = ["count", "id"]
elements = ["name"]

result.update(parse_attributes(attribs, tag))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><recording id="58169b2c-e31a-4a46-8741-71c672a089ac"><title>Penny Lane</title><length>178000</length><disambiguation>original mono studio mix</disambiguation><first-release-date>1967-02-17</first-release-date><tag-list><tag count="1"><name>classic pop</name></tag><tag count="2"><name>classic rock</name></tag><tag count="1"><name>mid to late 60's</name></tag><tag count="2"><name>pop</name></tag><tag count="1"><name>pop rock</name></tag><tag count="1"><name>psychedelic rock</name></tag><tag count="2"><name>rock</name></tag></tag-list><genre-list><genre count="2" id="93244085-20e5-4f16-9067-1d19143b3810"><name>classic rock</name></genre><genre count="2" id="911c7bbb-172d-4df8-9478-dbff4296e791"><name>pop</name></genre><genre count="1" id="797e2e85-5ffd-495c-a757-8b4079052f0e"><name>pop rock</name></genre><genre count="1" id="146ef761-5ad9-48b4-b0b3-483104f7da48"><name>psychedelic rock</name></genre><genre count="2" id="0e3fc579-2d24-4f20-9dae-736e1ec78798"><name>rock</name></genre></genre-list></recording></metadata>
12 changes: 12 additions & 0 deletions test/test_mbxml_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def testRecordingRelationCreditedAs(self):
attributes = rels[2]["attributes"]
expected = {"attribute": "piano", "credited-as": "Yamaha and Steinway pianos"}
self.assertEqual(expected, attributes[0])

def testTagsAndGenres(self):
res = _common.open_and_parse_test_data(self.datadir, "58169b2c-e31a-4a46-8741-71c672a089ac-tags-genres.xml")
recording = res["recording"]

tags = recording["tag-list"]
self.assertEqual(len(tags), 7)
self.assertEqual(tags[0], {"name": "classic pop", "count": "1"})

genres = recording["genre-list"]
self.assertEqual(len(genres), 5)
self.assertEqual(genres[0], {"name": "classic rock", "count": "2", "id": "93244085-20e5-4f16-9067-1d19143b3810"})