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
#%magic
%pdb on
Automatic pdb calling has been turned ON

Debugging with pdb

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

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', 'previous', 'items', 'limit', 'offset', 'total', 'next'])
artists = data['artists']['items']
artists[0].keys()
dict_keys(['external_urls', 'href', 'type', 'name', 'images', 'followers', 'uri', 'genres', 'id', 'popularity'])
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}]
def display_artist_image(artist):
    display_image(artist['images'][0]['url'])
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)

jpeg

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

IndexError                                Traceback (most recent call last)

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


<ipython-input-18-08aea5ed42f7> in display_artist_image(artist)
      1 def display_artist_image(artist):
----> 2     display_image(artist['images'][0]['url'])


IndexError: list index out of range


> <ipython-input-18-08aea5ed42f7>(2)display_artist_image()
      1 def display_artist_image(artist):
----> 2     display_image(artist['images'][0]['url'])

ipdb> dir()
['artist']
ipdb> artist
{'external_urls': {'spotify': 'https://open.spotify.com/artist/16yXrzdQsBFRalQjN7Wk6t'}, 'href': 'https://api.spotify.com/v1/artists/16yXrzdQsBFRalQjN7Wk6t', 'type': 'artist', 'name': 'Lil Chewy', 'images': [], 'followers': {'href': None, 'total': 0}, 'uri': 'spotify:artist:16yXrzdQsBFRalQjN7Wk6t', 'genres': [], 'id': '16yXrzdQsBFRalQjN7Wk6t', 'popularity': 0}
ipdb> artist['images'][0]['url']
*** IndexError: list index out of range
ipdb> artist['images'][0]
*** IndexError: list index out of range
ipdb> artist['images']
[]
ipdb> artist['name']
'Lil Chewy'
ipdb> continue
my_int = 55
my_str = "banjos"

#my_int + my_str