Skip to content

Commit b1548ed

Browse files
committed
Support the track/list_by_mbid API route
1 parent c41e1cc commit b1548ed

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

acoustid.py

+56
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ def _get_submission_status_url():
118118
return API_BASE_URL + 'submission_status'
119119

120120

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+
121126
# Compressed HTTP request bodies.
122127

123128
def _compress(data):
@@ -412,3 +417,54 @@ def get_submission_status(apikey, submission_id, timeout=None):
412417
'id': submission_id,
413418
}
414419
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

Comments
 (0)