From f73dd22ef1d9cae7f97884aa379852aa9eae6888 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Fri, 31 Dec 2021 13:16:22 +0100 Subject: [PATCH] Add "recording" methods for collections Collections can consist of recordings too and this adds the ability to manipulate the recordings in those. --- examples/collection.py | 14 +++++++++++--- musicbrainzngs/musicbrainz.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/examples/collection.py b/examples/collection.py index 5aaf1eef..5fcab532 100755 --- a/examples/collection.py +++ b/examples/collection.py @@ -163,17 +163,25 @@ def show_releases(collection): musicbrainzngs.auth(username, password) if args: - # Actions for a specific collction. + # Actions for a specific collection. collection_id = args[0] if options.add: - if option.type == "release": + if options.type == "recording": + musicbrainzngs.add_recordings_to_collection( + collection_id, [options.add] + ) + elif options.type == "release": musicbrainzngs.add_releases_to_collection( collection_id, [options.add] ) else: sys.exit("only release collections can be modified ATM") elif options.remove: - if option.type == "release": + if options.type == "recording": + musicbrainzngs.remove_recordings_from_collection( + collection_id, [options.remove] + ) + elif options.type == "release": musicbrainzngs.remove_releases_from_collection( collection_id, [options.remove] ) diff --git a/musicbrainzngs/musicbrainz.py b/musicbrainzngs/musicbrainz.py index ae1d9a48..68247980 100644 --- a/musicbrainzngs/musicbrainz.py +++ b/musicbrainzngs/musicbrainz.py @@ -1315,3 +1315,29 @@ def remove_releases_from_collection(collection, releases=[]): """ releaselist = ";".join(releases) return _do_mb_delete("collection/%s/releases/%s" % (collection, releaselist)) + +def add_recordings_to_collection(collection_mbid, recordings): + """ + :param collection_mbid: Musicbrainz ID of the collection + :param recordings: Musicbrainz IDs of the recordings to add + """ + + # XXX: Maximum URI length of 16kb means we should only allow ~400 recordings + recording_list = ";".join(recordings) + return _do_mb_put( + "collection/%s/recordings/%s" % (collection_mbid, recording_list) + ) + + +def remove_recordings_from_collection(collection_mbid, recordings): + """ + :param collection_mbid: Musicbrainz ID of the collection + :param recordings: Musicbrainz IDs of the recordings to remove + """ + + # XXX: Maximum URI length of 16kb means we should only allow ~400 recordings + recording_list = ";".join(recordings) + return _do_mb_delete( + "collection/%s/recordings/%s" % (collection_mbid, recording_list) + ) +