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 [50]:
import requests
response = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50')
data = response.json()
artists = data['artists']['items']
for artist in artists:
print(artist['name'], artist['popularity'])
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".
In [41]:
for artist in artists:
if not artist['genres']:
print(artist['name'], artist['popularity'], 'No genres listed')
else:
print(artist['name'], artist['popularity'], ', '.join(artist['genres']))
In [213]:
genre_list = []
for artist in artists:
for genre in artist['genres']:
genre_list.append(genre)
sorted_genre = sorted(genre_list)
genre_list_number = range(len(sorted_genre))
genre_count = 0
for number in genre_list_number:
if not sorted_genre[number] == sorted_genre[number - 1]:
print((sorted_genre[number]), genre_list.count(sorted_genre[number]))
if genre_count < genre_list.count(sorted_genre[number]):
genre_count = genre_list.count(sorted_genre[number])
freq_genre = sorted_genre[number]
print('')
print('With', genre_count, 'artists,', freq_genre, 'is the most represented in search results.')
How to use counter
In [4]:
# numbers = ['hip hop', 'jerk', 'juggalo', 'trap', 'soca', 'soca', 'juggalo', 'juggalo']
# from collections import Counter
# counts = Counter(numbers)
# counts
Out[4]:
In [5]:
# counts.most_common(2)
Out[5]:
Tip: "how to join a list Python" might be a helpful search
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 [35]:
highest_pop = 0
for artist in artists:
if artist['name'] != 'Lil Wayne' and highest_pop < artist['popularity']:
highest_pop = artist['popularity']
highest_pop_artist = artist['name']
print(highest_pop_artist, 'is the second-most-popular artist with \"Lil\" in his/her name.')
most_followers = 0
for artist in artists:
if most_followers < artist['followers']['total']:
most_followers = artist['followers']['total']
most_followers_artist = artist['name']
print(most_followers_artist, 'has', most_followers, 'followers.')
if highest_pop_artist == most_followers_artist:
print('The second-most-popular \'Lil\' artist is also the one with the most followers.')
else:
print('The second-most-popular \'Lil\' artist and the one with the most followers are different people.')
4) Print a list of Lil's that are more popular than Lil' Kim.
In [39]:
for artist in artists:
if artist['name'] == 'Lil\' Kim':
more_popular = artist['popularity']
for artist in artists:
if more_popular < artist ['popularity']:
print(artist['name'], 'is more popular than Lil\' Kim with a popularity score of', artist['popularity'])
5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks.
In [61]:
wayne_id = '55Aa2cqylxrFIXC767Z865'
wayne_response = requests.get('https://api.spotify.com/v1/artists/' + wayne_id + '/top-tracks?country=us')
wayne_data = wayne_response.json()
print('Lil Wayne\'s top tracks:')
wayne_tracks = wayne_data['tracks']
for track in wayne_tracks:
print(track['name'])
print('')
kim_id = '5tth2a3v0sWwV1C7bApBdX'
kim_response = requests.get('https://api.spotify.com/v1/artists/' + kim_id + '/top-tracks?country=us')
kim_data = kim_response.json()
print('Lil\' Kim\'s top tracks:')
kim_tracks = kim_data['tracks']
for track in kim_tracks:
print(track['name'])
Tip: You're going to be making two separate requests, be sure you DO NOT save them into the same variable.
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 [79]:
print('Lil Wayne\'s explicit top tracks:')
ew_total_pop = 0
ew_total_tracks = 0
ew_playtime = 0
for track in wayne_tracks:
if track['explicit']:
ew_total_pop = ew_total_pop + track['popularity']
ew_total_tracks = ew_total_tracks + 1
ew_playtime = ew_playtime + track['duration_ms']/60000
if ew_total_tracks == 0:
print('There are no explicit tracks.')
else:
print('The average popularity is', ew_total_pop / ew_total_tracks)
print('He has', ew_playtime, 'minutes of explicit music in his top tracks.')
print('')
print('Lil Wayne\'s non-explicit top tracks:')
nw_total_pop = 0
nw_total_tracks = 0
nw_playtime = 0
for track in wayne_tracks:
if not track['explicit']:
nw_total_pop = nw_total_pop + track ['popularity']
nw_total_tracks = nw_total_tracks + 1
nw_playtime = nw_playtime + track['duration_ms']/60000
if nw_total_tracks == 0:
print('There are no non-explicit tracks.')
else:
print('The average popularity is', nw_total_pop / nw_total_tracks)
print('He has', nw_playtime, 'minutes of non-explicit music in his top tracks.')
print('')
print('Lil\' Kim\'s explicit top tracks:')
ek_total_pop = 0
ek_total_tracks = 0
ek_playtime = 0
for track in kim_tracks:
if track['explicit']:
ek_total_pop = ek_total_pop + track ['popularity']
ek_total_tracks = ek_total_tracks + 1
ek_playtime = ek_playtime + track['duration_ms']/60000
if ek_total_tracks == 0:
print('There are no explicit tracks.')
else:
print('The average popularity is', ek_total_pop / ek_total_tracks)
print('She has', ek_playtime, 'minutes of explicit music in her top tracks.')
print('')
print('Lil\' Kim\'s non-explicit top tracks:')
nk_total_pop = 0
nk_total_tracks = 0
nk_playtime = 0
for track in kim_tracks:
if not track['explicit']:
nk_total_pop = nk_total_pop + track ['popularity']
nk_total_tracks = nk_total_tracks + 1
nk_playtime = nk_playtime + track['duration_ms']/60000
if nk_total_tracks == 0:
print('There are no non-explicit tracks.')
else:
print('The average popularity is', nk_total_pop / nk_total_tracks)
print('She has', nk_playtime, 'minutes of non-explicit music in her top tracks.')
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 [156]:
biggie_response = requests.get('https://api.spotify.com/v1/search?query=artist:biggie&type=artist&market=us&limit=50')
biggie_data = biggie_response.json()
biggie_artists = biggie_data['artists']['items']
total_biggies = 0
for artist in biggie_artists:
total_biggies = total_biggies + 1
print('There are', total_biggies, 'Biggies on Spotify.')
print('It would take', total_biggies * 5, 'seconds to request all of the Biggies if you were requesting one every five seconds.')
print('')
In [159]:
pages = range(90)
total_lils = 0
for page in pages:
lil_response = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50&offset=' + str(page * 50))
lil_data = lil_response.json()
lil_artists = lil_data['artists']['items']
for artist in lil_artists:
total_lils = total_lils + 1
print('There are', total_lils, 'Lils on Spotify.')
print('It would take', round(total_lils / 12), 'minutes to request all of the Lils if you were requesting one every five seconds.')
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
In [164]:
biggie_total_pop = 0
for artist in biggie_artists:
biggie_total_pop = biggie_total_pop + artist['popularity']
biggie_avg_pop = biggie_total_pop / 50
In [171]:
lil_response_pg1 = requests.get('https://api.spotify.com/v1/search?query=artist:lil&type=artist&market=us&limit=50')
lil_data_pg1 = lil_response_pg1.json()
lil_artists_pg1 = lil_data_pg1['artists']['items']
lil_total_pop = 0
for artist in lil_artists_pg1:
lil_total_pop = lil_total_pop + artist['popularity']
lil_avg_pop = lil_total_pop / 50
In [172]:
if biggie_avg_pop > lil_avg_pop:
print('The top 50 biggies are more popular.')
elif biggie_avg_pop < lil_avg_pop:
print('The top 50 lils are more popular.')
else:
print('They are equally popular.')
In [ ]: