Homework 05

Spotify Gianna-Carina Gruen 2016-06-07


In [1]:
import requests
lil_response = requests.get ('https://api.spotify.com/v1/search?query=Lil&type=artist&country=US&limit=50')
lil_data = lil_response.json()
print(type(lil_data))
lil_data.keys()


<class 'dict'>
Out[1]:
dict_keys(['artists'])

In [2]:
lil_data['artists'].keys()


Out[2]:
dict_keys(['previous', 'href', 'total', 'next', 'items', 'offset', 'limit'])

In [3]:
lil_artists = lil_data['artists']['items']
#check on what elements are in that list: 
#print (lil_artists[0])

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 [4]:
for artist in lil_artists:
    print(artist['name'], "has a popularity score of", artist['popularity'])


Lil Wayne has a popularity score of 86
Lil Yachty has a popularity score of 72
Lil Uzi Vert has a popularity score of 73
Lil Dicky has a popularity score of 68
Boosie Badazz has a popularity score of 67
Lil Jon has a popularity score of 72
King Lil G has a popularity score of 61
Lil Durk has a popularity score of 60
Lil Jon & The East Side Boyz has a popularity score of 60
Lil Bibby has a popularity score of 54
G Herbo has a popularity score of 53
Lil Rob has a popularity score of 50
Lil Reese has a popularity score of 50
Lil Keke has a popularity score of 48
Bow Wow has a popularity score of 57
Lil Scrappy has a popularity score of 48
Lil Wyte has a popularity score of 50
Lil Blood has a popularity score of 45
Lil Snupe has a popularity score of 45
Lil Mama has a popularity score of 45
Lil B has a popularity score of 44
Lil' Kim has a popularity score of 62
Lil Cuete has a popularity score of 40
Lil Phat has a popularity score of 39
Lil Boom has a popularity score of 39
Lil Debbie has a popularity score of 43
Lil Twist has a popularity score of 39
Lil Trill has a popularity score of 37
Lil AJ has a popularity score of 37
Lil Lonnie has a popularity score of 37
Lil Twon has a popularity score of 37
Lil Haiti has a popularity score of 36
Lil Goofy has a popularity score of 35
Mr. Lil One has a popularity score of 36
Lil Cray has a popularity score of 35
Lil Flash has a popularity score of 38
Lil Silva has a popularity score of 43
Lil Yase has a popularity score of 34
Lil Rue has a popularity score of 34
Lil Eddie has a popularity score of 41
Lil Kesh has a popularity score of 39
Lil Wayne, DJ Drama has a popularity score of 35
Lil Suzy has a popularity score of 34
Lil Mouse has a popularity score of 34
Lil C has a popularity score of 33
Lil Rick has a popularity score of 39
Lil June has a popularity score of 32
Lil E has a popularity score of 34
Lil Fate has a popularity score of 34
Lil' Flip has a popularity score of 49

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 [5]:
#http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python
from collections import Counter
genre_list = []
for genre in lil_artists:
    if genre['genres'] != []:
        genre_list = genre['genres'] + genre_list
c = Counter(genre_list)
print("These are the counts for each genre:", c)

#https://docs.python.org/2/library/collections.html
most_common = Counter(genre_list).most_common(1)
print("The most common genre is:",most_common)


These are the counts for each genre: Counter({'dirty south rap': 4, 'crunk': 3, 'chicano rap': 3, 'hip pop': 3, 'southern hip hop': 3, 'trap music': 2, 'pop rap': 2, 'juggalo': 1, 'latin hip hop': 1, 'soca': 1, 'deep trap': 1, 'freestyle': 1, 'jerk': 1})
The most common genre is: [('dirty south rap', 4)]

In [6]:
for artist in lil_artists:
    if artist['genres'] == []:
        print(artist['name'], "has a popularity score of", artist['popularity'], 
              "But there are no genres listed for this artist.")
    else:
        artist_genres = artist['genres']
        print(artist['name'], "has a popularity score of", artist['popularity'],
              "This artist is associated with", ', '.join(artist_genres))
# http://stackoverflow.com/questions/5850986/joining-elements-of-a-list-python


Lil Wayne has a popularity score of 86 This artist is associated with dirty south rap, pop rap, southern hip hop, trap music
Lil Yachty has a popularity score of 72 But there are no genres listed for this artist.
Lil Uzi Vert has a popularity score of 73 But there are no genres listed for this artist.
Lil Dicky has a popularity score of 68 But there are no genres listed for this artist.
Boosie Badazz has a popularity score of 67 But there are no genres listed for this artist.
Lil Jon has a popularity score of 72 This artist is associated with crunk, dirty south rap, southern hip hop
King Lil G has a popularity score of 61 But there are no genres listed for this artist.
Lil Durk has a popularity score of 60 But there are no genres listed for this artist.
Lil Jon & The East Side Boyz has a popularity score of 60 But there are no genres listed for this artist.
Lil Bibby has a popularity score of 54 But there are no genres listed for this artist.
G Herbo has a popularity score of 53 But there are no genres listed for this artist.
Lil Rob has a popularity score of 50 This artist is associated with chicano rap, latin hip hop
Lil Reese has a popularity score of 50 But there are no genres listed for this artist.
Lil Keke has a popularity score of 48 But there are no genres listed for this artist.
Bow Wow has a popularity score of 57 This artist is associated with hip pop, pop rap
Lil Scrappy has a popularity score of 48 This artist is associated with crunk, dirty south rap, southern hip hop, trap music
Lil Wyte has a popularity score of 50 This artist is associated with juggalo
Lil Blood has a popularity score of 45 But there are no genres listed for this artist.
Lil Snupe has a popularity score of 45 But there are no genres listed for this artist.
Lil Mama has a popularity score of 45 This artist is associated with hip pop
Lil B has a popularity score of 44 But there are no genres listed for this artist.
Lil' Kim has a popularity score of 62 This artist is associated with hip pop
Lil Cuete has a popularity score of 40 This artist is associated with chicano rap
Lil Phat has a popularity score of 39 But there are no genres listed for this artist.
Lil Boom has a popularity score of 39 But there are no genres listed for this artist.
Lil Debbie has a popularity score of 43 But there are no genres listed for this artist.
Lil Twist has a popularity score of 39 This artist is associated with jerk
Lil Trill has a popularity score of 37 This artist is associated with deep trap
Lil AJ has a popularity score of 37 But there are no genres listed for this artist.
Lil Lonnie has a popularity score of 37 But there are no genres listed for this artist.
Lil Twon has a popularity score of 37 But there are no genres listed for this artist.
Lil Haiti has a popularity score of 36 But there are no genres listed for this artist.
Lil Goofy has a popularity score of 35 But there are no genres listed for this artist.
Mr. Lil One has a popularity score of 36 This artist is associated with chicano rap
Lil Cray has a popularity score of 35 But there are no genres listed for this artist.
Lil Flash has a popularity score of 38 But there are no genres listed for this artist.
Lil Silva has a popularity score of 43 But there are no genres listed for this artist.
Lil Yase has a popularity score of 34 But there are no genres listed for this artist.
Lil Rue has a popularity score of 34 But there are no genres listed for this artist.
Lil Eddie has a popularity score of 41 But there are no genres listed for this artist.
Lil Kesh has a popularity score of 39 But there are no genres listed for this artist.
Lil Wayne, DJ Drama has a popularity score of 35 But there are no genres listed for this artist.
Lil Suzy has a popularity score of 34 This artist is associated with freestyle
Lil Mouse has a popularity score of 34 But there are no genres listed for this artist.
Lil C has a popularity score of 33 But there are no genres listed for this artist.
Lil Rick has a popularity score of 39 This artist is associated with soca
Lil June has a popularity score of 32 But there are no genres listed for this artist.
Lil E has a popularity score of 34 But there are no genres listed for this artist.
Lil Fate has a popularity score of 34 But there are no genres listed for this artist.
Lil' Flip has a popularity score of 49 This artist is associated with crunk, dirty south rap

3) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.


In [7]:
most_popular_score = 0
most_popular_name = []
for artist in lil_artists:
    if artist['popularity'] > most_popular_score:
        most_popular_name = artist['name']
        most_popular_score = artist['popularity']
print(most_popular_name, "is the most popular, with a rating of", most_popular_score)

second_max_popular = 0
for artist in lil_artists:
    if artist['popularity'] >= second_max_popular and artist['popularity'] < most_popular_score:
        second_max_popular = artist['popularity']
        print(artist['name'], "is the second most popular with a popularity rating of",artist['popularity'], "compared to", most_popular_name, "who has a rating of", most_popular_score)


Lil Wayne is the most popular, with a rating of 86
Lil Yachty is the second most popular with a popularity rating of 72 compared to Lil Wayne who has a rating of 86
Lil Uzi Vert is the second most popular with a popularity rating of 73 compared to Lil Wayne who has a rating of 86

Is it the same artist who has the largest number of followers?


In [8]:
most_followers = 0
for artist in lil_artists:
    if artist['followers']['total'] > most_followers:
        most_followers = artist['followers']['total']
        print(artist['name'], "has the largest number followers:", artist['followers']['total'])
print("The second most popular Lils have the following amount of followers:")
second_most_followers = 0
for artist in lil_artists:
    if artist['popularity'] >= second_max_popular and artist['popularity'] < 86:
        second_max_popular = artist['popularity']
        if artist['followers']['total'] > second_most_followers:
            second_most_followers = artist['followers']['total']
            print(artist['name'], artist['followers']['total'])


Lil Wayne has the largest number followers: 2627171
The second most popular Lils have the following amount of followers:
Lil Uzi Vert 58466

In [9]:
kim_popularity = 0
for artist in lil_artists:
    if artist['name'] == "Lil' Kim":
        kim_popularity = (artist['popularity'])
        for artist in lil_artists:
            if artist['popularity'] > kim_popularity:
                print(artist['name'], "has a popularity of", artist['popularity'], "which is higher than that of Lil' Kim.")


Lil Wayne has a popularity of 86 which is higher than that of Lil' Kim.
Lil Yachty has a popularity of 72 which is higher than that of Lil' Kim.
Lil Uzi Vert has a popularity of 73 which is higher than that of Lil' Kim.
Lil Dicky has a popularity of 68 which is higher than that of Lil' Kim.
Boosie Badazz has a popularity of 67 which is higher than that of Lil' Kim.
Lil Jon has a popularity of 72 which is higher than that of Lil' Kim.

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 [13]:
#for artist in lil_artists:
    #print(artist['name'], artist['id'])
#Lil Dicky 1tqhsYv8yBBdwANFNzHtcr

toptracks_Dicky_response = requests.get('https://api.spotify.com/v1/artists/1tqhsYv8yBBdwANFNzHtcr/top-tracks?country=US')
toptracks_Dicky_data = toptracks_Dicky_response.json()
tracks_Dicky = toptracks_Dicky_data['tracks']

print("THESE ARE THE TOP TRACKS OF LIL DICKY:")
for track in tracks_Dicky:
    print(track['name'])

#Lil Jon 7sfl4Xt5KmfyDs2T3SVSMK

toptracks_Jon_response = requests.get('https://api.spotify.com/v1/artists/7sfl4Xt5KmfyDs2T3SVSMK/top-tracks?country=US')
toptracks_Jon_data = toptracks_Jon_response.json()
tracks_Jon = toptracks_Jon_data['tracks']

print("THESE ARE THE TOP TRACKS OF LIL JON:")
for track in tracks_Jon:
    print(track['name'])


THESE ARE THE TOP TRACKS OF LIL DICKY:
$ave Dat Money (feat. Fetty Wap & Rich Homie Quan)
Professional Rapper (feat. Snoop Dogg)
Lemme Freak
Molly (feat. Brendon Urie of Panic at the Disco)
Personality (feat. T Pain)
White Crime
Bruh...
Pillow Talking (feat. Brain)
Who Knew
Oh Well (feat. Jace of Two-9)
THESE ARE THE TOP TRACKS OF LIL JON:
Turn Down for What
Gasolina - DJ Buddah Remix
Snap Yo Fingers
Bend Ova
Turn Down for What - Official Remix
Culo
Get Loose
The Anthem
Prison Riot
Outta Your Mind

6) Will the world explode if a musicians swears?

Get an average popularity for their explicit songs vs. their non-explicit songs.


In [14]:
print(tracks_Dicky[0].keys())


dict_keys(['external_ids', 'duration_ms', 'preview_url', 'album', 'id', 'explicit', 'popularity', 'external_urls', 'track_number', 'href', 'disc_number', 'name', 'available_markets', 'type', 'uri', 'artists'])

First solution -- this felt like a lot of repeating and as if there was a more efficient way to do it. Turns out, there is! With some explanation from Soma first -- see below.


In [15]:
explicit_Dicky_count = 0
non_explicit_Dicky_count = 0
explicit_popularity_Dicky_sum = 0
non_explicit_popularity_Dicky_sum = 0
for track in tracks_Dicky:
    if track['explicit'] == True:
        explicit_Dicky_count = explicit_Dicky_count + 1
        explicit_popularity_Dicky_sum = explicit_popularity_Dicky_sum + track['popularity']
    else:
        non_explicit_Dicky_count = non_explicit_Dicky_count + 1
        non_explicit_popularity_Dicky_sum = non_explicit_popularity_Dicky_sum + track['popularity']
print("The average popularity of explicit Lil Dicky songs is", explicit_popularity_Dicky_sum / explicit_Dicky_count)
if non_explicit_Dicky_count == 0:
    print("There are no non-explicit Lil Dicky songs.")
else:
    print("The average popularity of non-explicit Lil Dicky songs is:", non_explicit_popularity_Dicky_sum / non_explicit_Dicky_count)

explicit_Jon_count = 0
non_explicit_Jon_count = 0
explicit_popularity_Jon_sum = 0
non_explicit_popularity_Jon_sum = 0
for track in tracks_Jon:
    if track['explicit'] == True:
        explicit_Jon_count = explicit_Jon_count + 1
        explicit_popularity_Jon_sum = explicit_popularity_Jon_sum + track['popularity']
    else:
        non_explicit_Jon_count = non_explicit_Jon_count + 1
        non_explicit_popularity_Jon_sum = non_explicit_popularity_Jon_sum + track['popularity']
print("The average popularity of explicit Lil Jon songs is", explicit_popularity_Jon_sum / explicit_Jon_count)
if non_explicit_Jon_count == 0:
    print("There are no non-explicit Lil Jon songs.")
else:
    print("The average popularity of non-explicit Lil Jon songs is:", non_explicit_popularity_Jon_sum / non_explicit_Jon_count)


The average popularity of explicit Lil Dicky songs is 62.1
There are no non-explicit Lil Dicky songs.
The average popularity of explicit Lil Jon songs is 55.142857142857146
The average popularity of non-explicit Lil Jon songs is: 71.33333333333333

Soma explaining how to write functions in 30 seconds of Lab:


In [16]:
#function writing
def add(a, b):
    value = a + b
    print("the sum of", a, "and", b, "is", value)
    
add(5, 7)
add(1, 2)
add(4, 55)


the sum of 5 and 7 is 12
the sum of 1 and 2 is 3
the sum of 4 and 55 is 59

Based on that, I re-wrote my above code using a function


In [17]:
def average_popularity(a, b):
    explicit_count = 0
    non_explicit_count = 0
    explicit_popularity_sum = 0
    non_explicit_popularity_sum = 0
    for track in a:
        if track['explicit'] == True:
            explicit_count = explicit_count + 1
            explicit_popularity_sum = explicit_popularity_sum + track['popularity']
        else:
            non_explicit_count = non_explicit_count + 1
            non_explicit_popularity_sum = non_explicit_popularity_sum + track['popularity']
    if explicit_count == 0:
        print("There are no explicit songs by", b)
    else:
        print("The average popularity of explicit songs by", b, "is", explicit_popularity_sum / explicit_count)
    if non_explicit_count == 0:
        print("There are no non-explicit songs by", b)
    else:
        print("The average popularity of non-explicit songs by", b, "is", non_explicit_popularity_sum / non_explicit_count)

average_popularity(tracks_Dicky, "Lil Dicky")
average_popularity(tracks_Jon, "Lil Jon")


The average popularity of explicit songs by Lil Dicky is 62.1
There are no non-explicit songs by Lil Dicky
The average popularity of explicit songs by Lil Jon is 55.142857142857146
The average popularity of non-explicit songs by Lil Jon is 71.33333333333333

How many minutes of explicit songs do they have? Non-explicit?


