Im getting the top artists from a sepcific country, albums of that artists and tracks of that albums from lastfm. Im getting the artists and albums without errors but in the tracks it appears:
Do you know where is the issue?
The track part where is the issue:
Full working example:
Quote:for album in album_response['album']['tracks']['track']: KeyError: 'album'
Do you know where is the issue?
The track part where is the issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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() print (album_response) for album in album_response[ 'album' ][ 'tracks' ][ 'track' ]: #print(album) title = album[ 'name' ] number = album[ '@attr' ][ 'rank' ] duration = album[ 'duration' ] tracks[ 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' ] tracks[ ID ][ 'duration' ] = duration ID + = 1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import requests api_key = "b088cbedecd40b35dd89e90f55227ac2" #generated for the example ID = 0 artists = {} for i in range ( 2 , 3 ): artists_response = requests.get( 'http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=portugal&format=json&page=' + str (i) + '&api_key=' + api_key) artists_data = artists_response.json() for artist in artists_data[ "topartists" ][ "artist" ]: name = artist[ "name" ] url = artist[ "url" ] if ID > 3 : continue artists[ ID ] = {} artists[ ID ][ 'ID' ] = ID artists[ ID ][ 'name' ] = name artists[ ID ][ 'url' ] = url ID + = 1 for i, v in artists.items(): chosen = artists[i][ 'name' ].replace( " " , "+" ) artist_response = requests.get( 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&format=json&artist=' + chosen + '&api_key=' + api_key) artist_data = artist_response.json() for artist in artist_data[ 'artist' ][ 'image' ]: if (artist[ "size" ] = = "large" ): if (artist[ "size" ] is not None ): image = artist[ "#text" ] artists[i][ 'image' ] = image artists[i][ 'tags' ] = [] for artist in artist_data[ 'artist' ][ 'tags' ][ "tag" ]: tags = artist[ "name" ] artists[i][ 'tags' ].append(tags) bio = artist_data[ 'artist' ][ 'bio' ][ 'summary' ] albums = {} for i, v in artists.items(): chosen = artists[i][ 'name' ].replace( " " , "+" ) topalbums_response = requests.get( 'http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&format=json&artist=' + chosen + '&api_key=' + api_key + '&limit=5' ) albums_data = topalbums_response.json() #print(albums_data) for album in albums_data[ 'topalbums' ][ 'album' ]: name = album[ "name" ] url = album[ "url" ] albums[ ID ] = {} albums[ ID ][ 'ID' ] = ID albums[ ID ][ 'artist' ] = artists[i][ 'name' ] albums[ ID ][ 'artistID' ] = artists[i][ 'ID' ] albums[ ID ][ 'name' ] = name albums[ ID ][ 'url' ] = url for image in album[ 'image' ]: if (image[ "size" ] = = "large" ): if (image[ "size" ] is not None ): image = image[ "#text" ] albums[ ID ][ 'image' ] = image ID + = 1 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() print (album_response) for album in album_response[ 'album' ][ 'tracks' ][ 'track' ]: #print(album) title = album[ 'name' ] number = album[ '@attr' ][ 'rank' ] duration = album[ 'duration' ] tracks[ 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' ] tracks[ ID ][ 'duration' ] = duration ID + = 1 |