In [1]:
import requests
These are the search queries for the Spotify Web API
In [2]:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
In [3]:
Lil_data.keys()
Out[3]:
In [4]:
Lil_data['artists'].keys()
Out[4]:
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 [5]:
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
print(artist['name'], artist['popularity'])
2 a) What genres are most represented in the search results?
Finding all the genres and combining into one list.
In [6]:
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
print(artist['name'], artist['popularity'])
#joining
if len(artist['genres']) == 0:
print("No genres listed")
else:
genres = ", ".join(artist['genres'])
print("Genres: ", genres)
In [7]:
Lil_artists = Lil_data['artists']['items']
Lil_genres_list = []
for genres in Lil_artists:
Lil_genres_list = genres["genres"] + Lil_genres_list
print(Lil_genres_list)
Counting the genres.
In [8]:
Genre_list = [[x,Lil_genres_list.count(x)] for x in set(Lil_genres_list)]
print(Genre_list)
Sorting the genres by occurences.
In [9]:
sorted(Genre_list, key = lambda x: int(x[1]), reverse=True)
Sorted_by_occurences_Genre_list = sorted(Genre_list, key = lambda x: int(x[1]), reverse=True)
print("The most frequent genre of the musicians called Lil is", Sorted_by_occurences_Genre_list[0])
2 b) 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 [10]:
Lil_artists = Lil_data['artists']['items']
for artist in Lil_artists:
if artist['genres'] == []:
print(artist['name'], artist['popularity'], "No genres listed.")
else:
print(artist['name'], artist['popularity'], artist['genres'])
3 a) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
In [11]:
for artist in Lil_artists:
if artist['popularity'] >= 72 and artist['name'] != 'Lil Wayne':
print(artist['name'])
In [12]:
#Better solution:
most_popular_name = ""
most_popular_score = 0
for artist in Lil_artists:
#print("Comparing", artist['popularity'], 'to', most_popular_score)
if artist['popularity'] > most_popular_score:
print("checking for Lil Wayne")
if artist['name'] == 'Lil Wayne':
print('go away')
else:
#The change you are keeping track of
#a.k.a. what you are keeping track of
print('not Lil Wayne, updating our notebook')
most_popular_name = artist['name']
most_popular_score = artist['popularity']
print(most_popular_name, most_popular_score)
In [13]:
####### This doesn't work
#name = 'Lil Soma'
#target_score = 72
#1 INITIAL CONDITION
#second_best_artists = []
#second_best_artists = [Lil Yachty]
#Aggregation Problem
#When you're looping through a series of serious objects
#and sometimes you want to add one of those objects
#to a different list
#for artist in artists:
# print('Looking at', artist['name'])
#2 COndition
#wehen we want someone on the list
# if artist['popularity'] == 72:
# print('!!! The artist is popularity is 72.')
# second_best_artists.append(second_best_artists)
In [14]:
Lil_data['artists'].keys()
Out[14]:
In [15]:
for artist in Lil_artists:
if artist['name'] == ["Lil Wayne"]:
print(artist['popularity'], "is")
In [ ]:
3 b) Is it the same artist who has the largest number of followers?
In [16]:
type(artist['followers'])
Out[16]:
In [17]:
artist['followers']
Out[17]:
Creating a list of the popularity values, so we can sort them and say which one is the highest)
In [18]:
Lil_artists = Lil_data['artists']['items']
List_of_Followers = []
for artist in Lil_artists:
List_of_Followers.append(artist['followers']['total'])
print(List_of_Followers)
Deciding which one is highest:
In [19]:
List_of_Followers.sort(reverse=True)
print(List_of_Followers)
In [20]:
Highest_Number_of_Followers = (List_of_Followers[0])
In [21]:
print(Highest_Number_of_Followers)
In [22]:
for artist in Lil_artists:
if artist['followers']['total'] > List_of_Followers[0] and artist['name'] != 'Lil Wayne':
print(artist['name'], "has more followers than Lil Wayne.")
else:
print("Their are no artists with more followers that Lil Wayne.")
break
4) Print a list of Lil's that are more popular than Lil' Kim.
Establishing how high Lil' Kim's popularity is. Would this be possible in one go?
In [23]:
for artist in Lil_artists:
if artist['name'] == "Lil' Kim":
print(artist['popularity'])
In [24]:
for artist in Lil_artists:
if artist['popularity'] > 62:
print(artist['name'], artist['popularity'])
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 [25]:
for artist in Lil_artists:
print(artist['name'], artist['id'])
In [26]:
response = requests.get('https://api.spotify.com/v1/artists/5einkgXXrjhfYCyac1FANB/top-tracks?country=US')
Lil_Scrappy_data = response.json()
type(Lil_Scrappy_data)
Out[26]:
In [27]:
response = requests.get('https://api.spotify.com/v1/artists/5qK5bOC6wLtuLhG5KvU17c/top-tracks?country=US')
Lil_Mama_data = response.json()
type(Lil_Mama_data)
Out[27]:
In [28]:
Lil_Scrappy_data.keys()
Lil_Mama_data.keys()
Out[28]:
In [29]:
type(Lil_Scrappy_data.keys())
type(Lil_Mama_data.keys())
Out[29]:
In [30]:
Scrappy_tracks = Lil_Scrappy_data['tracks']
for tracks in Scrappy_tracks:
print(tracks['name'])
In [31]:
Mama_tracks = Lil_Mama_data['tracks']
for tracks in Mama_tracks:
print(tracks['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?
Number of Explicit Tracks for Lil Scrappy.
In [32]:
explicit_track_scrappy = 0
non_explicit_track_scrappy = 0
unknown_scrappy = 0
for tracks in Scrappy_tracks:
if tracks['explicit'] == True:
explicit_track_scrappy = explicit_track_scrappy + 1
elif tracks['explicit'] == False:
non_explicit_track_scrappy = non_explicit_track_scrappy + 1
else:
unknown_scrappy = unknown_scrappy + 1
explicit_track_pop_total = 0
non_explicit_track_pop_total = 0
for tracks in Scrappy_tracks:
if tracks['explicit'] == True:
explicit_track_pop_total = explicit_track_pop_total + tracks['popularity']
elif tracks['explicit'] == False:
non_explicit_track_pop_total = non_explicit_track_pop_total + tracks['popularity']
explicit_track_duration_total = 0
non_explicit_track_duration_total = 0
for tracks in Scrappy_tracks:
if tracks['explicit'] == True:
explicit_track_duration_total = explicit_track_duration_total + tracks['duration_ms']
elif tracks['explicit'] == False:
non_explicit_track_duration_total = non_explicit_track_duration_total + tracks['duration_ms']
print("The average rating of explicit songs by Lil Scrappy is", round(explicit_track_pop_total / explicit_track_scrappy), ".")
print("The average rating of non-explicit songs by Lil Scrappy is", round(non_explicit_track_pop_total / non_explicit_track_scrappy), ".")
print("The duration of explicit song material of Lil Scrappy is", round(explicit_track_duration_total / 1000), "minutes, and of non explicit material is", round(non_explicit_track_duration_total / 1000), "minutes.")
And this is the same for Lil Mama:
In [33]:
explicit_track_Mama = 0
non_explicit_track_Mama = 0
unknown = 0
for tracks in Mama_tracks:
if tracks['explicit'] == True:
explicit_track_Mama = explicit_track_Mama + 1
elif tracks['explicit'] == False:
non_explicit_track_Mama = non_explicit_track_Mama + 1
else:
unknown = unknown + 1
explicit_track_pop_total_Mama = 0
non_explicit_track_pop_total_Mama = 0
for tracks in Mama_tracks:
if tracks['explicit'] == True:
explicit_track_pop_total_Mama = explicit_track_pop_total_Mama + tracks['popularity']
elif tracks['explicit'] == False:
non_explicit_track_pop_total_Mama = non_explicit_track_pop_total_Mama + tracks['popularity']
explicit_track_duration_total_Mama = 0
non_explicit_track_duration_total_Mama = 0
for tracks in Mama_tracks:
if tracks['explicit'] == True:
explicit_track_duration_total_Mama = explicit_track_duration_total_Mama + tracks['duration_ms']
elif tracks['explicit'] == False:
non_explicit_track_duration_total_Mama = non_explicit_track_duration_total_Mama + tracks['duration_ms']
print("The average rating of explicit songs by Lil Mama is", round(explicit_track_pop_total_Mama / explicit_track_Mama), ".")
print("The average rating of non-explicit songs by Lil Mama is", round(non_explicit_track_pop_total_Mama / non_explicit_track_Mama), ".")
print("The duration of explicit song material of Lil Mama is", round(explicit_track_duration_total_Mama / 1000), "minutes, and of non explicit material is", round(non_explicit_track_duration_total_Mama / 1000), "minutes.")
7 a) 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 [34]:
response = requests.get('https://api.spotify.com/v1/search?query=Biggie&type=artist&limit=50&market=US')
Biggie_data = response.json()
In [35]:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
In [36]:
Biggie_artists = Biggie_data['artists']['total']
Lil_artists = Lil_data['artists']['total']
print("There are", Biggie_artists, "artists named Biggie on Spotify and", Lil_artists, "named Lil",)
In [37]:
Total_Download_Time_Biggie = Biggie_artists / 50 * 5
Total_Download_Time_Lil = Lil_artists / 50 * 5
print("It would take", round(Total_Download_Time_Biggie), "seconds to download all the Biggie artists and", round(Total_Download_Time_Lil), "seconds to download the Lil artists." )
8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
In [38]:
Lil_artists_popularity = Lil_data['artists']['items']
popularity_total = 0
for popularity in Lil_artists_popularity:
popularity_total = popularity_total + popularity['popularity']
print("The average rating for the top 50 artists called Lil is:", round(popularity_total / 50))
In [39]:
Biggie_artists_popularity = Biggie_data['artists']['items']
Biggie_popularity_total = 0
for popularity2 in Biggie_artists_popularity:
Biggie_popularity_total = Biggie_popularity_total + popularity2['popularity']
print("The average rating for the top 50 artists called Biggie is:", round(Biggie_popularity_total / 49) )
In [41]:
Lil_artists_popularity = Lil_data['artists']['items']
for popularity in Lil_artists_popularity:
print(popularity['name'], popularity['popularity'])
In [46]:
Biggie_popularity = Biggie_data['artists']['items']
for artist in Biggie_popularity:
print(artist['name'], artist['popularity'])
In [53]:
import csv
with open('Biggie.csv', 'w') as mycsvfile:
thedatawriter = csv.writer(mycsvfile)
for artist in Biggie_popularity:
thedatawriter.writerow(artist['name'])
thedatawriter.writerow(artist['popularity'])
In [ ]:
In [ ]: