Graded = 6/8


In [1]:
import requests
smallresponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=50')


# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=50
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=100
# https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US&offset=150

# offset = 0
smalldata = response.json()

# total numer of artists 
total_artists = smalldata['artists']['total']
total_pages = total_artists/50

import math
page_count = math.ceil(total_pages)

print("total pages", page_count)

for page in range(page_count):
    offset = (page) * 50
    print("We are on page", page + 1 , "with an offset of", offset)
    # make the request with a changed ?offset={offset}
    # data = response.json()
    # add our new artists to our list of existing artists
    # all_artists = all_artists + data['artists]['items']
smalldata = response.json()



# print(smalldata['artists'])



# smalldata['artists']
# print(smalldata['artists']['items'])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-0740d4e1a3d7> in <module>()
      9 
     10 # offset = 0
---> 11 smalldata = response.json()
     12 
     13 # total numer of artists

NameError: name 'response' is not defined

In [2]:
#http://google.com/search ? q = cats & lang = US (without spaces)
# URL                      PARAMETER   MORE PARAMETERS

#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.

import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
lil_artists = data['artists']['items']
for artist in lil_artists:
    print(artist['name'], artist['popularity'])
# for artist in lil_artists:
    # print(artist['popularity'])


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

In [3]:
# 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


import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
lil_artists = data['artists']['items']
for artist in lil_artists:
    print(artist['name'], artist['popularity'])
    if len(artist['genres']) > 0:
            s = ', '
            # prints without brackets! woohoo -- unlike print(artists['genres'])
            print(s.join(artist['genres']))
            
    else: 
           print("No genres listed.")


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

In [4]:
all_genres = []

for artist in lil_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']
    
for item in all_genres:
    # repeats dirty south 4 times ! 
     print(item, "shows up", all_genres.count(item), "times.")


dirty south rap shows up 4 times.
pop rap shows up 2 times.
southern hip hop shows up 3 times.
trap music shows up 2 times.
crunk shows up 3 times.
dirty south rap shows up 4 times.
southern hip hop shows up 3 times.
chicano rap shows up 3 times.
latin hip hop shows up 1 times.
crunk shows up 3 times.
dirty south rap shows up 4 times.
southern hip hop shows up 3 times.
trap music shows up 2 times.
hip pop shows up 3 times.
pop rap shows up 2 times.
juggalo shows up 1 times.
hip pop shows up 3 times.
hip pop shows up 3 times.
chicano rap shows up 3 times.
jerk shows up 1 times.
deep trap shows up 1 times.
chicano rap shows up 3 times.
freestyle shows up 1 times.
soca shows up 1 times.
crunk shows up 3 times.
dirty south rap shows up 4 times.

In [48]:
#import collections -- imports entire library 
from collections import Counter # -- only imports Counter 



all_genres = []

for artist in lil_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']
    
    # collections.Counter
counts = Counter(all_genres) # counts the occurence of each object # it looks like a dictionary


counts.most_common(2)

counts.most_common(1)

counts.most_common(4)


Out[48]:
[('dirty south rap', 4),
 ('chicano rap', 3),
 ('southern hip hop', 3),
 ('crunk', 3)]

In [5]:
# 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?


import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)
max_popularity = 0;
lil_artists = data['artists']['items']
for artist in lil_artists:
    if not artist['name'] == 'Lil Wayne':
        # print(artist['name'], artist['popularity'])
        if artist['popularity'] > max_popularity:
          #  print("Now",artist['name'], "is the most popular.")
            max_popularity = artist['popularity']
            most_popular = artist['name']
            
print("The most popular artist (not including Lil Wayne) is", most_popular )


The most popular artist (not including Lil Wayne) is Lil Yachty

In [11]:
# lilkim_id = '5tth2a3v0sWwV1C7bApBdX'
# lilkimresponse = requests.get('https://api.spotify.com/v1/artists/' + lilkim_id)

# lilkimresponse.keys()

In [7]:
# 4) Print a list of Lil's that are more popular than Lil' Kim.


import requests
response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
data = response.json()
# print(data)

lil_artists = data['artists']['items']

lilkim_id = '5tth2a3v0sWwV1C7bApBdX'
lilkimresponse = requests.get('https://api.spotify.com/v1/artists/' + lilkim_id)
lilkimresponse = lilkimresponse.json()
# print(lilkimresponse.keys())

lilkim_pop = lilkimresponse['popularity']
morepopularthankim = 0;
print("Lil Kim has a popularity of", lilkim_pop)


for artist in lil_artists:
       # print(artist['name'], artist['popularity'], artist['id'])
        if artist['popularity'] > lilkim_pop:
           print(artist['name'], "is more popular than Lil Kim.")
           morepopularthankim = morepopularthankim + 1
            
print(morepopularthankim, "Lil artists are more popular than Lil Kim.")
            
print("The most popular artist (not including Lil Wayne) is", most_popular )


Lil Kim has a popularity of 62
Lil Wayne is more popular than Lil Kim.
Lil Yachty is more popular than Lil Kim.
Lil Uzi Vert is more popular than Lil Kim.
Lil Dicky is more popular than Lil Kim.
Boosie Badazz is more popular than Lil Kim.
Lil Jon is more popular than Lil Kim.
6 Lil artists are more popular than Lil Kim.
The most popular artist (not including Lil Wayne) is Lil Yachty

In [8]:
#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.

lil_jon_id = '7sfl4Xt5KmfyDs2T3SVSMK'

