Graded = 6/8
In [1]:
import requests
smallresponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=50')
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=50
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=100
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=150
# offset = 0
smalldata = response.json()
# total numer of artists
total_artists = smalldata['artists']['total']
total_pages = total_artists/50
import math
page_count = math.ceil(total_pages)
print("total pages", page_count)
for page in range(page_count):
offset = (page) * 50
print("We are on page", page + 1 , "with an offset of", offset)
# make the request with a changed ?offset={offset}
# data = response.json()
# add our new artists to our list of existing artists
# all_artists = all_artists + data['artists]['items']
smalldata = response.json()
# print(smalldata['artists'])
# smalldata['artists']
# print(smalldata['artists']['items'])
In [2]:
#http://google.com/search ? q = cats & lang = US (without spaces)
# URL PARAMETER MORE PARAMETERS
#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.
import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
lil_artists = data['artists']['items']
for artist in lil_artists:
print(artist['name'], artist['popularity'])
# for artist in lil_artists:
# print(artist['popularity'])
In [3]:
# 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
import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
lil_artists = data['artists']['items']
for artist in lil_artists:
print(artist['name'], artist['popularity'])
if len(artist['genres']) > 0:
s = ', '
# prints without brackets! woohoo -- unlike print(artists['genres'])
print(s.join(artist['genres']))
else:
print("No genres listed.")
In [4]:
all_genres = []
for artist in lil_artists:
#print("All genres we've heard of:", all_genres)
#the conditional none
# print("Current artist has:", artist['genres'])
all_genres = all_genres + artist['genres']
for item in all_genres:
# repeats dirty south 4 times !
print(item, "shows up", all_genres.count(item), "times.")
In [48]:
#import collections -- imports entire library
from collections import Counter # -- only imports Counter
all_genres = []
for artist in lil_artists:
#print("All genres we've heard of:", all_genres)
#the conditional none
# print("Current artist has:", artist['genres'])
all_genres = all_genres + artist['genres']
# collections.Counter
counts = Counter(all_genres) # counts the occurence of each object # it looks like a dictionary
counts.most_common(2)
counts.most_common(1)
counts.most_common(4)
Out[48]:
In [5]:
# 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?
import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
max_popularity = 0;
lil_artists = data['artists']['items']
for artist in lil_artists:
if not artist['name'] == 'Lil Wayne':
# print(artist['name'], artist['popularity'])
if artist['popularity'] > max_popularity:
# print("Now",artist['name'], "is the most popular.")
max_popularity = artist['popularity']
most_popular = artist['name']
print("The most popular artist (not including Lil Wayne) is", most_popular )
In [11]:
# lilkim_id = '5tth2a3v0sWwV1C7bApBdX'
# lilkimresponse = requests.get('https://api.spotify.com/v1/artists/' + lilkim_id)
# lilkimresponse.keys()
In [7]:
# 4) Print a list of Lil's that are more popular than Lil' Kim.
import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
lil_artists = data['artists']['items']
lilkim_id = '5tth2a3v0sWwV1C7bApBdX'
lilkimresponse = requests.get('https://api.spotify.com/v1/artists/' + lilkim_id)
lilkimresponse = lilkimresponse.json()
# print(lilkimresponse.keys())
lilkim_pop = lilkimresponse['popularity']
morepopularthankim = 0;
print("Lil Kim has a popularity of", lilkim_pop)
for artist in lil_artists:
# print(artist['name'], artist['popularity'], artist['id'])
if artist['popularity'] > lilkim_pop:
print(artist['name'], "is more popular than Lil Kim.")
morepopularthankim = morepopularthankim + 1
print(morepopularthankim, "Lil artists are more popular than Lil Kim.")
print("The most popular artist (not including Lil Wayne) is", most_popular )
In [8]:
#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.
lil_jon_id = '7sfl4Xt5KmfyDs2T3SVSMK'
liljonresponse = requests.get('https://api.spotify.com/v1/artists/'+ lil_jon_id+'/top-tracks?country=US')
liljondata = liljonresponse.json()
#liljontracks = liljondata['items']['tracks']
# print(liljondata.keys())
# print(liljondata)
liljontracks = liljondata['tracks']
print('Lil Jon\'s top tracks are:')
for track in liljontracks:
print(track['name'])
# print(type(liljondata['items']))
print('Lil Wayne\'s top tracks are:')
lil_wayne_id = '55Aa2cqylxrFIXC767Z865'
lilwayneresponse = requests.get('https://api.spotify.com/v1/artists/'+ lil_wayne_id+'/top-tracks?country=US')
lilwaynedata = lilwayneresponse.json()
lilwaynetracks = lilwaynedata['tracks']
for track in lilwaynetracks:
print(track['name'])
#lilwayne = lilwaynedata['tracks']
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 [9]:
lj_clean_popularity = 0;
clean_count = 0;
lj_explicit_popularity = 0;
explicit_count =0;
for track in liljontracks:
print(track['name'])
if track['explicit']:
print("this track is explicit")
explicit_count = explicit_count + 1;
lj_explicit_popularity = lj_explicit_popularity + track['popularity']
else:
print("this track is clean")
clean_count = clean_count + 1;
lj_clean_popularity = lj_clean_popularity + track['popularity']
print("Overall the clean popularity for Lil Jon was",lj_clean_popularity/clean_count,"and the explicit popularity was",lj_explicit_popularity/explicit_count)
In [10]:
lw_clean_popularity = 0;
clean_count = 0;
lw_explicit_popularity = 0;
explicit_count =0;
for track in lilwaynetracks:
print(track['name'])
if track['explicit']:
print("this track is explicit")
explicit_count = explicit_count + 1;
lw_explicit_popularity = lw_explicit_popularity + track['popularity']
else:
print("this track is clean")
clean_count = clean_count + 1;
lw_clean_popularity = lw_clean_popularity + track['popularity']
if clean_count > 1 and explicit_count > 1:
print("Overall the clean popularity for Lil Wayne was",lw_clean_popularity/clean_count,"and the explicit popularity was",lw_explicit_popularity/explicit_count)
elif clean_count == 0 and explicit_count >1:
print("Overall Lil wayne had zero clean songs and the explicit popularity was",lw_explicit_popularity/explicit_count, '%')
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 [11]:
#TA-Stephan: Should have two different answers.
import requests
biggieResponse = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&market=US')
biggieData = biggieResponse.json()
# print(biggieData.keys())
biggie_count = 0
# print(len(biggieData['artists']))
for artist in biggieData['artists']['items']:
biggie_count = biggie_count + 1
print("There are",biggie_count,"Biggie artists.")
lilResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
lilData = lilResponse.json()
# print(lilData['artists'].keys())
lil_count = 0
# print(lilData['artists'])
for artist in lilData['artists']['items']:
lil_count = lil_count + 1
print("There are",lil_count,"lil artists.")
print("It would take", biggie_count,"seconds to get all of the data about Biggie artists and", lil_count,"seconds to get all the information about lil artists")
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
In [134]:
#Ta-Stephan: Should have had compared Biggies and Lils, not the most popular of them all
import requests
lilResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
lilData = lilResponse.json()
# print(data)
max_popularity = 0;
lil_artists = data['artists']['items']
for artist in lil_artists:
# print(artist['name'], artist['popularity'])
if artist['popularity'] > max_popularity:
max_popularity = artist['popularity']
most_popular = artist['name']
import requests
bigResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
bigData = bigResponse.json()
# print(data)
big_artists = data['artists']['items']
for artist in big_artists:
if artist['popularity'] > max_popularity:
max_popularity = artist['popularity']
most_popular = artist['name']
print("The most popular artist of all of these artists is", most_popular)
In [ ]: