1

I am new to python and am trying to fetch JSON data from musicbrainz using urllib and am able to parse some data. However, for some artists, certain fields / keys in the JSON data do not come as they might not be required. I am not sure how to filter the fetched JSON data based on a further filter criteria.

In the below example, I will need to filter the fetched JSON data only for the ones that have release-list>>release>>release-group>>type as 'Single'. The JSON fetched can have iterations up to 50 at times, but I only feed to further filter this to just the ones that were category 'Single'. Please let me know how one can go about. Thanks!

Sample JSON:

{
    "created": "2012-12-27T13:22:55.834Z",
    "recording-list": {
        "count": 3,
        "offset": 0,
        "recording": [{
            "score": "100",
            "title": "Stronger",
            "artist-credit": {
                "name-credit": [{
                    "artist": {
                        "name": "Britney Spears",
                        "sort-name": "Spears, Britney"
                    }
                }]
            },
            "release-list": {
                "release": [{
                    "id": "13c5511f-1f99-4ffe-97d5-562c05e9d8d5",
                    "title": "Hit Hammer 2001 (disc 1)",
                    "status": "Official",
                    "artist-credit": {
                        "name-credit": [{
                            "artist": {
                                "id": "89ad4ac3-39f7-470e-963a-56509c546377",
                                "name": "Various Artists"
                            }
                        }]
                    },
                      "release-group": {
                        "id": "6c4c2cc3-3d8e-3a19-9d46-da076c34b6e9",
                        "type": "Compilation",
                        "primary-type": "Album",
                        "secondary-type-list": {
                            "secondary-type": ["Compilation"]
                        }
                    },
                    "medium-list": {
                        "track-count": 20,
                        "medium": [{
                            "position": 1,
                            "track-list": {
                                "count": 20,
                                "offset": 0,
                                "track": [{
                                    "number": "1",
                                    "title": "Stronger",
                                    "length": 203266
                                }]
                            }
                        }]
                    }
                }]
            }
        }, {
            "id": "feb9acbf-1d3d-4395-9512-bfbdcfa72eb9",
            "score": "100",
            "title": "Stronger",
            "artist-credit": {
                "name-credit": [{
                    "joinphrase": "",
                    "artist": {
                        "name": "Britney Spears",
                        "sort-name": "Spears, Britney"
                    }
                }]
            },
            "release-list": {
                "release": [{
                    "id": "45e2a271-2f6b-4029-b11e-b6d94d169f9a",
                    "title": "Stronger: The Remixes",
                    "status": "Official",
                    "release-group": {
                        "id": "4d018ba8-f05e-4817-8c70-34307161a0fc",
                        "type": "Single",
                        "primary-type": "Single"
                    },
                    "date": "2000-12-12",
                    "country": "US",
                    "medium-list": {
                        "track-count": 6,
                        "medium": [{
                            "position": 1,
                            "format": "CD",
                            "track-list": {
                                "count": 6,
                                "offset": 0,
                                "track": [{
                                    "number": "1",
                                    "title": "Stronger",
                                    "length": 203000
                                }]
                            }
                        }]
                    }
                }]
            },
            "puid-list": {
                "puid": [{
                    "id": "28550845-c68a-314d-90c1-010dff730f4a"
                }]
            }
        }]
    }
}

Python Code:

def get_mbid(artist, song):
    artist=urllib.quote_plus(artist)
    song=urllib.quote_plus(song)
    recording_url = 'http://search.musicbrainz.org/ws/2/recording/?&fmt=json&query=artist:"'+artist+'"%20AND%20recording:"'+song+'"'
    search_results = urllib.urlopen(recording_url)

    json = simplejson.loads(search_results.read())
    search_results.close()
    if json['recording-list']['count'] == 0:
        return get_mbid_artist(artist)
    else:
        recordings = json['recording-list']['recording']
        for recording in recordings:
            mbid = recording['artist-credit']['name-credit'][0]['artist']['id']
            print mbid
Prem Minister
  • 407
  • 2
  • 10
  • 20
  • You want to filter on `primary-type` really; `type` is deprecated and is being replaced by `primary-` and `secondary-type`. Why not add the filter to the query URL? Adding `&primary-type=Single` should do it. – Martijn Pieters Dec 29 '12 at 15:38
  • Hi Martijn, Thank you for the suggestion but it doest seem to work, the JOSN dump still looks the same even with me passing the primary-type in the URL. http://musicbrainz.org/ws/2/recording?&fmt=json&query=artist%3A"Britney+Spears"+AND+recording%3A"Stronger"&primary-type=Single – Prem Minister Dec 29 '12 at 15:45
  • Right, I suspect it is returning any recording for which there is *at least* a Single release. What kind of filtering do you need? You can always loop over the releases and only process those that are singles, right? – Martijn Pieters Dec 29 '12 at 15:48
  • Is there a reason why you don't use a pre-made library for the web service? ([python-musicbrainz-ngs](https://github.com/alastair/python-musicbrainz-ngs)). It doesn't help with this problem, but it might be of help in other aspects. – JonnyJD Dec 30 '12 at 02:20

1 Answers1

0

http://musicbrainz.org/ws/2/recording?&query=artist%3A%22Britney+Spears%22+AND+recording%3A%22Stronger%22+AND+primarytype%3ASingle

will give you recordings with the primarytype Single.

That is without encoding:

artist:"Britney Spears" AND recording:"Stronger" AND primarytype:Single

See also: Web Service Search. You can add fmt=json again, of course. I just removed that part because it is easier to read XML in a browser.


I also want to note, that there is a python library for the current version of the XML Web Service.

JonnyJD
  • 2,593
  • 1
  • 28
  • 44