wolffd@0
|
1 #!/usr/bin/env python
|
wolffd@0
|
2 """A simple script that searches for a release in the MusicBrainz
|
wolffd@0
|
3 database and prints out a few details about the first matching release.
|
wolffd@0
|
4
|
wolffd@0
|
5 $ ./releasesearch.py "the beatles" revolver
|
wolffd@0
|
6 Revolver, by The Beatles
|
wolffd@0
|
7 Released 1966-08-08 (Official)
|
wolffd@0
|
8 MusicBrainz ID: b4b04cbf-118a-3944-9545-38a0a88ff1a2
|
wolffd@0
|
9 """
|
wolffd@0
|
10 from __future__ import print_function
|
wolffd@0
|
11 from __future__ import unicode_literals
|
wolffd@0
|
12 import musicbrainzngs
|
wolffd@0
|
13 import sys
|
wolffd@0
|
14
|
wolffd@0
|
15 musicbrainzngs.set_useragent(
|
wolffd@0
|
16 "python-musicbrainz-ngs-example",
|
wolffd@0
|
17 "0.1",
|
wolffd@0
|
18 "https://github.com/alastair/python-musicbrainz-ngs/",
|
wolffd@0
|
19 )
|
wolffd@0
|
20
|
wolffd@0
|
21 def show_release_details(rel):
|
wolffd@0
|
22 """Print some details about a release dictionary to stdout.
|
wolffd@0
|
23 """
|
wolffd@0
|
24 # "artist-credit-phrase" is a flat string of the credited artists
|
wolffd@0
|
25 # joined with " + " or whatever is given by the server.
|
wolffd@0
|
26 # You can also work with the "artist-credit" list manually.
|
wolffd@0
|
27 print("{}, by {}".format(rel['title'], rel["artist-credit-phrase"]))
|
wolffd@0
|
28 print("{}".format(rel['id']))
|
wolffd@0
|
29
|
wolffd@0
|
30
|
wolffd@0
|
31
|
wolffd@0
|
32 def fail(message):
|
wolffd@0
|
33 """Print a message to stderr and then exit with an error status.
|
wolffd@0
|
34 """
|
wolffd@0
|
35 print(message, file=sys.stderr)
|
wolffd@0
|
36 sys.exit(1)
|
wolffd@0
|
37
|
wolffd@0
|
38 if __name__ == '__main__':
|
wolffd@0
|
39 args = sys.argv[1:]
|
wolffd@0
|
40 if len(args) != 2:
|
wolffd@0
|
41 fail("usage: {} ARTIST ALBUM".format(sys.argv[0]))
|
wolffd@0
|
42 artist, album = args
|
wolffd@0
|
43
|
wolffd@0
|
44 # Keyword arguments to the "search_*" functions limit keywords to
|
wolffd@0
|
45 # specific fields. The "limit" keyword argument is special (like as
|
wolffd@0
|
46 # "offset", not shown here) and specifies the number of results to
|
wolffd@0
|
47 # return.
|
wolffd@0
|
48 result = musicbrainzngs.search_releases(artist=artist, release=album,
|
wolffd@0
|
49 limit=1)
|
wolffd@0
|
50 # On success, result is a dictionary with a single key:
|
wolffd@0
|
51 # "release-list", which is a list of dictionaries.
|
wolffd@0
|
52 if not result['release-list']:
|
wolffd@0
|
53 fail("no release found")
|
wolffd@0
|
54 for (idx, release) in enumerate(result['release-list']):
|
wolffd@0
|
55 # print("match #{}:".format(idx+1))
|
wolffd@0
|
56 show_release_details(release)
|