SPOTIFY HOMEWORK NOTEBOOK

grade = 8/8

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 [1]:
import requests
lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&type=track&market=US&limit=50')
#print(response.text)

In [2]:
lil_data = lil_response.json()

In [3]:
lil_data.keys()
#print(lil_data)


Out[3]:
dict_keys(['artists'])

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


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

In [8]:
lil_artists = lil_data['artists']['items']
for artist in lil_artists:
    print("Artist Name:",artist['name'],"|| Artist Popularity Score:",artist['popularity'],"|| Artist ID for reference,later",artist['id'])


Artist Name: Lil Wayne || Artist Popularity Score: 86 || Artist ID for reference,later 55Aa2cqylxrFIXC767Z865
Artist Name: Lil Yachty || Artist Popularity Score: 73 || Artist ID for reference,later 6icQOAFXDZKsumw3YXyusw
Artist Name: Lil Uzi Vert || Artist Popularity Score: 73 || Artist ID for reference,later 4O15NlyKLIASxsJ0PrXPfz
Artist Name: Lil Dicky || Artist Popularity Score: 68 || Artist ID for reference,later 1tqhsYv8yBBdwANFNzHtcr
Artist Name: Boosie Badazz || Artist Popularity Score: 67 || Artist ID for reference,later 6z7xFFHxYkE9t8bwIF0Bvg
Artist Name: Lil Jon || Artist Popularity Score: 72 || Artist ID for reference,later 7sfl4Xt5KmfyDs2T3SVSMK
Artist Name: King Lil G || Artist Popularity Score: 61 || Artist ID for reference,later 6L3x3if9RVimruryD9LoFb
Artist Name: Lil Durk || Artist Popularity Score: 60 || Artist ID for reference,later 3hcs9uc56yIGFCSy9leWe7
Artist Name: Lil Jon & The East Side Boyz || Artist Popularity Score: 60 || Artist ID for reference,later 3ciRvbBIVz9fBoPbtSYq4x
Artist Name: Lil Bibby || Artist Popularity Score: 54 || Artist ID for reference,later 4uSN8Y3kgFNVULUWsZEAVW
Artist Name: G Herbo || Artist Popularity Score: 53 || Artist ID for reference,later 5QdEbQJ3ylBnc3gsIASAT5
Artist Name: Lil Rob || Artist Popularity Score: 50 || Artist ID for reference,later 7B7TGqQe7QTVm2U6q8jzk1
Artist Name: Lil Reese || Artist Popularity Score: 50 || Artist ID for reference,later 1bPxKZtCdjB1aj1csBJpdS
Artist Name: Lil Keke || Artist Popularity Score: 48 || Artist ID for reference,later 1grI9x4Uzos1Asx8JmRW6T
Artist Name: Bow Wow || Artist Popularity Score: 57 || Artist ID for reference,later 7352aRY2mqSxBZwzUb6LmA
Artist Name: Lil Scrappy || Artist Popularity Score: 49 || Artist ID for reference,later 5einkgXXrjhfYCyac1FANB
Artist Name: Lil Wyte || Artist Popularity Score: 50 || Artist ID for reference,later 21O7WwRkik43ErKppxDKJq
Artist Name: Lil Blood || Artist Popularity Score: 45 || Artist ID for reference,later 74nSA5FdDOuuLw7Rn5JnuP
Artist Name: Lil Snupe || Artist Popularity Score: 45 || Artist ID for reference,later 42FaEHFfyxTdZQ5W28dXnj
Artist Name: Lil Mama || Artist Popularity Score: 45 || Artist ID for reference,later 5qK5bOC6wLtuLhG5KvU17c
Artist Name: Lil B || Artist Popularity Score: 44 || Artist ID for reference,later 4dqh62yIzDBmrMeBOLiP5F
Artist Name: Lil' Kim || Artist Popularity Score: 62 || Artist ID for reference,later 5tth2a3v0sWwV1C7bApBdX
Artist Name: Lil Cuete || Artist Popularity Score: 40 || Artist ID for reference,later 1I5u5Umau1AgHl0ZbPL1oR
Artist Name: Lil Phat || Artist Popularity Score: 39 || Artist ID for reference,later 3QnIBUOS4mUzs67rZ8r4c9
Artist Name: Lil Boom || Artist Popularity Score: 42 || Artist ID for reference,later 1mmlWsyPJvvxMdabcGJjRn
Artist Name: Lil Debbie || Artist Popularity Score: 43 || Artist ID for reference,later 3FNZcjyqT7F5upP99JV0oN
Artist Name: Lil Twist || Artist Popularity Score: 40 || Artist ID for reference,later 564gvOqSRcQoYAhaBpTiK2
Artist Name: Lil Trill || Artist Popularity Score: 37 || Artist ID for reference,later 5EQERGi7ffHvHsv3bnqzBn
Artist Name: Lil AJ || Artist Popularity Score: 37 || Artist ID for reference,later 2jXwYLNnCxNavms4mc1DYM
Artist Name: Lil Lonnie || Artist Popularity Score: 37 || Artist ID for reference,later 6zSBkdKFLKKggDtE3amfCk
Artist Name: Lil Twon || Artist Popularity Score: 37 || Artist ID for reference,later 5YZZbPdI7P7te3lW3dTpzK
Artist Name: Lil Goofy || Artist Popularity Score: 35 || Artist ID for reference,later 3rWaFjgOi5mjQfllMfN3VI
Artist Name: Lil Haiti || Artist Popularity Score: 37 || Artist ID for reference,later 4E9dumwOMLlTyXUp1i2WdI
Artist Name: Lil Cray || Artist Popularity Score: 35 || Artist ID for reference,later 43BqexhEx5NKF7VfeOYP9m
Artist Name: Mr. Lil One || Artist Popularity Score: 36 || Artist ID for reference,later 6tslWi0BXiDdtChermDzkU
Artist Name: Lil Flash || Artist Popularity Score: 38 || Artist ID for reference,later 069qBEK34YGoX7nSIT74Eg
Artist Name: Lil Silva || Artist Popularity Score: 43 || Artist ID for reference,later 2Kv0ApBohrL213X9avMrEn
Artist Name: Lil Yase || Artist Popularity Score: 34 || Artist ID for reference,later 4vIlHBnzWKbmWe8ZOkT1ZT
Artist Name: Lil Eddie || Artist Popularity Score: 41 || Artist ID for reference,later 5CY0QKsbUBpQJIE2yycsYi
Artist Name: Lil Rue || Artist Popularity Score: 34 || Artist ID for reference,later 4IFVaKBbEO8Qkurg6nmoc4
Artist Name: Lil Kesh || Artist Popularity Score: 39 || Artist ID for reference,later 38XiDu0kK3Z5jdHUDqBzNT
Artist Name: Lil Suzy || Artist Popularity Score: 34 || Artist ID for reference,later 5HPsVk1MblCoa44WLJsQwN
Artist Name: Lil Wayne, DJ Drama || Artist Popularity Score: 35 || Artist ID for reference,later 65npPa1U4cgobX9wU7Jgpb
Artist Name: Lil Mouse || Artist Popularity Score: 34 || Artist ID for reference,later 1cEHxCgGlEgqBc91YOcAEQ
Artist Name: Lil C || Artist Popularity Score: 33 || Artist ID for reference,later 69swdLSkKxCQBMYJ55O2mA
Artist Name: Lil Rick || Artist Popularity Score: 39 || Artist ID for reference,later 1qKzKUnuQsjB83hBZffoq0
Artist Name: Lil June || Artist Popularity Score: 32 || Artist ID for reference,later 3GH3KD2078kLPpEkN1UN26
Artist Name: Lil E || Artist Popularity Score: 34 || Artist ID for reference,later 0zn6yzsbWj3EPMgOTqfG5k
Artist Name: Lil Fate || Artist Popularity Score: 34 || Artist ID for reference,later 6JUnsP7jmvYmdhbg7lTMQj
Artist Name: Lil' Flip || Artist Popularity Score: 50 || Artist ID for reference,later 4Q5sPmM8j4SpMqL4UA1DtS

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 [6]:
for artist in lil_artists:
        print("Artist Name:",artist['name'])
        if not artist['genres']==[]:
            print("Artist Genres:",",".join(artist['genres']))
        else:
            print("No genres listed.")


