Skip to content

Instantly share code, notes, and snippets.

@richbs
Last active September 11, 2023 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richbs/cf82ce79200ac04148f155e4d26854a0 to your computer and use it in GitHub Desktop.
Save richbs/cf82ce79200ac04148f155e4d26854a0 to your computer and use it in GitHub Desktop.
Pull release and track info from Musicbrainz
import sys
import os
import requests
import json
import pprint
print(sys.argv)
if len(sys.argv) > 1:
release_id = sys.argv[1]
else:
release_id = 'b9a51ddb-7f4f-427e-a1f8-cb153101937d'
release_id = '8156c33f-e46c-4348-a41b-40bea6d7793e'
release_id = '723f4d97-d88a-4d76-a323-f4c951e147fb' # Kind of Blue
#release_id = '92b01866-1815-4d8e-91b3-700d11719bbb' # berliner centenary
def uniql(l):
outl = []
for i in l:
if i not in outl:
outl.append(i)
return outl
def unsort(st):
l = st.split(', ')
l.reverse()
return " ".join(l)
recs_to_get = 100
offset = 0
recplaces = {}
recplayers = {}
while recs_to_get >= 100:
u = 'https://musicbrainz.org/ws/2/recording?release='+release_id + \
'&inc=artist-credits+area-rels+place-rels+artist-rels&fmt=json&limit=100&offset=%d' % (
offset)
print(u)
r = requests.get(u)
recordings = r.json()
recs_to_get = len(recordings['recordings'])
print(recs_to_get, 'recordings')
for rec in recordings['recordings']:
places = []
players = []
for rel in rec['relations']:
if 'place' in rel:
#records[rec['id']] = "%s %s %s (%s) [%s-%s]" % (rec['title'], rel['type'],rel['place']['name'],rel['place']['area']['name'], rel['begin'], rel['end'])
places.append("%s %s (%s) [%s-%s]" % (rel['type'], rel['place']['name'],rel['place']['area']['name'], rel['begin'], rel['end']))
elif rel['type'] == 'instrument':
ins = ", ".join(rel['attributes'])
art = unsort(rel['artist']['sort-name'])
players.append("%s [%s]" % (art, ins))
#venues.append(rec['title'])
elif rel['type'] == 'work':
print(rel)
pass #print(rel)
recplaces[rec['id']] = list(dict.fromkeys(places))
recplayers[rec['id']] = list(dict.fromkeys(players))
offset += 100
print('places', len(recplaces))
u = 'https://musicbrainz.org/ws/2/release/'+release_id + \
'?inc=recordings%2Bartist-credits%2Bwork-rels%2Bwork-level-rels%2Brecording-level-rels%2Brelease-group-level-rels%2Bartist-rels&fmt=json'
print(u)
r = requests.get(u)
release = r.json()
all_artists = [unsort(a['artist']['sort-name']) +
' (' + a['artist']['disambiguation'] + ')' for a in release['artist-credit']]
album_artists = ", ".join([unsort(a['artist']['sort-name']) + ' (' + a['artist']
['disambiguation'] + ')' for a in release['artist-credit']])
for m in release['media']:
all_artists = []
all_places = []
print(m['position'])
for t in m['tracks']:
ttitle = t['title']
wtitle = ''
print(t)
for rel in t['recording']['relations']:
if 'place' in rel:
#records[rec['id']] = "%s %s %s (%s) [%s-%s]" % (rec['title'], rel['type'],rel['place']['name'],rel['place']['area']['name'], rel['begin'], rel['end'])
places.append("%s %s (%s) [%s-%s]" % (rel['type'], rel['place']['name'],rel['place']['area']['name'], rel['begin'], rel['end']))
elif rel['type'] == 'instrument':
ins = ", ".join(rel['attributes'])
art = unsort(rel['artist']['sort-name'])
players.append("%s [%s]" % (art, ins))
#venues.append(rec['title'])
elif rel['target-type'] == 'work':
wtitle = '['+rel['work']['title']+']'
pass #print(rel)
print(t['number'], ttitle,wtitle )
recording_artists = [unsort(a['artist']['sort-name']) + ' (' + a['artist']
['disambiguation'] + ')' for a in t['recording']['artist-credit']] + [a['name'] for a in t['artist-credit']]
composers = [unsort(a['artist']['sort-name']) + ' (' + a['artist']
['disambiguation'] + ')' for a in t['artist-credit']]
all_artists = all_artists + composers + \
recording_artists + recplayers[t['recording']['id']]
print_track_artists = [unsort(a['artist']['sort-name']) for a in t['recording']
['artist-credit']] + [unsort(a['artist']['sort-name']) for a in t['artist-credit']]
print(", ".join(print_track_artists))
if t['recording']['id'] in recplaces and recplaces[t['recording']['id']]:
rels = recplaces[t['recording']['id']]
print(rels[0])
all_places += rels
print("\n" + ", ".join(uniql(all_places)).replace(
" ()", ''))
print(", ".join(uniql(all_artists)).replace(
" ()", '') + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment