In [2]:
import requests
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 [3]:
response = requests.get('https://api.spotify.com/v1/search?q=lil&type=artist&?country=US&limit=50')
data = response.json()
In [4]:
type(data)
Out[4]:
In [5]:
data.keys()
Out[5]:
In [6]:
data['artists'].keys()
Out[6]:
In [7]:
artists = data['artists']['items']
In [8]:
for artist in artists:
print(artist['name'], artist['popularity'])
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
In [11]:
for artist in artists:
print (artist['name'], artist['popularity'])
if len(artist['genres']) != 0:
print(", ".join(artist['genres'])
else:
print ("No genres listed")
In [51]:
# AGGREGATION PROBLEM
all_genres = []
# THE LOOP
for artist in 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']
print ("ALLL THE GENRES WE'VE HEARD OF, FINALLY, AT THE END:")
# Has repeats
print (all_genres)
# your_list = ['a', 'b', 'c', 'c', 'c']
# your_list.count('a') = 1
# your_list.count('c') = 3
In [52]:
# This is bad becuase of the duplicates
for genre in all_genres:
genre_count = all_genres.count(genre)
print(genre, "shows up", genre_count, "times")
In [54]:
# We need a UNIQUE list of all_genres, a.k.a. a list
# with all duplicates removed
# 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 [59]:
# There is a library that comes with Python called COLLECTIONS!
# Inside of it is a magic thing alled Counter
# import collections
from collections import Counter
# all_genres = ['sourthern hip hop', 'souther hip hop', 'crunk', ]
counts = Counter(all_genres) #looks like dictionary # import collection -> collection.Counter
counts['crunk'] #so we can do this
counts.most_common(4)
Out[59]:
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?
In [32]:
most_popular_name = ""
most_popular_score = 0
for artist in artists:
print ("Looking at", artist['name'], "who has popularity score of", artist['popularity'])
# THE CONDITIONAL
# a.k.a. what you are testing
print ("Comparing", artist['popularity'], "to", most_popular_score)
if artist['popularity'] > most_popular_score:
print ("FOUND NEW MOST POPULAR, checking to see if it's Lil Wayne")
# Seeing if it's actually Lil Wayne
if artist['name'] == "Lil Wayne":
print ("Nice try Lil Wayne, we don't care")
# THE CHANGE
# a.k.a. what you're keeping track of
else:
print ("NEW POPULAR! Updating our notebook")
most_popular_name = artist['name']
most_popular_score = artist['popularity']
print ("+++++++++++++++++++++++")
print(most_popular_name, most_popular_score)
In [41]:
target_score = 72
# 1: INITIAL CONDITION
# We have no one who is in our list yet
second_best_artists = []
# AGGREGATION PROBLEM
# When you're looking 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'], "who has a popularity of", artist['popularity'])
# 2: CONDITIONAL
# When we want to add someone to our list
if artist['popularity'] == 72:
print ("!!!!!! The artist's popularity is 72")
# 3: THE CHANGE
# Add that artist to our list
# .append(newthing) is how we do that in Python
second_best_artists.append(artist['name'])
print ("OUR SECOND BEST ARTISTS ARE:")
for artist in second_best_artists:
print(artist['name'])
4) Print a list of Lil's that are more popular than Lil' Kim.
In [43]:
for artist in artists:
# print ("Looking at", artist['name'])
print
if artist['name'] == "Lil' Kim":
print ('Found Lil Kim')
print(artist['popularity'])
else:
pass
# print("Not Lil Kim")
In [45]:
lil_kim_popularity = 62
# AGGREGATION PROBLEM
more_popular_than_lil_kim = []
# THE LOOP
for artist in artists:
# THE CONDITIONAL is the artist more popular than lil kim?
if artist['popularity'] > lil_kim_popularity:
# IF yes, let's add them to our list
print (artist['name'], "is MORE POPULAR with a socre of", artist['popularity'])
more_popular_than_lil_kim.append(artist['name'])
else:
print(artist['name'], "is less popular with a socre of", artist['popularity'])
print ("++++++++++++++ MORE POPULAR THAN LIL KIM")
In [47]:
for artist_name in more_popular_than_lil_kim:
print (artist_name)
more_popular_string = ", ".join(more_popular_than_lil_kim)
print ("Artists more popular than Lil' Kim are:", more_popular_string)
In [73]:
response = requests.get('https://api.spotify.com/v1/search?q=lil&type=artist&?country=US&limit=50')
small_data = response.json()
In [74]:
small_data['artists']
print (len(small_data['artists']['items'])) # we only get 10 artists
print(data['artists']['total'])
In [80]:
import math
#response = requests.get('https://api.spotify.com/v1/search?q=lil&type=artist&limit=50')
#small_data = response.json()
page_count = math.ceil(4502/50)
page_count
Out[80]:
In [ ]:
list(range(0,91))
In [82]:
# First Page: artists 1-50
# 'https://api.spotify.com/v1/search?q=lil&type=artist&limit=50'
# Second Page: artists 51-100, offset of 50
# 'https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=50'
# Third Page: artists 101-150
# 'https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=100'
# Forth Page: artists 151-200
# 'https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=150'
all_artists = []
for page in range(91):
offset = (page*50)
print ("we are on page", page+1, "with an offset of", offset)
# make the request with a changed offset ?offset={offset}
# data = response.json()
# add our new artists to our list of existing artists
# all_artists = all_artists + data['artists']['items']
print ("successfully retrived", len(all_artists), "artists")
In [ ]:
response = request.get('')