Artist Name: Lil Wayne
Artist Genres: dirty south rap,pop rap,southern hip hop,trap music
Artist Name: Lil Yachty
No genres listed.
Artist Name: Lil Uzi Vert
No genres listed.
Artist Name: Lil Dicky
No genres listed.
Artist Name: Boosie Badazz
No genres listed.
Artist Name: Lil Jon
Artist Genres: crunk,dirty south rap,southern hip hop
Artist Name: King Lil G
No genres listed.
Artist Name: Lil Durk
No genres listed.
Artist Name: Lil Jon & The East Side Boyz
No genres listed.
Artist Name: Lil Bibby
No genres listed.
Artist Name: G Herbo
No genres listed.
Artist Name: Lil Rob
Artist Genres: chicano rap,latin hip hop
Artist Name: Lil Reese
No genres listed.
Artist Name: Lil Keke
No genres listed.
Artist Name: Bow Wow
Artist Genres: hip pop,pop rap
Artist Name: Lil Scrappy
Artist Genres: crunk,dirty south rap,southern hip hop,trap music
Artist Name: Lil Wyte
Artist Genres: juggalo
Artist Name: Lil Blood
No genres listed.
Artist Name: Lil Snupe
No genres listed.
Artist Name: Lil Mama
Artist Genres: hip pop
Artist Name: Lil B
No genres listed.
Artist Name: Lil' Kim
Artist Genres: hip pop
Artist Name: Lil Cuete
Artist Genres: chicano rap
Artist Name: Lil Phat
No genres listed.
Artist Name: Lil Boom
No genres listed.
Artist Name: Lil Debbie
No genres listed.
Artist Name: Lil Twist
Artist Genres: jerk
Artist Name: Lil Trill
Artist Genres: deep trap
Artist Name: Lil AJ
No genres listed.
Artist Name: Lil Lonnie
No genres listed.
Artist Name: Lil Twon
No genres listed.
Artist Name: Lil Goofy
No genres listed.
Artist Name: Lil Haiti
No genres listed.
Artist Name: Lil Cray
No genres listed.
Artist Name: Mr. Lil One
Artist Genres: chicano rap
Artist Name: Lil Flash
No genres listed.
Artist Name: Lil Silva
No genres listed.
Artist Name: Lil Yase
No genres listed.
Artist Name: Lil Eddie
No genres listed.
Artist Name: Lil Rue
No genres listed.
Artist Name: Lil Kesh
No genres listed.
Artist Name: Lil Suzy
Artist Genres: freestyle
Artist Name: Lil Wayne, DJ Drama
No genres listed.
Artist Name: Lil Mouse
No genres listed.
Artist Name: Lil C
No genres listed.
Artist Name: Lil Rick
Artist Genres: soca
Artist Name: Lil June
No genres listed.
Artist Name: Lil E
No genres listed.
Artist Name: Lil Fate
No genres listed.
Artist Name: Lil' Flip
Artist Genres: crunk,dirty south rap

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 [24]:
artistpopularity=0
for artist in lil_artists:
    if not artist['name']=='Lil Wayne':
        if artist['popularity'] > artistpopularity:
            artistpopularity=artist['popularity']
            artistname=artist['name']
print("Artist Name:",artistname, "|| Popularity Score:",artistpopularity)


Artist Name: Lil Uzi Vert || Popularity Score: 73

In [9]:
artistfollowers=0
for artist in lil_artists:
    if not artist['name']=='Lil Wayne':
        if artist['followers']['total'] > artistfollowers:
            artistfollowers=artist['followers']['total']
            artistname=artist['name']
print(artistfollowers)
print(artistname)


256564
Lil Jon

4) Print a list of Lil's that are more popular than Lil' Kim.


In [26]:
kimpopularity=0
for artist in lil_artists:
    if artist['name']=="Lil' Kim":
        kimpopularity=artist['popularity']
for artist in lil_artists:
    if kimpopularity < artist['popularity']:
        print("Artist Name:",artist['name'], "Popularity Score:",artist['popularity'])


Artist Name: Lil Wayne Popularity Score: 86
Artist Name: Lil Yachty Popularity Score: 72
Artist Name: Lil Uzi Vert Popularity Score: 73
Artist Name: Lil Dicky Popularity Score: 68
Artist Name: Boosie Badazz Popularity Score: 67
Artist Name: Lil Jon Popularity Score: 72

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 [27]:
artist_id = "0zn6yzsbWj3EPMgOTqfG5k"

response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_e = data['tracks']
for track in tracks_e:
    print(track['name'])


BBE Challenge
Certified Freak (feat. Beatking)
Solid (feat. D Bo & Kevin Gates)
Dat $tick (feat. Yung Navo)
Thugged Out (feat. Ralph)
Skrilla
Street Life (feat. Kevin Gates)
I Deserve Ya
Fuckin' Dat (feat. D Bo)
Young Wild and Reckless

In [28]:
artist_id = "7352aRY2mqSxBZwzUb6LmA"

response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_bowwow = data['tracks']
for track in tracks_bowwow:
    print(track['name'])


Shortie Like Mine
Outta My System
Let Me Hold You
Like You
Basketball
Ain't Thinkin' 'Bout You
Fresh Az I'm Iz
Girlfriend - Explicit Album Version
You Can Get It All - Album Crossfade Version
Sweat

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?


In [11]:
artist_id = "0zn6yzsbWj3EPMgOTqfG5k"
popularity_of_explicit_e=0
popularity_of_clean_e=0
c_count=0
e_count=0
e_time=0
c_time=0
response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_e = data['tracks']
for track in tracks_e:
   
    if track['explicit']==True:
        popularity_of_explicit_e= popularity_of_explicit_e+track['popularity']
        e_count=e_count+1
        e_time=e_time+track['duration_ms']
    else:
        popularity_of_clean_e= popularity_of_clean_e+track['popularity']
        c_count=c_count+1
        c_time=c_time+track['duration_ms']
print("The Average Popularity of Explicit Songs by the Artist Lil E is",popularity_of_explicit_e/e_count)
print("The minutes of Explicit Songs in their album",(e_time/1000)/60,"minutes")
print("The Average Popularity of Clean Songs by the Artist Lil E is",popularity_of_clean_e/c_count)
print("The minutes of Explicit Songs in their album",(c_time/1000)/60,"minutes")


The Average Popularity of Explicit Songs by the Artist Lil E is 6.285714285714286
The minutes of Explicit Songs in their album 25.386766666666666 minutes
The Average Popularity of Clean Songs by the Artist Lil E is 17.0
The minutes of Explicit Songs in their album 7.264783333333333 minutes

In [12]:
artist_id = "7352aRY2mqSxBZwzUb6LmA"
popularity_of_explicit_bowwow=0
popularity_of_clean_bowwow=0
c_count=0
e_count=0

response = requests.get("https://api.spotify.com/v1/artists/" + artist_id + "/top-tracks?country=US")
data = response.json()
tracks_bowwow = data['tracks']
for track in tracks_bowwow:
    if track['explicit']==True:
        popularity_of_explicit_e= popularity_of_explicit_e+track['popularity']
        e_count=e_count+1
        e_time=e_time+track['duration_ms']
    else:
        popularity_of_clean_e= popularity_of_clean_e+track['popularity']
        c_count=c_count+1
        c_time=c_time+track['duration_ms']
print("The Average Popularity of Explicit Songs by the Artist Lil Bow Wow is",popularity_of_explicit_e/e_count)
print("The minutes of Explicit Songs in their album",(e_time/1000)/60,"minutes")
print("The Average Popularity of Clean Songs by the Artist Lil Bow Wow is",popularity_of_clean_e/c_count)
print("The minutes of Explicit Songs in their album",(c_time/1000)/60,"minutes")


The Average Popularity of Explicit Songs by the Artist Lil Bow Wow is 56.333333333333336
The minutes of Explicit Songs in their album 38.86073333333333 minutes
The Average Popularity of Clean Songs by the Artist Lil Bow Wow is 56.0
The minutes of Explicit Songs in their album 34.66851666666666 minutes

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 [64]:
biggie_artist_count = 0
offset_value = 0
for page in range(0, 100):
    biggie_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&offset=' + str(offset_value) + '')
    biggie_data = biggie_response.json()
    biggie_artists = biggie_data['artists']['items']

    for artist in biggie_artists:
        biggie_artist_count = biggie_artist_count + 1
    offset_value = offset_value + 50
print("There are in total", biggie_artist_count, "Biggies")

lil_artist_count = 0
offset_value = 0
for page in range(0, 100):
    lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&offset=' + str(offset_value) + '')
    lil_data = lil_response.json()
    lil_artists = lil_data['artists']['items']

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


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

8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?


In [17]:
import requests
big_response = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&type=track&market=US&limit=50')
lil_response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&type=track&market=US&limit=50')
lil_data = lil_response.json()
big_data = big_response.json()
lil_count=0
lil_popularity=0
big_count=0
big_popularity=0
lil_artists = lil_data['artists']['items']
big_artists = big_data['artists']['items']

for count in lil_artists:
    lil_popularity=lil_popularity+count['popularity']
    
for count in big_artists:
    big_popularity=big_popularity+count['popularity']    

print ("Average Popularity of Lil's is",lil_popularity/50)
print ("Average Popularity of Biggies's is",big_popularity/50)

if lil_popularity/50>big_popularity/50:
    print("Lils are more popular.")
else:
    print("Biggies are more popular.")


Average Popularity of Lil's is 46.42
Average Popularity of Biggies's is 3.86
Lils are more popular.