@@ -0,0 +1,2480 @@
+# To experiment with this code freely you will have to run this code locally.%0A# Take a look at the main() function for an example of how to use the code.%0A# We have provided example json output in the other code editor tabs for you to%0A# look at, but you will not be able to run any queries through our UI.%0Aimport json%0Aimport requests%0A%0A%0ABASE_URL = %22http://musicbrainz.org/ws/2/%22%0AARTIST_URL = BASE_URL + %22artist/%22%0A%0A# query parameters are given to the requests.get function as a dictionary; this%0A# variable contains some starter parameters.%0Aquery_type = %7B  %22simple%22: %7B%7D,%0A                %22atr%22: %7B%22inc%22: %22aliases+tags+ratings%22%7D,%0A                %22aliases%22: %7B%22inc%22: %22aliases%22%7D,%0A                %22releases%22: %7B%22inc%22: %22releases%22%7D%7D%0A%0A%0Adef query_site(url, params, uid=%22%22, fmt=%22json%22):%0A    # This is the main function for making queries to the musicbrainz API.%0A    # A json document should be returned by the query.%0A    params%5B%22fmt%22%5D = fmt%0A    r = requests.get(url + uid, params=params)%0A    print %22requesting%22, r.url%0A%0A    if r.status_code == requests.codes.ok:%0A        return r.json()%0A    else:%0A        r.raise_for_status()%0A%0A%0Adef query_by_name(url, params, name):%0A    # This adds an artist name to the query parameters before making%0A    # an API call to the function above.%0A    params%5B%22query%22%5D = %22artist:%22 + name%0A    return query_site(url, params)%0A%0A%0Adef pretty_print(data, indent=4):%0A    # After we get our output, we can format it to be more readable%0A    # by using this function.%0A    if type(data) == dict:%0A        print json.dumps(data, indent=indent, sort_keys=True)%0A    else:%0A        print data%0A%0A%0Adef main():%0A    '''%0A    Modify the function calls and indexing below to answer the questions on%0A    the next quiz. HINT: Note how the output we get from the site is a%0A    multi-level JSON document, so try making print statements to step through%0A    the structure one level at a time or copy the output to a separate output%0A    file.%0A    '''%0A    results = query_by_name(ARTIST_URL, query_type%5B%22simple%22%5D, %22Nirvana%22)%0A    pretty_print(results)%0A%0A    artist_id = results%5B%22artists%22%5D%5B1%5D%5B%22id%22%5D%0A    print %22%5CnARTIST:%22%0A    pretty_print(results%5B%22artists%22%5D%5B1%5D)%0A%0A    artist_data = query_site(ARTIST_URL, query_type%5B%22releases%22%5D, artist_id)%0A    releases = artist_data%5B%22releases%22%5D%0A    print %22%5CnONE RELEASE:%22%0A    pretty_print(releases%5B0%5D, indent=2)%0A    release_titles = %5Br%5B%22title%22%5D for r in releases%5D%0A%0A    print %22%5CnALL TITLES:%22%0A    for t in release_titles:%0A        print t%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add scripts to process json files