Python Forum
can´t store the mbid - `KeyError: 'mbid'`. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: can´t store the mbid - `KeyError: 'mbid'`. (/thread-6502.html)



can´t store the mbid - `KeyError: 'mbid'`. - ozzyk - Nov-25-2017

I'm getting the tracks of some albums from lastfm. Then for each track I'm getting some information (title, number, artist, etc). Then, I want also to get the mbid of each track, but here I have an error:

KeyError: 'mbid'.

Get and store tracks information from albums is working fine:

 tracks = {}
    for i, v in albums.items():
        artist = albums[i]['artist'].replace(" ", "+")
        name = albums[i]['name'].replace(" ", "+")
        album_response_data = requests.get(
            'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&format=json&api_key=' + api_key + '&artist=' + artist + '&album=' + name)
        album_response = album_response_data.json()    
    
        if 'album' in album_response.keys():
            for album in album_response['album']['tracks']['track']:
                title = album['name']
      
                tracks[ID] = {} 
                tracks[ID]['trackID'] = ID
                tracks[ID]['title'] = title
                tracks[ID]['number'] = number
                tracks[ID]['artist'] = albums[i]['artist']
                tracks[ID]['album'] = albums[i]['name']
                tracks[ID]['albumID'] = albums[i]['ID']
                   
                ID += 1
Then getting and store the mbid of each track is where the issue is:

   for i, v in tracks.items():
        artist = tracks[i]['artist'].replace(" ", "+")
        title = tracks[i]['title'].replace(" ", "+")
    
    
        track_mbid =  requests.get('http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&api_key=' + api_key + '&artist=' + artist + '&track=' + title)
        track_mbid_response = track_mbid.json()
    
        #print(track_mbid_response.keys())
        #print(track_mbid_response)
    
        track_data = track_mbid_response["track"]
    
        #for track in track_data:
            #print(track)
        mbid = track_data['mbid']
    
    
        tracks[i]['mbid'] = mbid
    
    
    print(tracks)
print(track_mbid):

<Response [200]>

print(track_mbid_response) returns on this format

{"track":{"name":"Brown Sugar","mbid":"8d2338a2-4acc-4d99-b2a7-1151f428cab6","url":"https://www.last.fm/music/The+Rolling+Stones/_/B...


RE: can´t store the mbid - `KeyError: 'mbid'`. - Larz60+ - Nov-26-2017

Please show the fill traceback, verbatim.
It contains extremely valuable information for debugging.
Including line number where error is suspected to occur.


RE: can´t store the mbid - `KeyError: 'mbid'`. - ozzyk - Nov-26-2017

Sorry, but I didnt understand what is the fill traceback?

But the error is here:

mbid = track_data['mbid']


RE: can´t store the mbid - `KeyError: 'mbid'`. - Larz60+ - Nov-26-2017

sorry typo should have been 'full traceback', it's the entire error message.


RE: can´t store the mbid - `KeyError: 'mbid'`. - ozzyk - Nov-26-2017

Thanks, it appears:

Traceback (most recent call last):
File "C:/Users/Ozzy/PycharmProjects/getData/getData.py", line 110, in <module>
mbid = track_data['mbid']
KeyError: 'mbid'


RE: can´t store the mbid - `KeyError: 'mbid'`. - snippsat - Nov-26-2017

I will not merge thread now,as you have gotten answer.
You should not have started a new thread,as the task is same as here which also has more info.


RE: can´t store the mbid - `KeyError: 'mbid'`. - ozzyk - Nov-26-2017

Thanks and sorry, I didnt notice that the cause for the error, although it appears the same error information, was the same.


RE: can´t store the mbid - `KeyError: 'mbid'`. - heiner55 - Nov-26-2017

The error means: There is no key "mbid" in track_data.
You can not trust data from the internet.
You have to check each key for existence.


Sample:
#!/usr/bin/python3

dd = { "a": "aa" }

print(dd["a"])
print(dd["x"])   # <==  KeyError 'x'