Graded = 3/8
In [169]:
import requests
# query = 'lil' type = 'artist' number of responses(limit) = '50' market = 'US'
response = requests.get('https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&market=US')
# data is a string, use json
data = response.json()
# print(type(data))
# print(data.keys())
# print(type(data['artists']))
# print(data['artists'].keys())
artists = data['artists']['items']
# print(type(artists))
for artist in artists:
print(artist['name'], artist['popularity'])
In [170]:
for artist in artists:
print(artist['name'], artist['popularity'])
if len(artist['genres']) == 0:
print("No genres listed")
else:
genres = (", ".join(artist['genres']))
print("Genre list:", genres)
In [188]:
# sorted in descending order
artists_by_popularity=[(item[0], item[1]) for item in sorted([(item['name'], item['popularity']) for item in artists], key=lambda item: item[1], reverse=True)]
# print second item in list
print(artists_by_popularity[1])
# TA-Stephan: Did not display who has the second highest popularity.
In [91]:
for artist in artists:
if artist['name'] == "Lil' Kim":
print (artist['name'], "has a popularity of", artist['popularity'])
target_score = artist['popularity']
print("So, any artist with a score greater than", target_score, "is more popular than Lil' Kim")
more_popular = [artist['name'] for artist in artists if artist['popularity'] > 62]
print("The following artist have a popularity greater than", target_score, ":")
for artist in more_popular:
print(artist)
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 [92]:
#TA - Stephan: Want to print out top tracks, not genres
# Find Lil Wayne's id
# for artist in artists:
# if artist['name'] == 'Lil Wayne':
# print(artist['id'])
# get data for top tracks and print list of the artists top tracks
response = requests.get('https://api.spotify.com/v1/artists/55Aa2cqylxrFIXC767Z865/top-tracks?country=US')
data = response.json()
# print(data.keys())
toptracks1 = data['tracks']
print("*****Lil Wayne's top tracks are:*****")
for item in toptracks1:
print(item['name'])
# Find Lil Yachty's id
# for artist in artists:
# if artist['name'] == 'Lil Yachty':
# print(artist['id'])
# get data for top tracks and print list of the artists top tracks
response = requests.get('https://api.spotify.com/v1/artists/6icQOAFXDZKsumw3YXyusw/top-tracks?country=US')
data = response.json()
toptracks2 = data['tracks']
print("*****Lil Yachty's top tracks are:*****")
for item in toptracks2:
print(item['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 [119]:
#changed to Lil Jon because Lil Wayne and Lil Yachty have 100 percent explicit songs
# for artist in artists:
# if artist['name'] == 'Lil Jon':
# print(artist['id'])
# request data for Lil Jon top tracks
response = requests.get('https://api.spotify.com/v1/artists/7sfl4Xt5KmfyDs2T3SVSMK/top-tracks?country=US')
data = response.json()
toptracks3 = data['tracks']
#check dict keys
# print(toptracks3[0].keys())
# explicit songs
explicit_pops = [item['popularity']for item in toptracks3 if item['explicit']]
avg_explicit_pop = sum(explicit_pops)/len(explicit_pops)
print("The average popularity for Lil Jon's explicit songs are", avg_explicit_pop)
explicit_ms = [item['duration_ms'] for item in toptracks3 if item['explicit']]
tot_explicit_min = sum(explicit_ms)/60000
print("He has", tot_explicit_min, "minutes of explicit songs")
# non-explicit songs
nonexplicit_pops = [item['popularity']for item in toptracks3 if not item['explicit']]
avg_nonexplicit_pop = sum(nonexplicit_pops)/len(nonexplicit_pops)
print("The average popularity for Lil Jon's non-explicit songs are", avg_nonexplicit_pop)
nonexplicit_ms = [item['duration_ms'] for item in toptracks3 if not item['explicit']]
tot_nonexplicit_min = sum(nonexplicit_ms)/60000
print("He has", tot_nonexplicit_min, "minutes of non-explicit songs")
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 [168]:
response = requests.get("https://api.spotify.com/v1/search?q=biggie*&type=artist&limit=50")
data = response.json()
print(data)
artists = data['artists']['items']
artist_name=[item['name'] for item in artists]
print(len(artist_name))
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
In [ ]: