Python Forum
error getting tracks from albums
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error getting tracks from albums
#7
Thanks for your answer. I delete some things that should not be important for the issue and add some comments. The code is basically divided in 3 parts: get information about artists from a specific country, then get top albums info of that artists and then get the tracks info of that albums. The issue seems to be in the 3rd part, on getting tracks info of the albums.


import requests

api_key = "b088cbedecd40b35dd89e90f55227ac2"
ID= 0

#Get artists from specific country

artists = {}
for i in range(2, 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"]

        url = artist["url"]
        #if ID > 3 continue
        artists[ID] = {}
        artists[ID]['ID'] = ID
        artists[ID]['name'] = name

        ID += 1


#Get TopAlbums info from above 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

        ID += 1

#Get tracks info of the above albums

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


    for album in album_response['album']['tracks']['track']:  

            title = album['name']

            number = album['@attr']['rank']
            duration = album['duration']

            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']
            tracks[ID]['duration'] = duration

            ID += 1
Reply


Messages In This Thread
error getting tracks from albums - by ozzyk - Nov-21-2017, 08:21 PM
RE: error getting tracks from albums - by ozzyk - Nov-22-2017, 12:07 AM
RE: error getting tracks from albums - by heiner55 - Nov-22-2017, 03:57 AM
RE: error getting tracks from albums - by snippsat - Nov-22-2017, 12:48 PM
RE: error getting tracks from albums - by ozzyk - Nov-22-2017, 01:44 PM
RE: error getting tracks from albums - by snippsat - Nov-22-2017, 02:07 PM
RE: error getting tracks from albums - by ozzyk - Nov-22-2017, 03:24 PM
RE: error getting tracks from albums - by snippsat - Nov-22-2017, 07:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mido MIDI-file; how to distinguis tracks? philipbergwerf 2 2,339 Dec-10-2021, 05:06 PM
Last Post: philipbergwerf
  If statement not separating Albums giddyhead 5 2,603 Feb-26-2021, 11:31 PM
Last Post: giddyhead
  Getting top albums from last.fm ozzyk 1 3,054 Nov-19-2017, 09:40 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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