wolffd@0: #!/usr/bin/env python wolffd@0: """A simple script that searches for a release in the MusicBrainz wolffd@0: database and prints out a few details about the first matching release. wolffd@0: wolffd@0: $ ./releasesearch.py "the beatles" revolver wolffd@0: Revolver, by The Beatles wolffd@0: Released 1966-08-08 (Official) wolffd@0: MusicBrainz ID: b4b04cbf-118a-3944-9545-38a0a88ff1a2 wolffd@0: """ wolffd@0: from __future__ import print_function wolffd@0: from __future__ import unicode_literals wolffd@0: import musicbrainzngs wolffd@0: import sys wolffd@0: wolffd@0: musicbrainzngs.set_useragent( wolffd@0: "python-musicbrainz-ngs-example", wolffd@0: "0.1", wolffd@0: "https://github.com/alastair/python-musicbrainz-ngs/", wolffd@0: ) wolffd@0: wolffd@0: def show_release_details(rel): wolffd@0: """Print some details about a release dictionary to stdout. wolffd@0: """ wolffd@0: # "artist-credit-phrase" is a flat string of the credited artists wolffd@0: # joined with " + " or whatever is given by the server. wolffd@0: # You can also work with the "artist-credit" list manually. wolffd@0: print("{}, by {}".format(rel['title'], rel["artist-credit-phrase"])) wolffd@0: print("{}".format(rel['id'])) wolffd@0: wolffd@0: wolffd@0: wolffd@0: def fail(message): wolffd@0: """Print a message to stderr and then exit with an error status. wolffd@0: """ wolffd@0: print(message, file=sys.stderr) wolffd@0: sys.exit(1) wolffd@0: wolffd@0: if __name__ == '__main__': wolffd@0: args = sys.argv[1:] wolffd@0: if len(args) != 2: wolffd@0: fail("usage: {} ARTIST ALBUM".format(sys.argv[0])) wolffd@0: artist, album = args wolffd@0: wolffd@0: # Keyword arguments to the "search_*" functions limit keywords to wolffd@0: # specific fields. The "limit" keyword argument is special (like as wolffd@0: # "offset", not shown here) and specifies the number of results to wolffd@0: # return. wolffd@0: result = musicbrainzngs.search_releases(artist=artist, release=album, wolffd@0: limit=1) wolffd@0: # On success, result is a dictionary with a single key: wolffd@0: # "release-list", which is a list of dictionaries. wolffd@0: if not result['release-list']: wolffd@0: fail("no release found") wolffd@0: for (idx, release) in enumerate(result['release-list']): wolffd@0: # print("match #{}:".format(idx+1)) wolffd@0: show_release_details(release)