In [1]:
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 [15]:
response = requests.get("https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&market=US")
data = response.json()
In [16]:
type(data)
Out[16]:
In [17]:
data.keys()
Out[17]:
In [18]:
type(data['artists'])
Out[18]:
In [19]:
data['artists'].keys()
Out[19]:
In [20]:
artists = data['artists']['items']
#artists
In [22]:
for artist in artists:
print(artist['name'], artist['popularity'])
In [ ]:
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 [32]:
for artist in artists:
print(artist['name'], artist['popularity'])
# YES dirty south rap, pop rap, southern hip hop, trap music
# NO ['dirty south rap', 'pop rap', 'southern hip hop', 'trap music']
if len(artist['genres']) == 0:
print("No genres listed")
else:
genres = ", ".join(artist['genres'])
print("Genre list: ", genres)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
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 [44]:
most_popular_name = ""
most_popular_score = 0
for artist in artists:
print("Looking at", artist['name'], "who has a popularity score of", artist['popularity'])
# THE CONDITIONAL
# a.k.a. what you are testing
print("Comparing", artist['popularity'], "to", most_popular_score)
# Seeing if the artist is more popular that what we've saved
if artist['popularity'] > most_popular_score:
print("FOUND A NEW MOST POPULAR, checking to see if it's Lil Wayne trying to sneak through")
# Seeing if it's actually Lil Wayne
if artist['name'] == "Lil Wayne":
print("Nice try Lil Wayne, we don't care")
else:
# THE CHANGE
# a.k.a. what you're keeping track of
print("Not Lil Wayne, updating our notebook")
most_popular_name = artist['name']
most_popular_score = artist['popularity']
print("#########")
print(most_popular_name, most_popular_score)
In [53]:
name = "Lil Soma"
target_score = 72
# 1: INITIAL CONDITION
# We have no one who is in our list yet
second_best_artists = []
# 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:
# Let's print out what's on our notebook
print("Notebook is", second_best_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)
In [ ]:
In [ ]:
In [ ]:
4) Print a list of Lil's that are more popular than Lil' Kim.
In [62]:
for artist in artists:
#print("Looking at", artist['name'])
if artist['name'] == "Lil' Kim":
print("Found Lil Kim")
print(artist['popularity'])
else:
pass
#print("Not Lil Kim")
In [70]:
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 score of", artist['popularity'])
more_popular_than_lil_kim.append(artist['name'])
else:
print(artist['name'], "is less popular with a score of", artist['popularity'])
print("###### MORE POPULAR THAN LIL KIM #####")
for artist_name in more_popular_than_lil_kim:
print(artist_name)
In [72]:
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)
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 [ ]:
In [ ]:
In [ ]:
In [78]:
# 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']
# "Hello" + "world" => "Helloworld"
# [1, 2, 3] + [4, 5, 6] => [1, 2, 3, 4, 5, 6]
# [1, 2, 3] + 4 => DOES NOT WORK
# [1, 2, 3].append(4) => [1, 2, 3, 4]
print("########")
print("ALL THE GENRES WE'VE HEARD OF, FINALLY, AT THE END:")
# Has repeats
print(all_genres)
# your_list = ['a', 'b', 'c', 'd', 'c', 'b', 'c']
# your_list.count('c') => 3
# your_list.count('a') => 1
# your_list.count('d') => 1
In [79]:
all_genres.count('dirty south rap')
Out[79]:
In [80]:
all_genres.count('crunk')
Out[80]:
In [82]:
# This is bad because of the duplicates in all_genres
# It keeps repeating "dirty south rap shows up 4 times"
for genre in all_genres:
genre_count = all_genres.count(genre)
print(genre, "shows up", genre_count, "times")
In [84]:
# 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 [101]:
# There is a library that comes with Python called COLLECTIONS!!!
# Inside of it is a magic thing called Counter.
#import collections
from collections import Counter
# all_genres = [ 'southern hip hop', 'southern hip hop', 'crunk', ]
# a list of strings of genres, with duplicates
#counts = collections.Counter(all_genres)
counts = Counter(all_genres)
print('crunk shows up', counts['crunk'], 'times')
print('dirty south rap shows up', counts['dirty south rap'], 'times')
counts.most_common(4)
Out[101]:
In [ ]:
In [109]:
data['artists']
print(len(data['artists']['items'])) # We only get 10 artists
print(data['artists']['total'])
In [124]:
import math
#response = requests.get("https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&market=US")
#data = response.json()
# math.ceil rounds up
page_count = math.ceil(4502 / 50)
page_count
Out[124]:
In [128]:
range(91) # gives us a list of 0 through 90
#list(range(91))
Out[128]:
In [132]:
# FIRST PAGE: artists 1-50, offset of 0
# 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, offset of 100
# https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=100
# FOURTH PAGE: artists 151-200, offset of 150
# https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=100
# AGGREGATE
all_artists = []
for page in range(91):
offset = page * 50
print("We are on page", page, "with an offset of", offset)
# Make the request with a changed offset ?offset={offset}
url = "https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=" + str(offset)
print("Requesting", url)
response = requests.get(url)
data = response.json()
# add our new artists to our list of existing artists
print("Found", len(data['artists']['items']), "new artists")
all_artists = all_artists + data['artists']['items']
print("Total count of artists:", len(all_artists))
print("Successfully retrieved", len(all_artists), "artists")
In [133]:
len(all_artists)
Out[133]:
In [136]:
all_artists[4200]
Out[136]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [137]:
response = requests.get("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson")
data = response.json()
In [138]:
type(data)
Out[138]:
In [139]:
data.keys()
Out[139]:
In [140]:
type(data['features'])
Out[140]:
In [142]:
# How many earthquakes in the past day?
len(data['features'])
Out[142]:
In [144]:
# Taking a look at the first one
data['features'][0]
Out[144]:
In [146]:
data['features'][0].keys()
Out[146]:
In [148]:
data['features'][0]['properties'].keys()
Out[148]:
In [149]:
data['features'][0]['properties']['place']
Out[149]:
In [151]:
for earthquake in data['features']:
print(earthquake['properties']['place'])
In [166]:
states = []
for earthquake in data['features']:
place = earthquake['properties']['place']
# Split on comma space, otherwise we have
# extra whitespace (a.k.a. spaces)
edited_place = place.replace(" CA", " California")
results = edited_place.split(", ")
#print(results)
# Make sure it has two segments
if len(results) > 1:
state_name = results[1]
#if state_name == "CA":
# states.append("California")
#else:
# states.append(state_name)
states.append(state_name)
#print(results[1])
# states.append(place)
#print(earthquake['properties']['place'])
states
from collections import Counter
counts = Counter(states)
counts.most_common(3)
Out[166]:
In [ ]:
In [ ]: