Question

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

Was it helpful?

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"])

OTHER TIPS

This is an example script for getting the tracklist for an album using musicbrainzngs

#!/usr/bin/python3
from __future__ import print_function
from __future__ import unicode_literals
import musicbrainzngs
import sys

musicbrainzngs.set_useragent(
    "python-musicbrainzngs-example",
    "0.1",
    "https://github.com/alastair/python-musicbrainzngs/",
)

def get_tracklist(artist, album):
    result = musicbrainzngs.search_releases(artist=artist, release=album, limit=1)
    id = result["release-list"][0]["id"]
    
    #### get tracklist
    new_result = musicbrainzngs.get_release_by_id(id, includes=["recordings"])
    t = (new_result["release"]["medium-list"][0]["track-list"])
    for x in range(len(t)):
        line = (t[x])
        print(f'{line["number"]}. {line["recording"]["title"]}')

if __name__ == '__main__':
    ### get first release
    if len(sys.argv) > 1:
        artist, album = [sys.argv[1], sys.argv[2]]
        get_tracklist(artist, album)
    else:
        artist = input("Artist: ")
        album = input("Album: ")
        if not artist == "" and not album == "":
            get_tracklist(artist, album)
        else:
            print("Artist or Album missing")
    

Usage:

python3 album_get_tracklist.py "rolling stones" "beggars banquet"

or

python3 album_get_tracklist.py

it will ask for Artist and Album

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top