diff --git a/musicbrainzngs/mbxml.py b/musicbrainzngs/mbxml.py index 345a2fa..dd0420d 100644 --- a/musicbrainzngs/mbxml.py +++ b/musicbrainzngs/mbxml.py @@ -182,8 +182,12 @@ def parse_collection(collection): result = {} attribs = ["id", "type", "entity-type"] elements = ["name", "editor"] - # TODO: add event-list: - inner_els = {"release-list": parse_release_list} + inner_els = {"release-list": parse_release_list, + "artist-list": parse_artist_list, + "event-list": parse_event_list, + "place-list": parse_place_list, + "recording-list": parse_recording_list, + "work-list": parse_work_list} result.update(parse_attributes(attribs, collection)) result.update(parse_elements(elements, inner_els, collection)) diff --git a/musicbrainzngs/musicbrainz.py b/musicbrainzngs/musicbrainz.py index 9c7ac9c..8d73fc3 100644 --- a/musicbrainzngs/musicbrainz.py +++ b/musicbrainzngs/musicbrainz.py @@ -1182,16 +1182,59 @@ def get_collections(): # Missing the count in the reply return _do_mb_query("collection", '') +def _do_collection_query(collection, collection_type, limit, offset): + params = {} + if limit: params["limit"] = limit + if offset: params["offset"] = offset + return _do_mb_query("collection", "%s/%s" % (collection, collection_type), [], params) + +def get_artists_in_collection(collection, limit=None, offset=None): + """List the artists in a collection. + Returns a dict with a 'collection' key, which again has a 'artist-list'. + + See `Browsing`_ for how to use `limit` and `offset`. + """ + return _do_collection_query(collection, "artists", limit, offset) + def get_releases_in_collection(collection, limit=None, offset=None): """List the releases in a collection. Returns a dict with a 'collection' key, which again has a 'release-list'. See `Browsing`_ for how to use `limit` and `offset`. """ - params = {} - if limit: params["limit"] = limit - if offset: params["offset"] = offset - return _do_mb_query("collection", "%s/releases" % collection, [], params) + return _do_collection_query(collection, "releases", limit, offset) + +def get_events_in_collection(collection, limit=None, offset=None): + """List the events in a collection. + Returns a dict with a 'collection' key, which again has a 'event-list'. + + See `Browsing`_ for how to use `limit` and `offset`. + """ + return _do_collection_query(collection, "events", limit, offset) + +def get_places_in_collection(collection, limit=None, offset=None): + """List the places in a collection. + Returns a dict with a 'collection' key, which again has a 'place-list'. + + See `Browsing`_ for how to use `limit` and `offset`. + """ + return _do_collection_query(collection, "places", limit, offset) + +def get_recordings_in_collection(collection, limit=None, offset=None): + """List the recordings in a collection. + Returns a dict with a 'collection' key, which again has a 'recording-list'. + + See `Browsing`_ for how to use `limit` and `offset`. + """ + return _do_collection_query(collection, "recordings", limit, offset) + +def get_works_in_collection(collection, limit=None, offset=None): + """List the works in a collection. + Returns a dict with a 'collection' key, which again has a 'work-list'. + + See `Browsing`_ for how to use `limit` and `offset`. + """ + return _do_collection_query(collection, "works", limit, offset) # Submission methods diff --git a/test/data/collection/20562e36-c7cc-44fb-96b4-486d51a1174b-events.xml b/test/data/collection/20562e36-c7cc-44fb-96b4-486d51a1174b-events.xml new file mode 100644 index 0000000..6e57b6c --- /dev/null +++ b/test/data/collection/20562e36-c7cc-44fb-96b4-486d51a1174b-events.xml @@ -0,0 +1 @@ +event collectionalastairpT on the Fringe 20062006-08-042006-08-30 \ No newline at end of file diff --git a/test/data/collection/2326c2e8-be4b-4300-acc6-dbd0adf5645b-works.xml b/test/data/collection/2326c2e8-be4b-4300-acc6-dbd0adf5645b-works.xml new file mode 100644 index 0000000..3883252 --- /dev/null +++ b/test/data/collection/2326c2e8-be4b-4300-acc6-dbd0adf5645b-works.xml @@ -0,0 +1 @@ +work collectionalastairpMaggot Brain \ No newline at end of file diff --git a/test/data/collection/29611d8b-b3ad-4ffb-acb5-27f77342a5b0-artists.xml b/test/data/collection/29611d8b-b3ad-4ffb-acb5-27f77342a5b0-artists.xml new file mode 100644 index 0000000..ef40493 --- /dev/null +++ b/test/data/collection/29611d8b-b3ad-4ffb-acb5-27f77342a5b0-artists.xml @@ -0,0 +1 @@ +artist collectionalastairpQueenQueenUK rock group1970-06-27 \ No newline at end of file diff --git a/test/data/collection/855b134e-9a3b-4717-8df8-8c4838d89924-places.xml b/test/data/collection/855b134e-9a3b-4717-8df8-8c4838d89924-places.xml new file mode 100644 index 0000000..6c248f8 --- /dev/null +++ b/test/data/collection/855b134e-9a3b-4717-8df8-8c4838d89924-places.xml @@ -0,0 +1 @@ +place collectionalastairpSan Francisco Bath Houseaka 'San Fran'
171 Cuba Street, Wellington, New Zealand
\ No newline at end of file diff --git a/test/data/collection/a91320b2-fd2f-4a93-9e4e-603d16d514b6-recordings.xml b/test/data/collection/a91320b2-fd2f-4a93-9e4e-603d16d514b6-recordings.xml new file mode 100644 index 0000000..8a38f33 --- /dev/null +++ b/test/data/collection/a91320b2-fd2f-4a93-9e4e-603d16d514b6-recordings.xml @@ -0,0 +1 @@ +recording collectionalastairpMaggot Brain1201000 \ No newline at end of file diff --git a/test/test_mbxml_collection.py b/test/test_mbxml_collection.py index 4bd072a..99f84c6 100644 --- a/test/test_mbxml_collection.py +++ b/test/test_mbxml_collection.py @@ -23,36 +23,98 @@ def setUp(self): def testGetCollection(self): musicbrainzngs.get_releases_in_collection("0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac") self.assertEqual("http://musicbrainz.org/ws/2/collection/0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac/releases", self.opener.get_url()) - # TODO: get events_in_collection + + musicbrainzngs.get_works_in_collection("898676a6-bc79-4fe2-98ae-79c5940fe1a2") + self.assertEqual("http://musicbrainz.org/ws/2/collection/898676a6-bc79-4fe2-98ae-79c5940fe1a2/works", self.opener.get_url()) + + musicbrainzngs.get_events_in_collection("65cb5dda-44aa-44a8-9c0d-4f99a14ab944") + self.assertEqual("http://musicbrainz.org/ws/2/collection/65cb5dda-44aa-44a8-9c0d-4f99a14ab944/events", self.opener.get_url()) + + musicbrainzngs.get_places_in_collection("9dde4c3c-520a-4bfd-9aae-446c3a04ce0c") + self.assertEqual("http://musicbrainz.org/ws/2/collection/9dde4c3c-520a-4bfd-9aae-446c3a04ce0c/places", self.opener.get_url()) + + musicbrainzngs.get_recordings_in_collection("42bc6dd9-8deb-4bd7-83eb-5dacdb218b38") + self.assertEqual("http://musicbrainz.org/ws/2/collection/42bc6dd9-8deb-4bd7-83eb-5dacdb218b38/recordings", self.opener.get_url()) + + musicbrainzngs.get_artists_in_collection("7e582256-b3ce-421f-82ba-451b0ab080eb") + self.assertEqual("http://musicbrainz.org/ws/2/collection/7e582256-b3ce-421f-82ba-451b0ab080eb/artists", self.opener.get_url()) class GetCollectionTest(unittest.TestCase): def setUp(self): self.datadir = os.path.join(os.path.dirname(__file__), "data", "collection") - def testCollectionType(self): - """ - Test if the type of the collection is parsed correctly. - """ - - res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml") - self.assertEqual(res["collection"]["entity-type"], "release") - self.assertEqual(res["collection"]["type"], "Release") - # TODO: example for Event type - def testCollectionInfo(self): """ Test that the id, name and author are given. """ res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml") - self.assertEqual(res["collection"]["id"], "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac") - self.assertEqual(res["collection"]["name"], "My Collection") - self.assertEqual(res["collection"]["editor"], "JonnyJD") def testCollectionReleases(self): """ Test that the list of releases is given. """ res = _common.open_and_parse_test_data(self.datadir, "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac-releases.xml") - self.assertEqual(res["collection"]["release-count"], 400) + coll = res["collection"] + self.assertEqual(coll["id"], "0b15c97c-8eb8-4b4f-81c3-0eb24266a2ac") + self.assertEqual(coll["name"], "My Collection") + self.assertEqual(coll["editor"], "JonnyJD") + self.assertEqual(coll["entity-type"], "release") + self.assertEqual(coll["type"], "Release") + self.assertEqual(coll["release-count"], 400) self.assertTrue("release-list" in res["collection"]) + + def testCollectionWorks(self): + res = _common.open_and_parse_test_data(self.datadir, "2326c2e8-be4b-4300-acc6-dbd0adf5645b-works.xml") + coll = res["collection"] + self.assertEqual(coll["id"], "2326c2e8-be4b-4300-acc6-dbd0adf5645b") + self.assertEqual(coll["name"], "work collection") + self.assertEqual(coll["editor"], "alastairp") + self.assertEqual(coll["entity-type"], "work") + self.assertEqual(coll["type"], "Work") + self.assertEqual(coll["work-count"], 1) + self.assertEqual(len(coll["work-list"]), 1) + + def testCollectionArtists(self): + res = _common.open_and_parse_test_data(self.datadir, "29611d8b-b3ad-4ffb-acb5-27f77342a5b0-artists.xml") + coll = res["collection"] + self.assertEqual(coll["id"], "29611d8b-b3ad-4ffb-acb5-27f77342a5b0") + self.assertEqual(coll["name"], "artist collection") + self.assertEqual(coll["editor"], "alastairp") + self.assertEqual(coll["entity-type"], "artist") + self.assertEqual(coll["type"], "Artist") + self.assertEqual(coll["artist-count"], 1) + self.assertEqual(len(coll["artist-list"]), 1) + + def testCollectionEvents(self): + res = _common.open_and_parse_test_data(self.datadir, "20562e36-c7cc-44fb-96b4-486d51a1174b-events.xml") + coll = res["collection"] + self.assertEqual(coll["id"], "20562e36-c7cc-44fb-96b4-486d51a1174b") + self.assertEqual(coll["name"], "event collection") + self.assertEqual(coll["editor"], "alastairp") + self.assertEqual(coll["entity-type"], "event") + self.assertEqual(coll["type"], "Event") + self.assertEqual(coll["event-count"], 1) + self.assertEqual(len(coll["event-list"]), 1) + + def testCollectionPlaces(self): + res = _common.open_and_parse_test_data(self.datadir, "855b134e-9a3b-4717-8df8-8c4838d89924-places.xml") + coll = res["collection"] + self.assertEqual(coll["id"], "855b134e-9a3b-4717-8df8-8c4838d89924") + self.assertEqual(coll["name"], "place collection") + self.assertEqual(coll["editor"], "alastairp") + self.assertEqual(coll["entity-type"], "place") + self.assertEqual(coll["type"], "Place") + self.assertEqual(coll["place-count"], 1) + self.assertEqual(len(coll["place-list"]), 1) + + def testCollectionRecordings(self): + res = _common.open_and_parse_test_data(self.datadir, "a91320b2-fd2f-4a93-9e4e-603d16d514b6-recordings.xml") + coll = res["collection"] + self.assertEqual(coll["id"], "a91320b2-fd2f-4a93-9e4e-603d16d514b6") + self.assertEqual(coll["name"], "recording collection") + self.assertEqual(coll["editor"], "alastairp") + self.assertEqual(coll["entity-type"], "recording") + self.assertEqual(coll["type"], "Recording") + self.assertEqual(coll["recording-count"], 1) + self.assertEqual(len(coll["recording-list"]), 1)