@@ -118,6 +118,11 @@ def _get_submission_status_url():
118
118
return API_BASE_URL + 'submission_status'
119
119
120
120
121
+ def _get_track_by_mbid_url ():
122
+ """Get the URL of the track-by-MBID API endpoint."""
123
+ return API_BASE_URL + 'track/list_by_mbid'
124
+
125
+
121
126
# Compressed HTTP request bodies.
122
127
123
128
def _compress (data ):
@@ -412,3 +417,54 @@ def get_submission_status(apikey, submission_id, timeout=None):
412
417
'id' : submission_id ,
413
418
}
414
419
return _api_request (_get_submission_status_url (), params , timeout )
420
+
421
+
422
+ def track_by_mbid (release_ids , disabled = False , timeout = None ):
423
+ """Get AcoustID track id(s) corresponding to the given MusicBrainz
424
+ release id(s).
425
+ If ``release_ids`` is a str, a list of strs (possibly empty) is returned.
426
+ If ``release_ids`` is a list of strs, a dict mapping each str in that list
427
+ to a (possibly empty) list of strs is returned.
428
+ If ``disabled`` is True, those lists of str are instead pairs of lists of
429
+ strs, the first containing the enabled AcoustID track ids and the second the
430
+ disabled track ids."""
431
+
432
+ # Avoid isinstance(release_ids, list) in case the caller wants to pass some
433
+ # other sequence. We let requests convert the sequence to a repeated param.
434
+ batch = not isinstance (release_ids , str )
435
+ params = {
436
+ 'format' : 'json' ,
437
+ 'mbid' : release_ids ,
438
+ 'disabled' : '1' if disabled else '0' ,
439
+ 'batch' : '1' if batch else '0' ,
440
+ # this route doesn't require an API key
441
+ }
442
+
443
+ response = _api_request (_get_track_by_mbid_url (), params , timeout )
444
+ # Copied from submit, above.
445
+ if response .get ('status' ) != 'ok' :
446
+ try :
447
+ code = response ['error' ]['code' ]
448
+ message = response ['error' ]['message' ]
449
+ except KeyError :
450
+ raise WebServiceError ("response: {0}" .format (response ))
451
+ raise WebServiceError ("error {0}: {1}" .format (code , message ))
452
+
453
+ # When disabled is true, we defensively check for disabled: false even
454
+ # though AcoustID currently omits that attribute for enabled MBIDs.
455
+ if batch :
456
+ mbids = response ['mbids' ]
457
+ if disabled :
458
+ return {m ['mbid' ]:
459
+ ([x ['id' ] for x in m ['tracks' ] if 'disabled' not in x or not x ['disabled' ]],
460
+ [x ['id' ] for x in m ['tracks' ] if 'disabled' in x and x ['disabled' ]])
461
+ for m in mbids }
462
+ else :
463
+ return {m ['mbid' ]: [x ['id' ] for x in m ['tracks' ]] for m in mbids }
464
+ else :
465
+ tracks = response ['tracks' ]
466
+ if disabled :
467
+ return ([x ['id' ] for x in tracks if 'disabled' not in x or not x ['disabled' ]],
468
+ [x ['id' ] for x in tracks if 'disabled' in x and x ['disabled' ]])
469
+ else :
470
+ return [x ['id' ] for x in tracks ]
0 commit comments