<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Homework 05
Spotify
Gianna-Carina Gruen
2016-06-07
Step1:  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.
Step2:  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
Step3:  3) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
Step4:  Is it the same artist who has the largest number of followers?
Step5:  4) Print a list of Lil's that are more popular than Lil' Kim.
Step6:  5) Pick two of your favorite Lils to fight it out, and use their IDs to print out their top tracks.
Tip
Step7:  6) Will the world explode if a musicians swears?
Get an average popularity for their explicit songs vs. their non-explicit songs.
Step8:  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.
Step9:  Soma explaining how to write functions in 30 seconds of Lab
Step10:  Based on that, I re-wrote my above code using a function
Step11:  How many minutes of explicit songs do they have? Non-explicit?
Step12:  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?
Step13:  8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
<ASSISTANT_TASK:>
Python 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])
Explanation: Homework 05
Spotify
Gianna-Carina Gruen
2016-06-07
End of explanation
for artist in lil_artists:
    print(artist['name'], "has a popularity score of", artist['popularity'])
Explanation: 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.
End of explanation
#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   
Explanation: 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
End of explanation
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)
    
    
Explanation: 3) Use a for loop to determine who BESIDES Lil Wayne has the highest popularity rating.
End of explanation
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'])
Explanation: Is it the same artist who has the largest number of followers?
End of explanation
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.")
Explanation: 4) Print a list of Lil's that are more popular than Lil' Kim.
End of explanation
#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'])
Explanation: 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.
End of explanation
print(tracks_Dicky[0].keys())
Explanation: 6) Will the world explode if a musicians swears?
Get an average popularity for their explicit songs vs. their non-explicit songs.
End of explanation
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)
Explanation: 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.
End of explanation
#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)
Explanation: Soma explaining how to write functions in 30 seconds of Lab:
End of explanation
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")
Explanation: Based on that, I re-wrote my above code using a function
End of explanation
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")
Explanation: How many minutes of explicit songs do they have? Non-explicit?
End of explanation
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.")
Explanation: 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?
End of explanation
# 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.")
Explanation: 8) Out of the top 50 "Lil"s and the top 50 "Biggie"s, who is more popular on average?
End of explanation
<END_TASK>