Search code examples
pythonmusicbrainz

Getting a tracklist from MusicBrainz


I am trying to learn Python, and thought I'd learn by writing something I'd actually use. SO I'm trying to write a little script to rip some music CDs.

I am using the musicbrainzngs package. I would like to get the tracklist of the CD. My code currently:

#! /usr/bin/env python

import argparse
import musicbrainzngs
import discid

musicbrainzngs.set_useragent("Audacious", "0.1", "https://github.com/jonnybarnes/audacious")

parser = argparse.ArgumentParser()
parser.add_argument("--cdrom", help="provide the source of the cd", default="/dev/cdrom")
args = parser.parse_args()

device = args.cdrom

print("device: %s" % device)
disc = discid.read(device)
print("id: %s" % disc.id)

try:
    result = musicbrainzngs.get_releases_by_discid(disc.id, includes=["artists"])
except musicbrainzngs.ResponseError:
    print("disc not found or bad response")
else:
    if result.get("disc"):
        print("artist:\t%s" %
            result["disc"]["release-list"][0]["artist-credit-phrase"])
        print("title:\t%s" % result["disc"]["release-list"][0]["title"])
    elif result.get("cdstub"):
        print("artist:\t" % result["cdstub"]["artist"])
        print("title:\t" % result["cdstub"]["title"])

How can I get the tracklist, looking at the full results returned there is a track-list property but regardless of what CD I try the result is always empty


Solution

  • Getting releases by discid is a lookup and its "'inc=' arguments supported are identical to a lookup request for a release" which are listed earlier on that page. To get a non-empty tracklist you simply need to add the "recordings" include:

    result = musicbrainzngs.get_releases_by_discid(disc.id, includes=["artists", "recordings"])