grade = 8/8
1) With "Lil Wayne" and "Lil Kim" there are a lot of "Lil" musicians. Do a search and print a list of 50 that are playable in the USA (or the country of your choice), along with their popularity score.
In [1]:
import requests
lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&type=track&market=US&limit=50')
#print(response.text)
In [2]:
lil_data = lil_response.json()
In [3]:
lil_data.keys()
#print(lil_data)
Out[3]:
In [4]:
lil_data['artists'].keys()
Out[4]:
In [8]:
lil_artists = lil_data['artists']['items']
for artist in lil_artists:
print("Artist Name:",artist['name'],"|| Artist Popularity Score:",artist['popularity'],"|| Artist ID for reference,later",artist['id'])
2) What genres are most represented in the search results? Edit your previous printout to also display a list of their genres in the format "GENRE_1, GENRE_2, GENRE_3". If there are no genres, print "No genres listed". Tip: "how to join a list Python" might be a helpful search
In [6]:
for artist in lil_artists:
print("Artist Name:",artist['name'])
if not artist['genres']==[]:
print("Artist Genres:",",".join(artist['genres']))
else:
print("No genres listed.")
3) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating. Is it the same artist who has the largest number of followers?
In [24]:
artistpopularity=0
for artist in lil_artists:
if not artist['name']=='Lil Wayne':
if artist['popularity'] > artistpopularity:
artistpopularity=artist['popularity']
artistname=artist['name']
print("Artist Name:",artistname, "|| Popularity Score:",artistpopularity)
In [9]:
artistfollowers=0
for artist in lil_artists:
if not artist['name']=='Lil Wayne':
if artist['followers']['total'] > artistfollowers:
artistfollowers=artist['followers']['total']
artistname=artist['name']
print(artistfollowers)
print(artistname)
4) Print a list of Lil's that are more popular than Lil' Kim.
In [26]:
kimpopularity=0
for artist in lil_artists:
if artist['name']=="Lil' Kim":
kimpopularity=artist['popularity']
for artist in lil_artists:
if kimpopularity < artist['popularity']:
print("Artist Name:",artist['name'], "Popularity Score:",artist['popularity'])
5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks. Tip: You're going to be making two separate requests, be sure you DO NOT save them into the same variable.
In [27]:
artist_id = "0zn6yzsbWj3EPMgOTqfG5k"
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_e = data['tracks']
for track in tracks_e:
print(track['name'])
In [28]:
artist_id = "7352aRY2mqSxBZwzUb6LmA"
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_bowwow = data['tracks']
for track in tracks_bowwow:
print(track['name'])
6) Will the world explode if a musicians swears? Get an average popularity for their explicit songs vs. their non-explicit songs. How many minutes of explicit songs do they have? Non-explicit?
In [11]:
artist_id = "0zn6yzsbWj3EPMgOTqfG5k"
popularity_of_explicit_e=0
popularity_of_clean_e=0
c_count=0
e_count=0
e_time=0
c_time=0
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_e = data['tracks']
for track in tracks_e:
if track['explicit']==True:
popularity_of_explicit_e= popularity_of_explicit_e+track['popularity']
e_count=e_count+1
e_time=e_time+track['duration_ms']
else:
popularity_of_clean_e= popularity_of_clean_e+track['popularity']
c_count=c_count+1
c_time=c_time+track['duration_ms']
print("The Average Popularity of Explicit Songs by the Artist Lil E is",popularity_of_explicit_e/e_count)
print("The minutes of Explicit Songs in their album",(e_time/1000)/60,"minutes")
print("The Average Popularity of Clean Songs by the Artist Lil E is",popularity_of_clean_e/c_count)
print("The minutes of Explicit Songs in their album",(c_time/1000)/60,"minutes")
In [12]:
artist_id = "7352aRY2mqSxBZwzUb6LmA"
popularity_of_explicit_bowwow=0
popularity_of_clean_bowwow=0
c_count=0
e_count=0
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_bowwow = data['tracks']
for track in tracks_bowwow:
if track['explicit']==True:
popularity_of_explicit_e= popularity_of_explicit_e+track['popularity']
e_count=e_count+1
e_time=e_time+track['duration_ms']
else:
popularity_of_clean_e= popularity_of_clean_e+track['popularity']
c_count=c_count+1
c_time=c_time+track['duration_ms']
print("The Average Popularity of Explicit Songs by the Artist Lil Bow Wow is",popularity_of_explicit_e/e_count)
print("The minutes of Explicit Songs in their album",(e_time/1000)/60,"minutes")
print("The Average Popularity of Clean Songs by the Artist Lil Bow Wow is",popularity_of_clean_e/c_count)
print("The minutes of Explicit Songs in their album",(c_time/1000)/60,"minutes")
7) Since we're talking about Lils, what about Biggies? How many total "Biggie" artists are there? How many total "Lil"s? If you made 1 request every 5 seconds, how long would it take to download information on all the Lils vs the Biggies?
In [64]:
biggie_artist_count = 0
offset_value = 0
for page in range(0, 100):
biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_value) + '')
biggie_data = biggie_response.json()
biggie_artists = biggie_data['artists']['items']
for artist in biggie_artists:
biggie_artist_count = biggie_artist_count + 1
offset_value = offset_value + 50
print("There are in total", biggie_artist_count, "Biggies")
lil_artist_count = 0
offset_value = 0
for page in range(0, 100):
lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_value) + '')
lil_data = lil_response.json()
lil_artists = lil_data['artists']['items']
for artist in lil_artists:
lil_artist_count = lil_artist_count + 1
offset_value = offset_value + 50
print("There are in total", lil_artist_count, "Lils.")
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
In [17]:
import requests
big_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&type=track&market=US&limit=50')
lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&type=track&market=US&limit=50')
lil_data = lil_response.json()
big_data = big_response.json()
lil_count=0
lil_popularity=0
big_count=0
big_popularity=0
lil_artists = lil_data['artists']['items']
big_artists = big_data['artists']['items']
for count in lil_artists:
lil_popularity=lil_popularity+count['popularity']
for count in big_artists:
big_popularity=big_popularity+count['popularity']
print ("Average Popularity of Lil's is",lil_popularity/50)
print ("Average Popularity of Biggies's is",big_popularity/50)
if lil_popularity/50>big_popularity/50:
print("Lils are more popular.")
else:
print("Biggies are more popular.")