import requests
import urllib
import matplotlib.pyplot as plt
from IPython.display import display, Image
from IPython.core.display import HTML 
import requests
%matplotlib inline

Shotgun Debugging

def display_image(url):
    response = urllib.request.urlopen(url).read()
    img = Image(response, width=200)
    display(img)
display_image("https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg")

jpeg

# Making a request to the spotify API
# https://api.spotify.com/v1/search?
# query=lil&
# offset=0&
# limit=20&
# type=artist
response = requests.get("https://api.spotify.com/v1/search?query=lil&offset=0&limit=20&type=artist")
data = response.json()
data.keys()
dict_keys(['artists'])
data['artists'].keys()
dict_keys(['href', 'limit', 'total', 'previous', 'items', 'next', 'offset'])
artists = data['artists']['items']
artists[0]
{'external_urls': {'spotify': 'https://open.spotify.com/artist/55Aa2cqylxrFIXC767Z865'},
 'followers': {'href': None, 'total': 2646569},
 'genres': ['dirty south rap', 'pop rap', 'southern hip hop', 'trap music'],
 'href': 'https://api.spotify.com/v1/artists/55Aa2cqylxrFIXC767Z865',
 'id': '55Aa2cqylxrFIXC767Z865',
 'images': [{'height': 1239,
   'url': 'https://i.scdn.co/image/cf012139c3b8681b46a66bae70558a8a336ab231',
   'width': 1000},
  {'height': 793,
   'url': 'https://i.scdn.co/image/fffd48d60e27901f6e9ce99423f045cb2b893944',
   'width': 640},
  {'height': 248,
   'url': 'https://i.scdn.co/image/bf03141629c202e94b206f1374a39326a9d8c6ca',
   'width': 200},
  {'height': 79,
   'url': 'https://i.scdn.co/image/521f99f2469883b8806a69a3a2487fdd983bd621',
   'width': 64}],
 'name': 'Lil Wayne',
 'popularity': 87,
 'type': 'artist',
 'uri': 'spotify:artist:55Aa2cqylxrFIXC767Z865'}
artists[0].keys()
dict_keys(['genres', 'href', 'images', 'type', 'external_urls', 'popularity', 'followers', 'id', 'uri', 'name'])
artists[0]['images']
[{'height': 1239,
  'url': 'https://i.scdn.co/image/cf012139c3b8681b46a66bae70558a8a336ab231',
  'width': 1000},
 {'height': 793,
  'url': 'https://i.scdn.co/image/fffd48d60e27901f6e9ce99423f045cb2b893944',
  'width': 640},
 {'height': 248,
  'url': 'https://i.scdn.co/image/bf03141629c202e94b206f1374a39326a9d8c6ca',
  'width': 200},
 {'height': 79,
  'url': 'https://i.scdn.co/image/521f99f2469883b8806a69a3a2487fdd983bd621',
  'width': 64}]
artists[0]['images'][2]
{'height': 248,
 'url': 'https://i.scdn.co/image/bf03141629c202e94b206f1374a39326a9d8c6ca',
 'width': 200}
image_url = artists[0]['images'][2]['url']
display_image(image_url)

jpeg

for artist in artists:
    display_image(artist['images'][0]['url'])

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

# display_image(artist['images'][0]['url'])
# display_artist_image(artist)    
def display_artist_image(artist):
    display_image(artist['images'][1]['url'])
display_artist_image(artists[10])

jpeg

display_artist_image(artists[15])

jpeg

display_image("https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg")

jpeg

for artist in artists:
    display_artist_image(artist)

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

jpeg

# https://api.spotify.com/v1/search
# ?query=lil
# &offset=1200
# &limit=20
# &type=artist
unpopular_response = requests.get("https://api.spotify.com/v1/search?query=lil&offset=1200&limit=20&type=artist")
unpopular_data = unpopular_response.json()
#unpopular_artists = unpopular_data['artists']['items']
#for artist in unpopular_artists:
#    display_artist_image(artist)
unpopular_artists = unpopular_data['artists']['items']
unpopular_artists[0]
{'external_urls': {'spotify': 'https://open.spotify.com/artist/4d5e17qy0VPTjsdfICnkO4'},
 'followers': {'href': None, 'total': 36},
 'genres': [],
 'href': 'https://api.spotify.com/v1/artists/4d5e17qy0VPTjsdfICnkO4',
 'id': '4d5e17qy0VPTjsdfICnkO4',
 'images': [{'height': 640,
   'url': 'https://i.scdn.co/image/2f460a72975d1f0701c56482aad3f820ac5635c6',
   'width': 640},
  {'height': 300,
   'url': 'https://i.scdn.co/image/6cabe0ef1ac8fe662b366871f908e4714ad9406a',
   'width': 300},
  {'height': 64,
   'url': 'https://i.scdn.co/image/984c525d4772cc4ca88bf4bfed215be361c6bda3',
   'width': 64}],
 'name': 'Lil Chuuuch',
 'popularity': 1,
 'type': 'artist',
 'uri': 'spotify:artist:4d5e17qy0VPTjsdfICnkO4'}
display_artist_image(unpopular_artists[0])

jpeg

for artist in unpopular_artists:
    display_artist_image(artist)

jpeg

---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

<ipython-input-35-d4d4b59d28a5> in <module>()
      1 for artist in unpopular_artists:
----> 2     display_artist_image(artist)


<ipython-input-34-8886dd6e3c6a> in display_artist_image(artist)
      1 def display_artist_image(artist):
----> 2     display_image(artist['images'][1]['url'])


IndexError: list index out of range