In [3]:
import requests
In [4]:
response = requests.get('https://api.spotify.com/v1/search?query=80s&type=playlist')
In [6]:
print(response.text)
In [8]:
data = response.json()
In [10]:
type(data)
Out[10]:
In [15]:
data.keys()
Out[15]:
In [16]:
response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist')
In [17]:
data = response.json()
In [18]:
data.keys()
Out[18]:
In [20]:
playlist_response = requests.get('https://api.spotify.com/v1/search?query=80s&type=playlist')
playlist_data = playlist_response.json()
biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist')
biggie_data = biggie_response.json()
In [21]:
biggie_data
Out[21]:
In [22]:
type(data)
Out[22]:
In [23]:
data.keys()
Out[23]:
In [25]:
type(data['artists'])
Out[25]:
In [26]:
data['artists'].keys()
Out[26]:
In [28]:
biggie_artists = data['artists']['items']
In [30]:
for artist in biggie_artists:
print(artist['name'], artist['popularity'])
In [32]:
import requests
biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist')
biggie_data = biggie_response.json()
for artist in biggie_artists:
print(artist['name'], artist['popularity'], artist['id'])
In [55]:
# /v1/artists/{id}/albums
# From the docs at https://developer.spotify.com/web-api/artist-endpoints/
# Biggie the Kid
response = requests.get("https://api.spotify.com/v1/artists/35B4rRFHwIJrmeXLNN1hF1/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
for track in tracks:
print(track['name'])
In [71]:
# How explicit is Biggie the Kid?
response = requests.get("https://api.spotify.com/v1/artists/35B4rRFHwIJrmeXLNN1hF1/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
# Counting problem!!!!!
# PART ONE: Initial condition (a.k.a. our start value)
track_count = 0 # I don't know of any tracks!!!!
clean_count = 0 # I don't know of any explicit tracks!!!
for track in tracks:
# You can discover new things in here! It's like a treasure chest
print(track['name'], track['explicit'])
# Make track_count one bigger every time through the loop
if True:
track_count = track_count + 1
# PART TWO: Test, and an increment
if not track['explicit']:
clean_count = clean_count + 1
print("I have found", track_count, "tracks, and", clean_count, "are clean")
# PART THREE: Final use of our counting
print("Overall, I discovered", track_count, "tracks")
print("And", clean_count, "were non-explicit")
print("Which means", 100 * clean_count / track_count, " percent were clean")
In [72]:
# How explicit is the Notorious B.I.G.?
response = requests.get("https://api.spotify.com/v1/artists/5me0Irg2ANcsgc93uaYrpb/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
# Counting problem!!!!!
# PART ONE: Initial condition (a.k.a. our start value)
track_count = 0 # I don't know of any tracks!!!!
clean_count = 0 # I don't know of any explicit tracks!!!
for track in tracks:
# You can discover new things in here! It's like a treasure chest
print(track['name'], track['explicit'])
# Make track_count one bigger every time through the loop
if True:
track_count = track_count + 1
# PART TWO: Test, and an increment
if not track['explicit']:
clean_count = clean_count + 1
print("I have found", track_count, "tracks, and", clean_count, "are clean")
# PART THREE: Final use of our counting
print("Overall, I discovered", track_count, "tracks")
print("And", clean_count, "were non-explicit")
print("Which means", 100 * clean_count / track_count, " percent were clean")
In [77]:
# MC Biggie is 0cbg8Q3ul7S0EOg53foHP1
artist_id = "0cbg8Q3ul7S0EOg53foHP1"
# How explicit is the given artist?
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
# Counting problem!!!!!
# PART ONE: Initial condition (a.k.a. our start value)
track_count = 0 # I don't know of any tracks!!!!
clean_count = 0 # I don't know of any explicit tracks!!!
for track in tracks:
# You can discover new things in here! It's like a treasure chest
print(track['name'], track['explicit'])
# Make track_count one bigger every time through the loop
if True:
track_count = track_count + 1
# PART TWO: Test, and an increment
if not track['explicit']:
clean_count = clean_count + 1
print("I have found", track_count, "tracks, and", clean_count, "are clean")
# PART THREE: Final use of our counting
if track_count > 0:
print("Overall, I discovered", track_count, "tracks")
print("And", clean_count, "were non-explicit")
print("Which means", 100 * clean_count / track_count, " percent were clean")
else:
print("No top tracks found")
In [84]:
#
#
# Given artist_id, how explicit is the artist?
#
#
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks = data['tracks']
# Counting problem!!!!!
# PART ONE: Initial condition (a.k.a. our start value)
track_count = 0 # I don't know of any tracks!!!!
clean_count = 0 # I don't know of any explicit tracks!!!
for track in tracks:
# You can discover new things in here! It's like a treasure chest
print(track['name'], track['explicit'])
# Make track_count one bigger every time through the loop
if True:
track_count = track_count + 1
# PART TWO: Test, and an increment
if not track['explicit']:
clean_count = clean_count + 1
print("I have found", track_count, "tracks, and", clean_count, "are clean")
# PART THREE: Final use of our counting
print("artist_id is", artist_id)
if track_count > 0:
print("Overall, I discovered", track_count, "tracks")
print("And", clean_count, "were non-explicit")
print("Which means", 100 * clean_count / track_count, " percent were clean")
else:
print("No top tracks found")
In [83]:
artist_id = "1OyAhAPFH8Z4bIJewzy3mE"
In [1]:
import requests
response = requests.get("https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&market=US")
data = response.json()
data
In [ ]:
type(data)
data.keys()
type(data['artists'])
data['artists'].keys()
artists = data['artists']['items']
artists
for artist in artists:
print(artist['name'], artist['popularity'])
if len(artist['genres']) > 0:
genres = ", ".join(artist['genres'])
print("Genre list: ", genres)
else:
print("No genres listesd")
In [1]:
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 - WHAT YOU'RE TESTING
print("Comparing", aritst['popularity'], "to", most_popular_score, "of")
if artist['popularity'] > most_popular_score and artist ['name'] != "Lil Wayne":
#THE CHANGE - WHAT YOU'RE KEEPING TRACK OF
most_popular_name = artist['name']
most_popular_score = artist['popularity']
print(most_popular_name, most_popular_score)
In [ ]:
target_score = 72
#PART ONE: INITIAL CONDITON
second_best_artists = []
#AGGREGATION PROBLEM - when you're looping through a series of objects and you someitmes you want to add some
#of those objects to a DIFFERENT list
for artists in artists:
print("Looking at", artist['name'], "who has a popularity of", artist['popularity'])
#PART TWO: CONDITONAL - when we want to add someone to our list
if artist['popularity'] == 72:
#PART THREE: THE CHANGE - add artist to our list
second_best_artists.append(artist['name'])
print("OUR SECOND BEST ARTISTS ARE:")
for artist in second_best_artists:
print(artist)
In [ ]:
In [ ]:
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")
lil_kim_popularity = 62
more_popular_than_lil_kim = []
for artist in artist:
#THE CONDITONAL is the artist more popular than lil kim?
if artist['popularity'] > lil_kim_popularity:
print(artist['name'], "has 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"): print(artist_name)
all_genres = []
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("##### ALL THE GENRES WE'VE HEARD OF AT THE END:") print(all_genres)
unique_genres = set(all_genres) for genre in unique_genres: genre_count = all_genres.count(genre) print(genre, "shows up", genre_count, "times")
In [ ]:
#list(range(91)) gives us a list of 0-90
In [ ]:
# How to automate getting all of the results
response = requests.get("https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&market=US")
small_data = resonse.json()
data['artists']
print(len(data['artists']['items'])) #we only get 10 artists
print(data['artists']['total'])
#FIRST PAGE: artists 1-50, offeset of 0
#https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=50
#SECOND PAGE: artists 51-100, offset of 50
#https://api.spotify.com/v1/search?q=lil&type=artist&limit=50&offset=
all artists = []
for page in range(91):
#Get a page
offset = page * 50
print("We are on page", page, "with an offset of", offset)
print("Successfully retrieved", len(all_artists), "artists")
In [ ]:
states = []
for earthquake in data['features']:
place = earthquake['properties']['place']
results = place.split(",")
#print(results)
#make sure it has to segments
if len(results) > 1:
states_name = results[1]
if state_name == "CA":
states.append("California")
#print(results[1])
#states.append(place)
#print(earthquake)['properties]['place]
states
from collections import Counter
counts = Counter(states)
counts.most_common