Skip to content

Commit 57b5f0b

Browse files
committed
Fixes #258
Signed-off-by: Ashhar Hasan <hashhar_dev@outlook.com>
1 parent e4bca39 commit 57b5f0b

File tree

1 file changed

+148
-5
lines changed

1 file changed

+148
-5
lines changed

musicbrainzngs/musicbrainz.py

+148-5
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,30 @@ def get_works_in_collection(collection, limit=None, offset=None):
12451245
"""
12461246
return _do_collection_query(collection, "works", limit, offset)
12471247

1248+
def get_areas_in_collection(collection, limit=None, offset=None):
1249+
"""List the areas in a collection.
1250+
Returns a dict with a 'collection' key, which again has a 'area-list'.
1251+
1252+
See `Browsing`_ for how to use `limit` and `offset`.
1253+
"""
1254+
return _do_collection_query(collection, "areas", limit, offset)
1255+
1256+
def get_labels_in_collection(collection, limit=None, offset=None):
1257+
"""List the labels in a collection.
1258+
Returns a dict with a 'collection' key, which again has a 'label-list'.
1259+
1260+
See `Browsing`_ for how to use `limit` and `offset`.
1261+
"""
1262+
return _do_collection_query(collection, "labels", limit, offset)
1263+
1264+
def get_release_groups_in_collection(collection, limit=None, offset=None):
1265+
"""List the release-groups in a collection.
1266+
Returns a dict with a 'collection' key, which again has a 'release-group-list'.
1267+
1268+
See `Browsing`_ for how to use `limit` and `offset`.
1269+
"""
1270+
return _do_collection_query(collection, "areas", limit, offset)
1271+
12481272

12491273
# Submission methods
12501274

@@ -1296,17 +1320,136 @@ def submit_ratings(**kwargs):
12961320
query = mbxml.make_rating_request(**kwargs)
12971321
return _do_mb_post("rating", query)
12981322

1323+
def _do_collection_put(collection, collection_type, entities=[]):
1324+
"""Add entities of a given type (one of areas, artists, events, labels,
1325+
places, recordings, releases, release-groups, or works). Collection and
1326+
the entities should be identified by their MBIDs. A maximum of 400
1327+
MBIDs can be submitted at once. See
1328+
https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#collections
1329+
for details.
1330+
"""
1331+
if len(entities) > 400:
1332+
raise WebServiceError("Maximum URI length of 16kb means we only allow 400 releases")
1333+
entitieslist = ";".join(entities)
1334+
return _do_mb_put("collection/%s/%s/%s" % (collection, collection_type, entitieslist))
1335+
1336+
def _do_collection_delete(collection, collection_type, entities=[]):
1337+
"""Remove entities of a given type (one of areas, artists, events, labels,
1338+
places, recordings, releases, release-groups, or works). Collection and
1339+
the entities should be identified by their MBIDs. A maximum of 400
1340+
MBIDs can be submitted at once. See
1341+
https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2#collections
1342+
for details.
1343+
"""
1344+
if len(entities) > 400:
1345+
raise WebServiceError("Maximum URI length of 16kb means we only allow 400 releases")
1346+
entitieslist = ";".join(entities)
1347+
return _do_mb_delete("collection/%s/%s/%s" % (collection, collection_type, entitieslist))
1348+
1349+
def add_areas_to_collection(collection, areas=[]):
1350+
"""Add areas to a collection.
1351+
Collection and areas should be identified by their MBIDs
1352+
"""
1353+
return _do_collection_put(collection, "areas", areas)
1354+
1355+
def remove_areas_from_collection(collection, areas=[]):
1356+
"""Remove areas from a collection.
1357+
Collection and areas should be identified by their MBIDs
1358+
"""
1359+
return _do_collection_delete(collection, "areas", areas)
1360+
1361+
def add_artists_to_collection(collection, artists=[]):
1362+
"""Add artists to a collection.
1363+
Collection and artists should be identified by their MBIDs
1364+
"""
1365+
return _do_collection_put(collection, "artists", artists)
1366+
1367+
def remove_artists_from_collection(collection, artists=[]):
1368+
"""Remove artists from a collection.
1369+
Collection and artists should be identified by their MBIDs
1370+
"""
1371+
return _do_collection_delete(collection, "artists", artists)
1372+
1373+
def add_events_to_collection(collection, events=[]):
1374+
"""Add events to a collection.
1375+
Collection and events should be identified by their MBIDs
1376+
"""
1377+
return _do_collection_put(collection, "events", events)
1378+
1379+
def remove_events_from_collection(collection, events=[]):
1380+
"""Remove events from a collection.
1381+
Collection and events should be identified by their MBIDs
1382+
"""
1383+
return _do_collection_delete(collection, "events", events)
1384+
1385+
def add_labels_to_collection(collection, labels=[]):
1386+
"""Add labels to a collection.
1387+
Collection and labels should be identified by their MBIDs
1388+
"""
1389+
return _do_collection_put(collection, "labels", labels)
1390+
1391+
def remove_labels_from_collection(collection, labels=[]):
1392+
"""Remove labels from a collection.
1393+
Collection and labels should be identified by their MBIDs
1394+
"""
1395+
return _do_collection_delete(collection, "labels", labels)
1396+
1397+
def add_places_to_collection(collection, places=[]):
1398+
"""Add places to a collection.
1399+
Collection and places should be identified by their MBIDs
1400+
"""
1401+
return _do_collection_put(collection, "places", places)
1402+
1403+
def remove_places_from_collection(collection, places=[]):
1404+
"""Remove places from a collection.
1405+
Collection and places should be identified by their MBIDs
1406+
"""
1407+
return _do_collection_delete(collection, "places", places)
1408+
1409+
def add_recordings_to_collection(collection, recordings=[]):
1410+
"""Add recordings to a collection.
1411+
Collection and recordings should be identified by their MBIDs
1412+
"""
1413+
return _do_collection_put(collection, "recordings", recordings)
1414+
1415+
def remove_recordings_from_collection(collection, recordings=[]):
1416+
"""Remove recordings from a collection.
1417+
Collection and recordings should be identified by their MBIDs
1418+
"""
1419+
return _do_collection_delete(collection, "recordings", recordings)
1420+
12991421
def add_releases_to_collection(collection, releases=[]):
13001422
"""Add releases to a collection.
13011423
Collection and releases should be identified by their MBIDs
13021424
"""
1303-
# XXX: Maximum URI length of 16kb means we should only allow ~400 releases
1304-
releaselist = ";".join(releases)
1305-
return _do_mb_put("collection/%s/releases/%s" % (collection, releaselist))
1425+
return _do_collection_put(collection, "releases", releases)
13061426

13071427
def remove_releases_from_collection(collection, releases=[]):
13081428
"""Remove releases from a collection.
13091429
Collection and releases should be identified by their MBIDs
13101430
"""
1311-
releaselist = ";".join(releases)
1312-
return _do_mb_delete("collection/%s/releases/%s" % (collection, releaselist))
1431+
return _do_collection_delete(collection, "releases", releases)
1432+
1433+
def add_release_groups_to_collection(collection, release_groups=[]):
1434+
"""Add release-groups to a collection.
1435+
Collection and release-groups should be identified by their MBIDs
1436+
"""
1437+
return _do_collection_put(collection, "release-groups", release_groups)
1438+
1439+
def remove_release_groups_from_collection(collection, release_groups=[]):
1440+
"""Remove release-groups from a collection.
1441+
Collection and release-groups should be identified by their MBIDs
1442+
"""
1443+
return _do_collection_delete(collection, "release-groups", release_groups)
1444+
1445+
def add_works_to_collection(collection, works=[]):
1446+
"""Add works to a collection.
1447+
Collection and works should be identified by their MBIDs
1448+
"""
1449+
return _do_collection_put(collection, "works", works)
1450+
1451+
def remove_works_from_collection(collection, works=[]):
1452+
"""Remove works from a collection.
1453+
Collection and works should be identified by their MBIDs
1454+
"""
1455+
return _do_collection_delete(collection, "works", works)

0 commit comments

Comments
 (0)