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" )
# 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 ()
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/v 1 /artists/ 55 Aa 2 cqylxrFIXC 767 Z 865 ',
'id': ' 55 Aa 2 cqylxrFIXC 767 Z 865 ',
'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: 55 Aa 2 cqylxrFIXC 767 Z 865 '}
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 )
for artist in artists :
display_image ( artist [ 'images' ][ 0 ][ 'url' ])
# 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 ])
display_artist_image ( artists [ 15 ])
display_image ( "https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg" )
for artist in artists :
display_artist_image ( artist )
# 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/v 1 /artists/ 4 d 5 e 17 qy 0 VPTjsdfICnkO 4 ',
'id': ' 4 d 5 e 17 qy 0 VPTjsdfICnkO 4 ',
'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: 4 d 5 e 17 qy 0 VPTjsdfICnkO 4 '}
display_artist_image ( unpopular_artists [ 0 ])
for artist in unpopular_artists :
display_artist_image ( artist )
---------------------------------------------------------------------------
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