In [1]:
import requests
lil_response = requests.get ('https://api.spotify.com/v1/search?query=Lil&type=artist&country=US&limit=50')
lil_data = lil_response.json()
print(type(lil_data))
lil_data.keys()
Out[1]:
In [2]:
lil_data['artists'].keys()
Out[2]:
In [3]:
lil_artists = lil_data['artists']['items']
#check on what elements are in that list:
#print (lil_artists[0])
In [4]:
for artist in lil_artists:
print(artist['name'], "has a popularity score of", artist['popularity'])
In [5]:
#http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python
from collections import Counter
genre_list = []
for genre in lil_artists:
if genre['genres'] != []:
genre_list = genre['genres'] + genre_list
c = Counter(genre_list)
print("These are the counts for each genre:", c)
#https://docs.python.org/2/library/collections.html
most_common = Counter(genre_list).most_common(1)
print("The most common genre is:",most_common)
In [6]:
for artist in lil_artists:
if artist['genres'] == []:
print(artist['name'], "has a popularity score of", artist['popularity'],
"But there are no genres listed for this artist.")
else:
artist_genres = artist['genres']
print(artist['name'], "has a popularity score of", artist['popularity'],
"This artist is associated with", ', '.join(artist_genres))
# http://stackoverflow.com/questions/5850986/joining-elements-of-a-list-python
In [7]:
most_popular_score = 0
most_popular_name = []
for artist in lil_artists:
if artist['popularity'] > most_popular_score:
most_popular_name = artist['name']
most_popular_score = artist['popularity']
print(most_popular_name, "is the most popular, with a rating of", most_popular_score)
second_max_popular = 0
for artist in lil_artists:
if artist['popularity'] >= second_max_popular and artist['popularity'] < most_popular_score:
second_max_popular = artist['popularity']
print(artist['name'], "is the second most popular with a popularity rating of",artist['popularity'], "compared to", most_popular_name, "who has a rating of", most_popular_score)
In [8]:
most_followers = 0
for artist in lil_artists:
if artist['followers']['total'] > most_followers:
most_followers = artist['followers']['total']
print(artist['name'], "has the largest number followers:", artist['followers']['total'])
print("The second most popular Lils have the following amount of followers:")
second_most_followers = 0
for artist in lil_artists:
if artist['popularity'] >= second_max_popular and artist['popularity'] < 86:
second_max_popular = artist['popularity']
if artist['followers']['total'] > second_most_followers:
second_most_followers = artist['followers']['total']
print(artist['name'], artist['followers']['total'])
In [9]:
kim_popularity = 0
for artist in lil_artists:
if artist['name'] == "Lil' Kim":
kim_popularity = (artist['popularity'])
for artist in lil_artists:
if artist['popularity'] > kim_popularity:
print(artist['name'], "has a popularity of", artist['popularity'], "which is higher than that of Lil' Kim.")
In [13]:
#for artist in lil_artists:
#print(artist['name'], artist['id'])
#Lil Dicky 1tqhsYv8yBBdwANFNzHtcr
toptracks_Dicky_response = requests.get('https://api.spotify.com/v1/artists/1tqhsYv8yBBdwANFNzHtcr/top-tracks?country=US')
toptracks_Dicky_data = toptracks_Dicky_response.json()
tracks_Dicky = toptracks_Dicky_data['tracks']
print("THESE ARE THE TOP TRACKS OF LIL DICKY:")
for track in tracks_Dicky:
print(track['name'])
#Lil Jon 7sfl4Xt5KmfyDs2T3SVSMK
toptracks_Jon_response = requests.get('https://api.spotify.com/v1/artists/7sfl4Xt5KmfyDs2T3SVSMK/top-tracks?country=US')
toptracks_Jon_data = toptracks_Jon_response.json()
tracks_Jon = toptracks_Jon_data['tracks']
print("THESE ARE THE TOP TRACKS OF LIL JON:")
for track in tracks_Jon:
print(track['name'])
In [14]:
print(tracks_Dicky[0].keys())
In [15]:
explicit_Dicky_count = 0
non_explicit_Dicky_count = 0
explicit_popularity_Dicky_sum = 0
non_explicit_popularity_Dicky_sum = 0
for track in tracks_Dicky:
if track['explicit'] == True:
explicit_Dicky_count = explicit_Dicky_count + 1
explicit_popularity_Dicky_sum = explicit_popularity_Dicky_sum + track['popularity']
else:
non_explicit_Dicky_count = non_explicit_Dicky_count + 1
non_explicit_popularity_Dicky_sum = non_explicit_popularity_Dicky_sum + track['popularity']
print("The average popularity of explicit Lil Dicky songs is", explicit_popularity_Dicky_sum / explicit_Dicky_count)
if non_explicit_Dicky_count == 0:
print("There are no non-explicit Lil Dicky songs.")
else:
print("The average popularity of non-explicit Lil Dicky songs is:", non_explicit_popularity_Dicky_sum / non_explicit_Dicky_count)
explicit_Jon_count = 0
non_explicit_Jon_count = 0
explicit_popularity_Jon_sum = 0
non_explicit_popularity_Jon_sum = 0
for track in tracks_Jon:
if track['explicit'] == True:
explicit_Jon_count = explicit_Jon_count + 1
explicit_popularity_Jon_sum = explicit_popularity_Jon_sum + track['popularity']
else:
non_explicit_Jon_count = non_explicit_Jon_count + 1
non_explicit_popularity_Jon_sum = non_explicit_popularity_Jon_sum + track['popularity']
print("The average popularity of explicit Lil Jon songs is", explicit_popularity_Jon_sum / explicit_Jon_count)
if non_explicit_Jon_count == 0:
print("There are no non-explicit Lil Jon songs.")
else:
print("The average popularity of non-explicit Lil Jon songs is:", non_explicit_popularity_Jon_sum / non_explicit_Jon_count)
In [16]:
#function writing
def add(a, b):
value = a + b
print("the sum of", a, "and", b, "is", value)
add(5, 7)
add(1, 2)
add(4, 55)
In [17]:
def average_popularity(a, b):
explicit_count = 0
non_explicit_count = 0
explicit_popularity_sum = 0
non_explicit_popularity_sum = 0
for track in a:
if track['explicit'] == True:
explicit_count = explicit_count + 1
explicit_popularity_sum = explicit_popularity_sum + track['popularity']
else:
non_explicit_count = non_explicit_count + 1
non_explicit_popularity_sum = non_explicit_popularity_sum + track['popularity']
if explicit_count == 0:
print("There are no explicit songs by", b)
else:
print("The average popularity of explicit songs by", b, "is", explicit_popularity_sum / explicit_count)
if non_explicit_count == 0:
print("There are no non-explicit songs by", b)
else:
print("The average popularity of non-explicit songs by", b, "is", non_explicit_popularity_sum / non_explicit_count)
average_popularity(tracks_Dicky, "Lil Dicky")
average_popularity(tracks_Jon, "Lil Jon")
In [18]:
def explicit_minutes(a, b):
explicit_milliseconds = 0
non_explicit_milliseconds = 0
for track in a:
if track['explicit'] == True:
explicit_milliseconds = explicit_milliseconds + track['duration_ms']
else:
non_explicit_milliseconds = non_explicit_milliseconds + track['duration_ms']
if explicit_milliseconds !=0:
print(b, "has", explicit_milliseconds / 6000 , "minutes of explicit music.")
if non_explicit_milliseconds !=0:
print(b, "has", non_explicit_milliseconds / 6000, "minutes of non-explicit music.")
else:
print(b, "has", "has no non-explicit music.")
explicit_minutes(tracks_Dicky, "Lil Dicky")
explicit_minutes(tracks_Jon, "Lil Jon")
In [19]:
import requests
biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50')
biggieT_data = biggieT_response.json()
biggieT_artists = biggieT_data['artists']['items']
artist_count = 0
for artist in biggieT_artists:
artist_count = artist_count + 1
print("There are in total", artist_count, "Biggies.")
In [12]:
import requests
import math
offset_valueB = 0
biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_valueB) + '')
biggieT_data = biggieT_response.json()
biggieT_artists = biggieT_data['artists']['items']
offset_limitB = biggieT_data['artists']['total']
offset_valueL = 0
lilT_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_valueL) + '')
lilT_data = lilT_response.json()
lilT_artists = lilT_data['artists']['items']
offset_limitL = lilT_data['artists']['total']
page_countB = math.ceil(offset_limitB/ 50)
print("The page count for all the Biggies is:", page_countB)
page_countL = math.ceil(offset_limitL/ 50)
print("The page count for all the Lils is:", page_countL)
print("If you made 1 request every 5 seconds, it will take", page_countL * 5, "seconds for all the Lils requests to process. Whereas for the Biggies it's", page_countB* 5, ", so the total amount of time is", page_countB*5 + page_countL*5, "seconds.")
In [13]:
artist_count = 0
offset_value = 0
for page in range(0, 1):
biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_valueB) + '')
biggieT_data = biggieT_response.json()
biggieT_artists = biggieT_data['artists']['items']
for artist in lilT_artists:
artist_count = artist_count + 1
offset_value = offset_value + 50
print("There are in total", artist_count, "Biggies.")
artist_count = 0
offset_value = 0
for page in range(0, 91):
lilT_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_value) + '')
lilT_data = lilT_response.json()
lilT_artists = lilT_data['artists']['items']
for artist in lilT_artists:
artist_count = artist_count + 1
offset_value = offset_value + 50
print("There are in total", artist_count, "Lils.")
In [69]:
# tried to solve it with a function as well, but didn't work out, gave an error message. So back to the old way.
biggie50_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50')
biggie50_data = biggie50_response.json()
biggie50_artists = biggie50_data['artists']['items']
popularity_biggie50 = 0
for artist in biggie50_artists:
popularity_biggie50 = popularity_biggie50 + artist['popularity']
print("The average popularity of the top50 Biggies is", popularity_biggie50 / 50)
lil50_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50')
lil50_data = lil50_response.json()
lil50_artists = lil50_data['artists']['items']
popularity_lil50 = 0
for artist in lil50_artists:
popularity_lil50 = popularity_lil50 + artist['popularity']
print("The average popularity of the top50 Lils is", popularity_lil50 / 50)
if popularity_biggie50 > popularity_lil50:
print("The top50 Biggies are on average more popular than the top50 Lils.")
if popularity_biggie50 == popularity_lil50:
print("The top50 Biggies are on average as popular as the top50 Lils.")
else:
print("The top50 Lils are on average more popular than the top50 Biggies.")
In [ ]: