Skip to content

Commit abcc8cd

Browse files
committed
Add support for more collection types
Signed-off-by: Ashhar Hasan <hashhar_dev@outlook.com>
1 parent 1638c62 commit abcc8cd

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
@@ -1251,6 +1251,30 @@ def get_works_in_collection(collection, limit=None, offset=None):
12511251
"""
12521252
return _do_collection_query(collection, "works", limit, offset)
12531253

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

12551279
# Submission methods
12561280

@@ -1302,17 +1326,136 @@ def submit_ratings(**kwargs):
13021326
query = mbxml.make_rating_request(**kwargs)
13031327
return _do_mb_post("rating", query)
13041328

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

13131433
def remove_releases_from_collection(collection, releases=[]):
13141434
"""Remove releases from a collection.
13151435
Collection and releases should be identified by their MBIDs
13161436
"""
1317-
releaselist = ";".join(releases)
1318-
return _do_mb_delete("collection/%s/releases/%s" % (collection, releaselist))
1437+
return _do_collection_delete(collection, "releases", releases)
1438+
1439+
def add_release_groups_to_collection(collection, release_groups=[]):
1440+
"""Add release-groups to a collection.
1441+
Collection and release-groups should be identified by their MBIDs
1442+
"""
1443+
return _do_collection_put(collection, "release-groups", release_groups)
1444+
1445+
def remove_release_groups_from_collection(collection, release_groups=[]):
1446+
"""Remove release-groups from a collection.
1447+
Collection and release-groups should be identified by their MBIDs
1448+
"""
1449+
return _do_collection_delete(collection, "release-groups", release_groups)
1450+
1451+
def add_works_to_collection(collection, works=[]):
1452+
"""Add works to a collection.
1453+
Collection and works should be identified by their MBIDs
1454+
"""
1455+
return _do_collection_put(collection, "works", works)
1456+
1457+
def remove_works_from_collection(collection, works=[]):
1458+
"""Remove works from a collection.
1459+
Collection and works should be identified by their MBIDs
1460+
"""
1461+
return _do_collection_delete(collection, "works", works)

0 commit comments

Comments
 (0)