<jupyter_start><jupyter_text>Homework 05SpotifyGianna-<NAME>2016-06-07<jupyter_code>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()
lil_data['artists'].keys()
lil_artists = lil_data['artists']['items']
#check on what elements are in that list: 
#print (lil_artists[0])<jupyter_output><empty_output><jupyter_text>1) With "<NAME>" and "<NAME>" 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.<jupyter_code>for artist in lil_artists:
    print(artist['name'], "has a popularity score of", artist['popularity'])<jupyter_output><NAME> 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
<NAME> 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
<NAME> 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
L<NAME> has[...]<jupyter_text>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<jupyter_code>#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)
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<jupyter_output><NAME> 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.
<NAME> has a popularity score of 68 But there are no genres listed for this artist.
<NAME> has a popularity score of 67 But there are no genres listed for this artist.
<NAME> has a popularity score of 72 This artist is associated with crunk, dirty south rap, southern hip hop
<NAME> has a popularity score of 61 But there are no genres listed for this artist.
<NAME> has a popularity score of 60 But there are no genres listed for this artist.
<NAME> & The East Side Boyz has a popularity score of 60 But there are no genres listed for this artist.
<NAME> has a popularity score of 54 But there are no genres listed for this artist.
<NAME> has a popularity score of 53 But th[...]<jupyter_text>3) Use a for loop to determine who BESIDES <NAME> has the highest popularity rating.<jupyter_code>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)<jupyter_output><NAME> is the most popular, with a rating of 86
<NAME> is the second most popular with a popularity rating of 72 compared to Lil Wayne who has a rating of 86
<NAME> is the second most popular with a popularity rating of 73 compared to Lil Wayne who has a rating of 86<jupyter_text>Is it the same artist who has the largest number of followers?<jupyter_code>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'])<jupyter_output><NAME> has the largest number followers: 2627171
The second most popular Lils have the following amount of followers:
Lil Uzi Vert 58466<jupyter_text>4) Print a list of Lil's that are more popular than Lil' Kim.<jupyter_code>kim_popularity = 0
for artist in lil_artists:
    if artist['name'] == "<NAME>":
        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.")<jupyter_output><NAME> has a popularity of 86 which is higher than that of Lil' Kim.
L<NAME> 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.
<NAME> has a popularity of 68 which is higher than that of Lil' Kim.
<NAME> 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.<jupyter_text>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.<jupyter_code>#for artist in lil_artists:
    #print(artist['name'], artist['id'])
#<NAME> 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'])

#<NAME> 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'])<jupyter_output>THESE ARE THE TOP TRACKS OF LIL DICKY:
$ave Dat Money (feat. Fetty Wap & <NAME>an)
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
<NAME>
Turn Down for What - Official Remix
Culo
Get Loose
The Anthem
Prison Riot
Outta Your Mind<jupyter_text>6) Will the world explode if a musicians swears? Get an average popularity for their explicit songs vs. their non-explicit songs.<jupyter_code>print(tracks_Dicky[0].keys())<jupyter_output>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'])<jupyter_text>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.<jupyter_code>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)<jupyter_output>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<jupyter_text>Soma explaining how to write functions in 30 seconds of Lab:<jupyter_code>#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)<jupyter_output>the sum of 5 and 7 is 12
the sum of 1 and 2 is 3
the sum of 4 and 55 is 59<jupyter_text>Based on that, I re-wrote my above code using a function<jupyter_code>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, "<NAME>")
average_popularity(tracks_Jon, "<NAME>")<jupyter_output>The average popularity of explicit songs by <NAME> is 62.1
There are no non-explicit songs by <NAME>
The average popularity of explicit songs by <NAME> is 55.142857142857146
The average popularity of non-explicit songs by <NAME> is 71.33333333333333<jupyter_text>How many minutes of explicit songs do they have? Non-explicit?<jupyter_code>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, "<NAME>")
explicit_minutes(tracks_Jon, "<NAME>")<jupyter_output><NAME> has 497.19283333333334 minutes of explicit music.
<NAME> has has no non-explicit music.
<NAME> has 246.20966666666666 minutes of explicit music.
<NAME> has 128.47083333333333 minutes of non-explicit music.<jupyter_text>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?<jupyter_code>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.")
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.")
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.")<jupyter_output>There are in total 50 Biggies.
There are in total 4513 Lils.<jupyter_text>8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?<jupyter_code># 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.")<jupyter_output>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.
{'fraction_non_alphanumeric': 0.09213470575072723, 'fraction_numerical': 0.0279145222644887, 'mean_word_length': 4.844066688460281, 'pattern_counts': {'":': 1, '<': 88, '<?xml version=': 0, '>': 93, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '288644', 'license': 'MIT', 'n_tokens_mistral': 6182, 'n_tokens_neox': 5670, 'n_words': 1961, 'path': 'foundations-homework/05/homework-05-gruen-spotify.ipynb', 'repo_name': 'gcgruen/foundations-homework', 'size': '18000'}