In [18]:
def explicit_minutes(a, b):
    explicit_milliseconds = 0 
    non_explicit_milliseconds = 0
    for track in a:
        if track['explicit'] == True:
            explicit_milliseconds = explicit_milliseconds + track['duration_ms']
        else:
            non_explicit_milliseconds = non_explicit_milliseconds + track['duration_ms']
    if explicit_milliseconds !=0:
        print(b, "has", explicit_milliseconds / 6000 , "minutes of explicit music.")
    if non_explicit_milliseconds !=0:
        print(b, "has", non_explicit_milliseconds / 6000, "minutes of non-explicit music.")
    else:
        print(b, "has", "has no non-explicit music.")

explicit_minutes(tracks_Dicky, "Lil Dicky")
explicit_minutes(tracks_Jon, "Lil Jon")


Lil Dicky has 497.19283333333334 minutes of explicit music.
Lil Dicky has has no non-explicit music.
Lil Jon has 246.20966666666666 minutes of explicit music.
Lil Jon has 128.47083333333333 minutes of non-explicit music.

7) 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 [19]:
import requests
biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50')
biggieT_data = biggieT_response.json()
biggieT_artists = biggieT_data['artists']['items']

artist_count = 0
for artist in biggieT_artists:
    artist_count = artist_count + 1
print("There are in total", artist_count, "Biggies.")


There are in total 50 Biggies.

In [12]:
import requests
import math

offset_valueB = 0
biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_valueB) + '')
biggieT_data = biggieT_response.json()
biggieT_artists = biggieT_data['artists']['items']
offset_limitB = biggieT_data['artists']['total']

offset_valueL = 0
lilT_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_valueL) + '')
lilT_data = lilT_response.json()
lilT_artists = lilT_data['artists']['items']
offset_limitL = lilT_data['artists']['total']

page_countB = math.ceil(offset_limitB/ 50)
print("The page count for all the Biggies is:", page_countB)

page_countL = math.ceil(offset_limitL/ 50)
print("The page count for all the Lils is:", page_countL)

print("If you made 1 request every 5 seconds, it will take", page_countL * 5, "seconds for all the Lils requests to process. Whereas for the Biggies it's", page_countB* 5, ", so the total amount of time is", page_countB*5 + page_countL*5, "seconds.")


The page count for all the Biggies is: 1
The page count for all the Lils is: 91
If you made 1 request every 5 seconds, it will take 455 seconds for all the Lils requests to process. Whereas for the Biggies it's 5 , so the total amount of time is 460 seconds.

In [13]:
artist_count = 0
offset_value = 0
for page in range(0, 1):
    biggieT_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_valueB) + '')
    biggieT_data = biggieT_response.json()
    biggieT_artists = biggieT_data['artists']['items']

    for artist in lilT_artists:
        artist_count = artist_count + 1
    offset_value = offset_value + 50
print("There are in total", artist_count, "Biggies.")

artist_count = 0
offset_value = 0
for page in range(0, 91):
    lilT_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_value) + '')
    lilT_data = lilT_response.json()
    lilT_artists = lilT_data['artists']['items']

    for artist in lilT_artists:
        artist_count = artist_count + 1
    offset_value = offset_value + 50
print("There are in total", artist_count, "Lils.")


There are in total 50 Biggies.
There are in total 4513 Lils.

In [69]:
# tried to solve it with a function as well, but didn't work out, gave an error message. So back to the old way.

biggie50_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50')
biggie50_data = biggie50_response.json()
biggie50_artists = biggie50_data['artists']['items']
popularity_biggie50 = 0
for artist in biggie50_artists:
    popularity_biggie50 = popularity_biggie50 + artist['popularity']
print("The average popularity of the top50 Biggies is", popularity_biggie50 / 50)

lil50_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50')
lil50_data = lil50_response.json()
lil50_artists = lil50_data['artists']['items']
popularity_lil50 = 0
for artist in lil50_artists:
    popularity_lil50 = popularity_lil50 + artist['popularity']
print("The average popularity of the top50 Lils is", popularity_lil50 / 50)

if popularity_biggie50 > popularity_lil50:
    print("The top50 Biggies are on average more popular than the top50 Lils.")
if popularity_biggie50 == popularity_lil50:
    print("The top50 Biggies are on average as popular as the top50 Lils.")
else:
    print("The top50 Lils are on average more popular than the top50 Biggies.")


The average popularity of the top50 Biggies is 3.86
The average popularity of the top50 Lils is 46.16
The top50 Lils are on average more popular than the top50 Biggies.

In [ ]: