In [3]:
import requests
These are the search queries for the Spotify Web API
In [4]:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
In [5]:
Lil_data.keys()
Out[5]:
In [6]:
Lil_data['artists'].keys()
Out[6]:
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 [7]:
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 [8]:
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 [9]:
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 [10]:
Genre_list = [[x,Lil_genres_list.count(x)] for x in set(Lil_genres_list)]
print(Genre_list)
Sorting the genres by occurences.
In [11]:
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 [12]:
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'])
In [27]:
Lil_artists = Lil_data['artists']['items']
#Genres
all_genres = []
#The Loop
for artist in Lil_artists:
#print("All Genres we have heard of:", all_genres)
#print('Current artist has', artist['genres'])
all_genres = all_genres + artist['genres']
print(all_genres)
all_genres.count('dirty south rap')
# your_list
Out[27]:
In [28]:
#This shows duplicates
for genre in all_genres:
genre_count = all_genres.count(genre)
print(genre, "shows up", genre_count, "times.")
In [29]:
#Unique list of all genres:
#Unique List = set(list_with_duplicates)
unique_genres = set(all_genres)
for genre in unique_genres:
genre_count = all_genres.count(genre)
print(genre, "shows up", genre_count, "times.")
In [ ]:
#There is a library tha comes with Python called Collections
#Inside of this library is Counter
In [34]:
import collections
In [42]:
from collections import Counter
counts = Counter(all_genres)
counts.most_common(1)
Out[42]:
In [36]:
#
print(counts['crunk'])
In [60]:
from collections import Counter
counts = Collections.Counter(all_genres)
counts.most_common(1)
In [61]:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
small_data = response.json()
In [62]:
small_data['artists']
len(small_data['artists'])
Out[62]:
In [63]:
print("test")
In [ ]:
3 a) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
In [64]:
for artist in Lil_artists:
if artist['popularity'] >= 72 and artist['name'] != 'Lil Wayne':
print(artist['name'])
In [65]:
#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 [66]:
####### 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 [67]:
Lil_data['artists'].keys()
Out[67]:
3 b) Is it the same artist who has the largest number of followers?
In [68]:
type(artist['followers'])
Out[68]:
In [69]:
artist['followers']
Out[69]:
Creating a list of the popularity values, so we can sort them and say which one is the highest)
In [19]:
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 [18]:
List_of_Followers.sort(reverse=True)
print(List_of_Followers)
In [19]:
Highest_Number_of_Followers = (List_of_Followers[0])
In [20]:
print(Highest_Number_of_Followers)
In [21]:
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 [19]:
for artist in Lil_artists:
if artist['name'] == "Lil' Kim":
print(artist['popularity'])
In [20]:
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 [21]:
for artist in Lil_artists:
print(artist['name'], artist['id'])
In [22]:
response = requests.get('https://api.spotify.com/v1/artists/5einkgXXrjhfYCyac1FANB/top-tracks?country=US')
Lil_Scrappy_data = response.json()
type(Lil_Scrappy_data)
Out[22]:
In [23]:
response = requests.get('https://api.spotify.com/v1/artists/5qK5bOC6wLtuLhG5KvU17c/top-tracks?country=US')
Lil_Mama_data = response.json()
type(Lil_Mama_data)
Out[23]:
In [24]:
Lil_Scrappy_data.keys()
Lil_Mama_data.keys()
Out[24]:
In [25]:
type(Lil_Scrappy_data.keys())
type(Lil_Mama_data.keys())
Out[25]:
In [26]:
Scrappy_tracks = Lil_Scrappy_data['tracks']
for tracks in Scrappy_tracks:
print(tracks['name'])
In [27]:
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 [89]:
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 [90]:
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 [35]:
response = requests.get('https://api.spotify.com/v1/search?query=Biggie&type=artist&limit=50&market=US')
Biggie_data = response.json()
In [38]:
response = requests.get('https://api.spotify.com/v1/search?query=Lil&type=artist&limit=50&market=US')
Lil_data = response.json()
In [39]:
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 [43]:
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 [68]:
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 [70]:
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 [ ]:
In [65]:
Biggie_popularity = Biggie_data['artists']['items']
for artist in Biggie_popularity:
print(artist['name'], artist['popularity'])
In [ ]: