In [1]:
import requests
In [2]:
Lil_response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&country=US')
Lil_data = Lil_response.json()
#Lil_data
In [3]:
Lil_data.keys()
Out[3]:
In [4]:
Lil_data['artists'].keys()
Out[4]:
In [5]:
Lil_artists = Lil_data['artists']['items']
In [6]:
#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.
count =0
for artist in Lil_artists:
count += 1
print(count,".", artist['name'],"has the popularity of", artist['popularity'])
In [79]:
# 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
# if len(artist['genres']) == 0 )
# print ("no genres")
# else:
# genres = ", ".join(artist['genres'])
genre_list = []
genre_loop = Lil_data['artists']['items']
for item in genre_loop:
#print(item['genres'])
item_gen = item['genres']
for i in item_gen:
genre_list.append(i)
#print(sorted(genre_list))
#COUNTING the most
genre_counter = {}
for word in genre_list:
if word in genre_counter:
genre_counter[word] += 1
else:
genre_counter[word] = 1
popular_genre = sorted(genre_counter, key = genre_counter.get, reverse = True)
top_genre = popular_genre[:1]
print("The genre most represented is", top_genre)
#COUNTING the most with count to confirm
from collections import Counter
count = Counter(genre_list)
most_count = count.most_common(1)
print("The genre most represented and the count are", most_count)
print("-----------------------------------------------------")
for artist in Lil_artists:
num_genres = 'no genres listed'
if len(artist['genres']) > 0:
num_genres= str.join(',', (artist['genres']))
print(artist['name'],"has the popularity of", artist['popularity'], ", and has", num_genres, "under genres")
Use Excel, Illustrator or something like https://infogr.am/ to make a graphic about the Lil's, or the Lil's vs. the Biggies. Just a simple bar graph of their various popularities sounds good to me. Link to the Line Graph of Lil's Popularity chart
In [7]:
Lil_response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&country=US')
Lil_data = Lil_response.json()
#Lil_data
In [9]:
#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?
name_highest = ""
name_follow =""
second_high_pop = 0
highest_pop = 0
high_follow = 0
for artist in Lil_artists:
if (highest_pop < artist['popularity']) & (artist['name'] != "Lil Wayne"):
#second_high_pop = highest_pop
#name_second = artist['name']
highest_pop = artist['popularity']
name_highest = artist['name']
if (high_follow < artist['followers']['total']):
high_follow = artist ['followers']['total']
name_follow = artist['name']
#print(artist['followers']['total'])
print(name_highest, "has the second highest popularity, which is", highest_pop)
print(name_follow, "has the highest number of followers:", high_follow)
#print("the second highest popularity is", second_high_pop)
In [8]:
Lil_response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&country=US')
Lil_data = Lil_response.json()
#Lil_data
In [9]:
Lil_artists = Lil_data['artists']['items']
#Print a list of Lil's that are more popular than Lil' Kim.
count = 0
for artist in Lil_artists:
if artist['popularity'] > 62:
count+=1
print(count, artist['name'],"has the popularity of", artist['popularity'])
#else:
#print(artist['name'], "is less popular with a score of", artist['popularity'])
In [17]:
response = requests.get("https://api.spotify.com/v1/search?query=Lil&type=artist&limit=2&country=US")
data = response.json()
for artist in Lil_artists:
#print(artist['name'],artist['id'])
if artist['name'] == "Lil Wayne":
wayne = artist['id']
print(artist['name'], "id is",wayne)
if artist['name'] == "Lil Yachty":
yachty = artist['id']
print(artist['name'], "id is", yachty)
In [18]:
#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.
response = requests.get("https://api.spotify.com/v1/artists/" +wayne+ "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
print("Lil Wayne's top tracks are: ")
for track in tracks:
print("-", track['name'])
print("-----------------------------------------------")
response = requests.get("https://api.spotify.com/v1/artists/" +yachty+ "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
print("Lil Yachty 's top tracks are: ")
for track in tracks:
print("-", track['name'])
In [28]:
response = requests.get("https://api.spotify.com/v1/artists/" +yachty+ "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
#print(tracks)
In [29]:
#for track in tracks:
#print(track.keys())
In [27]:
#Get an average popularity for their explicit songs vs. their non-explicit songs.
#How many minutes of explicit songs do they have? Non-explicit?
# How explicit is Lils?
response = requests.get("https://api.spotify.com/v1/artists/" +yachty+ "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
# counter for tracks for explicit and clean
track_count = 0
clean_count = 0
#counter to find avg popularity
popular_exp = 0
popular_clean = 0
#counter for avg time in minutes are below:
timer = 0
data_timer = 0
timer_clean = 0
for track in tracks:
print("The track,", track['name'],", with the id",track['id'], "is", track['explicit'],"for explicit content, and has the popularity of", track['popularity'])
track_id = track['id']
time_ms = track['duration_ms']
if True:
track_count = track_count + 1
popular_exp = popular_exp + track['popularity']
response = requests.get("https://api.spotify.com/v1/tracks/" + track_id)
data_track = response.json()
print("and has the duration of", data_track['duration_ms'], "milli seconds.")
timer = timer + time_ms
timer_minutes = ((timer / (1000*60)) % 60)
if not track['explicit']:
clean_count = clean_count + 1
popular_clean = popular_clean + track['popularity']
response = requests.get("https://api.spotify.com/v1/tracks/" + track_id)
data_tracks = response.json()
timer_clean = timer_clean + time_ms
timer_minutes_clean = ((data_timer / (1000*60)) % 60)
print(", and has the duration of", timer_minutes_clean, "minutes")
print("------------------------------------")
avg_pop = popular_exp / track_count
print("I have found", track_count, "tracks, and has the average popularity of", avg_pop, "and has the average duration of", timer_minutes,"minutes and", clean_count, "are clean")
#print("Overall, I discovered", track_count, "tracks")
#print("And", clean_count, "were non-explicit")
#print("Which means", , " percent were clean for Lil Wayne")
In [30]:
#Get an average popularity for their explicit songs vs. their non-explicit songs.
#How many minutes of explicit songs do they have? Non-explicit?
# How explicit is Lils?
response = requests.get("https://api.spotify.com/v1/artists/" +wayne+ "/top-tracks?country=US")
data = response.json()
# counter for tracks for explicit and clean
track_count = 0
clean_count = 0
#counter to find avg popularity
popular_exp = 0
popular_clean = 0
#counter for avg time in minutes are below:
timer = 0
#data_timer = 0
timer_clean = 0
for track in tracks:
print("The track,", track['name'],", with the id",track['id'], "is", track['explicit'],"for explicit content, and has the popularity of", track['popularity'])
track_id = track['id']
time_ms = data_track['duration_ms']
if True:
track_count = track_count + 1
popular_exp = popular_exp + track['popularity']
response = requests.get("https://api.spotify.com/v1/tracks/" + track_id)
data_track = response.json()
print("and has the duration of", data_track['duration_ms'], "milli seconds.")
timer = timer + time_ms
timer_minutes = ((timer / (1000*60)) % 60)
if not track['explicit']:
clean_count = clean_count + 1
popular_clean = popular_clean + track['popularity']
response = requests.get("https://api.spotify.com/v1/tracks/" + track_id)
data_tracks = response.json()
timer_clean = timer_clean + time_ms
timer_minutes_clean = ((data_timer / (1000*60)) % 60)
print(", and has the duration of", timer_minutes_clean, "minutes")
print("------------------------------------")
avg_pop = popular_exp / track_count
print("I have found", track_count, "tracks, and has the average popularity of", avg_pop, "and has the average duration of", timer_minutes,"minutes and", clean_count, "are clean")
#print("Overall, I discovered", track_count, "tracks")
#print("And", clean_count, "were non-explicit")
#print("Which means", , " percent were clean for Lil Wayne")
In [31]:
#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?
biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&country=US')
biggie_data = biggie_response.json()
biggie_artists = biggie_data['artists']['total']
print("Total number of Biggie artists are", biggie_artists)
lil_response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&country=US')
lil_data = lil_response.json()
lil_artists = lil_data['artists']['total']
print("Total number of Lil artists are", lil_artists)
In [22]:
#If you made 1 request every 5 seconds, how long would it take to download information on all the Lils vs the Biggies?
limit_download = 50
biggie_artists = biggie_data['artists']['total']
Lil_artist = Lil_data['artists']['total']
#1n 5 sec = 50
#in 1 sec = 50 / 5 req = 10 no, for 1 no, 1/10 sec
# for 4501 = 4501/10 sec
# for 49 49/ 10 sec
big_count = biggie_artists/10
lil_count = Lil_artist / 10
print("It would take", big_count, "seconds for Biggies, where as it would take", lil_count,"seconds for Lils" )
In [19]:
#Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&country=US')
biggie_data = biggie_response.json()
biggie_artists = biggie_data['artists']['items']
big_count_pop = 0
for artist in biggie_artists:
#count_pop = artist['popularity']
big_count_pop = big_count_pop + artist['popularity']
print("Biggie has a total popularity of ", big_count_pop)
big_pop = big_count_pop / 49
print("Biggie is on an average", big_pop,"popular")
#Lil
Lil_response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&country=US')
Lil_data = Lil_response.json()
Lil_artists = Lil_data['artists']['items']
lil_count_pop = 0
for artist in Lil_artists:
count_pop_lil = artist['popularity']
lil_count_pop = lil_count_pop + count_pop_lil
lil_pop = lil_count_pop / 50
print("Lil is on an average", lil_pop,"popular")
In [ ]: