Python Forum
Getting top albums from last.fm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting top albums from last.fm
#1
Im getting the top artists from a specific country from lastfm api. Im getting the name of each artist and store in the artists{} with success with:

import requests
    
    api_key = "0929f1144e531702a5563c887cbbade0" "generated for the working example
    
    ID = 0
    artists = {}
    
    for i in range(1, 3):
    
        artists_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&page=' + str(i) + '&api_key=' + api_key)
        artists_data = artists_response.json()
    
        for artist in artists_data["topartists"]["artist"]:
    
            name = artist["name"]
     
            artists[ID] = {}
            artists[ID]['ID'] = ID
            artists[ID]['name'] = name
            ID += 1
But now for each artist I want to get the top5 albums of the artist and store in the albums{} the artist name, the correspondent artist id from artists{}, the name the album, and the url of the album.

For example for each artist, I want to print 5 records, one for each album of the top5 albums of that artist like:


 "Artist": "U2", "ID": 175, "ArtistID": 10, "Title": Achtung    Baby",  
     "Image": "https://lastfm-img2.akamaized.net/i/u/174s/a482040d21924ddacd5fe75dedbb1ef2.png"},
     "URL": "https://www.last.fm/music/U2/Achtung+Baby"}, "487": {"Date": "2004-01-01"
    
     "Artist": "U2", "ID": 176, "ArtistID": 10, "Description": "None", "Title": "Boy", 


     ... and then the same for the other 3 albums of the top5
To get this I have the code below, but its not working correctly. The artistId appears always as "0" and it only appears the 5 albums of the first artist. Do you know where is the issue?

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()
    
        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
    
            print(albums)
Full example


    import requests
    
    api_key = "0929f1144e531702a5563c887cbbade0" "generated for the working example
    
    ID = 0
    artists = {}
    
    for i in range(1, 3):
    
        artists_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&page=' + str(i) + '&api_key=' + api_key)
        artists_data = artists_response.json()
    
        for artist in artists_data["topartists"]["artist"]:
    
            name = artist["name"]
     
            artists[ID] = {}
            artists[ID]['ID'] = ID
            artists[ID]['name'] = name
            ID += 1


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()
    
        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
    
            print(albums)
Reply
#2
When I run your program, I don't get: "The artistId appears always as 0"

#!/usr/bin/python3
import requests
import pprint

api_key = "0929f1144e531702a5563c887cbbade0"
ID = 0
artists = {}
for i in range(1, 3):
    artists_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&page=' + str(i) + '&api_key=' + api_key)
    artists_data = artists_response.json()
    for artist in artists_data["topartists"]["artist"]:
        name = artist["name"]
        if ID > 3: continue # 3 artists only for testing
        artists[ID] = {}
        artists[ID]['ID'] = ID
        artists[ID]['name'] = name
        ID += 1
pprint.pprint(artists)

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()

    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
pprint.pprint(albums)
#done
Output:
{0: {'ID': 0, 'name': 'David Bowie'},  1: {'ID': 1, 'name': 'Radiohead'},  2: {'ID': 2, 'name': 'Coldplay'},  3: {'ID': 3, 'name': 'Queen'}} {4: {'ID': 4,      'artist': 'David Bowie',      'artistID': 0,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/a59b52a89a2238773d25cc0f18a4c4b7.png',      'name': 'Best of Bowie',      'url': 'https://www.last.fm/music/David+Bowie/Best+of+Bowie'},  5: {'ID': 5,      'artist': 'David Bowie',      'artistID': 0,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/0a77b1de7d60538f2338480cb9a4c094.png',      'name': 'Hunky Dory',      'url': 'https://www.last.fm/music/David+Bowie/Hunky+Dory'},  6: {'ID': 6,      'artist': 'David Bowie',      'artistID': 0,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/6b33a72911784ee592b794d5b6681a2f.png',      'name': "Let's Dance",      'url': 'https://www.last.fm/music/David+Bowie/Let%27s+Dance'},  7: {'ID': 7,      'artist': 'David Bowie',      'artistID': 0,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/c6a02272b7a14d5b9cb1591b2cd504aa.png',      'name': 'The Best of David Bowie 1969-74',      'url': 'https://www.last.fm/music/David+Bowie/The+Best+of+David+Bowie+1969-74'},  8: {'ID': 8,      'artist': 'David Bowie',      'artistID': 0,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/2b712d28e5344f09bc672841d54bd6b7.png',      'name': 'Heroes',      'url': 'https://www.last.fm/music/David+Bowie/Heroes'},  9: {'ID': 9,      'artist': 'Radiohead',      'artistID': 1,      'image': 'https://lastfm-img2.akamaized.net/i/u/174s/62d26c6cb4ac4bdccb8f3a2a0fd55421.png',      'name': 'OK Computer',      'url': 'https://www.last.fm/music/Radiohead/OK+Computer'},  10: {'ID': 10,       'artist': 'Radiohead',       'artistID': 1,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/8d91b7dd13084606b99d756175917f7d.png',       'name': 'In Rainbows',       'url': 'https://www.last.fm/music/Radiohead/In+Rainbows'},  11: {'ID': 11,       'artist': 'Radiohead',       'artistID': 1,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/9421beaa754748d1b09236f57e3532c6.png',       'name': 'The Bends',       'url': 'https://www.last.fm/music/Radiohead/The+Bends'},  12: {'ID': 12,       'artist': 'Radiohead',       'artistID': 1,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/2ed343318c844d19cd897ec67fad11c4.png',       'name': 'Kid A',       'url': 'https://www.last.fm/music/Radiohead/Kid+A'},  13: {'ID': 13,       'artist': 'Radiohead',       'artistID': 1,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/f78e87f3ecef4a4b81ec7285ae9882c0.png',       'name': 'Pablo Honey',       'url': 'https://www.last.fm/music/Radiohead/Pablo+Honey'},  14: {'ID': 14,       'artist': 'Coldplay',       'artistID': 2,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/3d3d6d2b41544f42b8f750b6abdbd180.png',       'name': 'A Rush of Blood to the Head',       'url': 'https://www.last.fm/music/Coldplay/A+Rush+of+Blood+to+the+Head'},  15: {'ID': 15,       'artist': 'Coldplay',       'artistID': 2,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/7c62af235dc74f30cf3125906932b842.png',       'name': 'Parachutes',       'url': 'https://www.last.fm/music/Coldplay/Parachutes'},  16: {'ID': 16,       'artist': 'Coldplay',       'artistID': 2,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/c8933ae124dd49eb928ed9cf45d9fd5b.png',       'name': 'Viva la Vida or Death and All His Friends',       'url': 'https://www.last.fm/music/Coldplay/Viva+la+Vida+or+Death+and+All+His+Friends'},  17: {'ID': 17,       'artist': 'Coldplay',       'artistID': 2,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/a8699df2036a44d9b9ac5d3623930811.png',       'name': 'X&Y',       'url': 'https://www.last.fm/music/Coldplay/X&Y'},  18: {'ID': 18,       'artist': 'Coldplay',       'artistID': 2,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/54d3430791b444f5abca98f96dd3bd09.png',       'name': 'Mylo Xyloto',       'url': 'https://www.last.fm/music/Coldplay/Mylo+Xyloto'},  19: {'ID': 19,       'artist': 'Queen',       'artistID': 3,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/1c96bfd593ff1600aa0d0d2ae1361ebb.png',       'name': 'Greatest Hits',       'url': 'https://www.last.fm/music/Queen/Greatest+Hits'},  20: {'ID': 20,       'artist': 'Queen',       'artistID': 3,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/5acf44b6b52e40d5a345f5f39c4cb715.png',       'name': 'Greatest Hits II',       'url': 'https://www.last.fm/music/Queen/Greatest+Hits+II'},  21: {'ID': 21,       'artist': 'Queen',       'artistID': 3,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/43871b6b40f14f4d89360b68d9084802.png',       'name': 'A Night at the Opera',       'url': 'https://www.last.fm/music/Queen/A+Night+at+the+Opera'},  22: {'ID': 22,       'artist': 'Queen',       'artistID': 3,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/6682a933a9814d48bf1307e7a99cd655.png',       'name': 'Jazz',       'url': 'https://www.last.fm/music/Queen/Jazz'},  23: {'ID': 23,       'artist': 'Queen',       'artistID': 3,       'image': 'https://lastfm-img2.akamaized.net/i/u/174s/59d6321637014d6fa76e2eed82991c57.png',       'name': 'Greatest Hits III',       'url': 'https://www.last.fm/music/Queen/Greatest+Hits+III'}}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If statement not separating Albums giddyhead 5 2,534 Feb-26-2021, 11:31 PM
Last Post: giddyhead
  error getting tracks from albums ozzyk 7 4,569 Nov-22-2017, 07:33 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020