Guest User

Untitled

a guest
Nov 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import requests
  2.  
  3. api_key = "0929f1144e531702a5563c887cbbade0"
  4.  
  5. ID = 0
  6. artists = {}
  7.  
  8. for i in range(1, 3):
  9.  
  10. artists_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&page=' + str(i) + '&api_key=' + api_key)
  11. artists_data = artists_response.json()
  12.  
  13. for artist in artists_data["topartists"]["artist"]:
  14.  
  15. name = artist["name"]
  16.  
  17. artists[ID] = {}
  18. artists[ID]['ID'] = ID
  19. artists[ID]['name'] = name
  20. ID += 1
  21.  
  22. "Artist": "U2", "ID": 175, "ArtistID": 10, "Title": Achtung Baby",
  23. "Image": "https://lastfm-img2.akamaized.net/i/u/174s/a482040d21924ddacd5fe75dedbb1ef2.png"},
  24. "URL": "https://www.last.fm/music/U2/Achtung+Baby"}, "487": {"Date": "2004-01-01"
  25.  
  26. "Artist": "U2", "ID": 176, "ArtistID": 10, "Description": "None", "Title": "Boy",
  27.  
  28.  
  29. ... and then the same for the other 3 albuns of the top5
  30.  
  31. albuns = {}
  32.  
  33. for i,v in artists.items():
  34. chosen = artists[i]['name'].replace(" ", "+")
  35. topalbuns_response = requests.get('http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&format=json&artist=' + chosen + '&api_key=' + api_key + '&limit=5')
  36. albuns_data = topalbuns_response.json()
  37.  
  38. for album in albuns_data['topalbums']['album']:
  39.  
  40. name = album["name"]
  41. url = album["url"]
  42.  
  43. albuns[ID] = {}
  44. albuns[ID]['ID'] = ID
  45. albuns[ID]['artist'] = artists[i]['name']
  46. albuns[ID]['artistID'] = artists[i]['ID']
  47. albuns[ID]['name'] = name
  48. albuns[ID]['url'] = url
  49.  
  50. for image in album['image']:
  51. if (image["size"] == "large"):
  52. if (image["size"] is not None):
  53. image = image["#text"]
  54. albuns[ID]['image'] = image
  55.  
  56. ID += 1
  57.  
  58. print(albuns)
Add Comment
Please, Sign In to add comment