liljonresponse = requests.get('https://api.spotify.com/v1/artists/'+ lil_jon_id+'/top-tracks?country=US')
liljondata = liljonresponse.json()
#liljontracks = liljondata['items']['tracks']
# print(liljondata.keys())

# print(liljondata)

liljontracks = liljondata['tracks']

print('Lil Jon\'s top tracks are:')
for track in liljontracks:
    print(track['name'])
# print(type(liljondata['items']))


print('Lil Wayne\'s top tracks are:')
lil_wayne_id = '55Aa2cqylxrFIXC767Z865'
lilwayneresponse = requests.get('https://api.spotify.com/v1/artists/'+ lil_wayne_id+'/top-tracks?country=US')
lilwaynedata = lilwayneresponse.json()

lilwaynetracks = lilwaynedata['tracks']

for track in lilwaynetracks:
    print(track['name'])
#lilwayne = lilwaynedata['tracks']


Lil Jon's top tracks are:
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
Lil Wayne's top tracks are:
Forever
Nothing But Trouble - Instagram Models
6 Foot 7 Foot
A Milli
Right Above It
Lollipop
Rich As Fuck
Love Me
Mirror
Believe Me

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 [9]:
lj_clean_popularity = 0;
clean_count = 0;

lj_explicit_popularity = 0;
explicit_count =0;

for track in liljontracks:
    print(track['name'])
    if track['explicit']:
        print("this track is explicit")
        explicit_count = explicit_count + 1;
        lj_explicit_popularity = lj_explicit_popularity + track['popularity']
    else:
        print("this track is clean")
        clean_count = clean_count + 1;
        lj_clean_popularity = lj_clean_popularity + track['popularity']
            
print("Overall the clean popularity for Lil Jon was",lj_clean_popularity/clean_count,"and the explicit popularity was",lj_explicit_popularity/explicit_count)


Turn Down for What
this track is clean
Gasolina - DJ Buddah Remix
this track is clean
Snap Yo Fingers
this track is clean
Bend Ova
this track is explicit
Turn Down for What - Official Remix
this track is explicit
Culo
this track is explicit
Get Loose
this track is explicit
The Anthem
this track is explicit
Prison Riot
this track is explicit
Outta Your Mind
this track is explicit
Overall the clean popularity for Lil Jon was 70.66666666666667 and the explicit popularity was 54.857142857142854

In [10]:
lw_clean_popularity = 0;
clean_count = 0;

lw_explicit_popularity = 0;
explicit_count =0;

for track in lilwaynetracks:
    print(track['name'])
    if track['explicit']:
        print("this track is explicit")
        explicit_count = explicit_count + 1;
        lw_explicit_popularity = lw_explicit_popularity + track['popularity']
    else:
        print("this track is clean")
        clean_count = clean_count + 1;
        lw_clean_popularity = lw_clean_popularity + track['popularity']

if clean_count > 1 and explicit_count > 1:
    print("Overall the clean popularity for Lil Wayne was",lw_clean_popularity/clean_count,"and the explicit popularity was",lw_explicit_popularity/explicit_count)
elif clean_count == 0 and explicit_count >1:
    print("Overall Lil wayne had zero clean songs and the explicit popularity was",lw_explicit_popularity/explicit_count, '%')


Forever
this track is explicit
Nothing But Trouble - Instagram Models
this track is explicit
6 Foot 7 Foot
this track is explicit
A Milli
this track is explicit
Right Above It
this track is explicit
Lollipop
this track is explicit
Rich As Fuck
this track is explicit
Love Me
this track is explicit
Mirror
this track is explicit
Believe Me
this track is explicit
Overall Lil wayne had zero clean songs and the explicit popularity was 68.8 %

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 [11]:
#TA-Stephan: Should have two different answers. 

import requests
biggieResponse = requests.get('https://api.spotify.com/v1/search?query=biggie&type=artist&limit=50&market=US')
biggieData = biggieResponse.json()
# print(biggieData.keys())
biggie_count = 0
# print(len(biggieData['artists']))

for artist in biggieData['artists']['items']:
    biggie_count = biggie_count + 1

print("There are",biggie_count,"Biggie artists.")


lilResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
lilData = lilResponse.json()

# print(lilData['artists'].keys())

lil_count = 0

# print(lilData['artists'])
for artist in lilData['artists']['items']:
    lil_count = lil_count + 1

print("There are",lil_count,"lil artists.")

print("It would take", biggie_count,"seconds to get all of the data about Biggie artists and", lil_count,"seconds to get all the information about lil artists")


There are 50 Biggie artists.
There are 50 lil artists.
It would take 50 seconds to get all of the data about Biggie artists and 50 seconds to get all the information about lil artists

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


In [134]:
#Ta-Stephan: Should have had compared Biggies and Lils, not the most popular of them all
import requests
lilResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
lilData = lilResponse.json()
# print(data)
max_popularity = 0;
lil_artists = data['artists']['items']
for artist in lil_artists:
        # print(artist['name'], artist['popularity'])
        if artist['popularity'] > max_popularity:
            max_popularity = artist['popularity']
            most_popular = artist['name']

import requests
bigResponse = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
bigData = bigResponse.json()
# print(data)
big_artists = data['artists']['items']
for artist in big_artists:
        if artist['popularity'] > max_popularity:
            max_popularity = artist['popularity']
            most_popular = artist['name']
print("The most popular artist of all of these artists is", most_popular)


The most popular artist of all of these artists is Lil Wayne

In [ ]: