In [1]:
!pip3 install requests
graded = 8/8
In [2]:
import requests
response = requests.get('https://api.spotify.com/v1/search?q=Lil&type=artist&market=US&limit=50')
Lil = response.json()
print(Lil.keys())
In [3]:
print(type(Lil['artists']))
In [4]:
print(Lil['artists'].keys())
In [5]:
Lil_info = Lil['artists']['items']
In [6]:
print(type(Lil_info))
In [7]:
print(Lil_info[1])
In [8]:
for every_lil in Lil_info:
print(every_lil['name'], every_lil['popularity'],"Genre:",(", ".join(every_lil['genres'])))
if every_lil['genres'] == []:
print("No genres listed")
In [13]:
genres = []
for item in Lil_info:
genres = item['genres'] + genres
print(genres)
unique_genres = set(genres)
for every_genre in unique_genres:
print(every_genre , genres.count(every_genre))
#genre_name = set(every_genre)
#genre_number = set(genres.count(every_genre))
#print(genre_name)
In [12]:
#this is the other option I did it with soma's help
from collections import Counter
# genres is a long list of genres with a lot repeats
genre_counter = Counter(genres)
genre_counter
counts = Counter(all_genres)
counts.most_common(1)
Out[12]:
In [ ]:
genre_counter.most_common(3)
In [ ]:
#class review. Other option:
In [11]:
#aggregation problem
all_genres = []
for artist in Lil_info:
#conditional
print('All genres we have', all_genres)
print("Current artist has", artist['genres'])
all_genres = all_genres + artist['genres']
print('!!!!All the genres we have are:')
print(all_genres)
all_genres.count("hip pop")
Out[11]:
In [14]:
unique_genre = set(all_genres)
for genre in unique_genres:
genre_count = all_genres.count(genre)
print(genre, "shows up", genre_count, "times")
#we have to convert it into a set
In [15]:
most_popular = 0
artist_name = Lil
for artist_popularity in Lil_info:
if artist_popularity['popularity'] == 86:
most_popular = most_popular
artist_name = artist_name
elif artist_popularity['popularity'] > most_popular:
most_popular = artist_popularity['popularity']
artist_name = artist_popularity['name']
print("Our second most popular artist is", artist_name , "with a popularity score of", most_popular)
In [16]:
most_followers = 0
artist_name = Lil
for artist_followers in Lil_info:
if artist_followers['followers']['total'] > 0:
most_followers = artist_followers['followers']['total']
artist_name = artist_followers['name']
else:
most_followers = most_followers
artist_name = artist_name
print("Our artist with the most followers is not the most popular, it is", artist_name , "with ", most_followers, "followers")
In [17]:
#class review. Soma used ---> artist['name'] != 'Lil Wayne':
In [18]:
second_most_popular_name = ""
second_most_popular_score = 0
for artist in Lil_info:
#this is the conditional
if artist['popularity'] > second_most_popular_score and artist['name'] != 'Lil Wayne':
#these are the changes
second_most_popular_name = artist['name']
second_most_popular_score = artist['popularity']
print(second_most_popular_name,second_most_popular_score)
In [19]:
#class review. he answered the question of what to do if two people have the same popularity scores
In [20]:
target_score = 72
#initial condition
second_best_artist = []
for artist in Lil_info:
print("Looking at", artist['name'], "who has a popularity of", artist['popularity'])
#conditional
if artist['popularity'] == 72:
#change, add new artist to the list where Lil Yatchy is already in. We do that with .append(new thing)
print("!!!!!!!the artist popularity is 72")
second_best_artist.append(artist['name'])
print("Our second best artists are:")
for artist in second_best_artist:
print(artist)
In [21]:
for artist in Lil_info:
#print("Looking at", artist['name'])
if artist['name'] == "Lil' Kim":
print("Found Lil' Kim")
print(artist['popularity'])
else:
pass #to keep it from breaking
#print("NOT Lil' Kim)
In [22]:
#aggregation problem
more_popular_lil_kim = []
#the loop
for artist in Lil_info:
#the conditional if artist is more popular than lil kim
if artist['popularity'] > 62:
print(artist['name'],"is more popular than Lil' Kim with a score of", artist['popularity'])
more_popular_lil_kim.append(artist['name'])
print(more_popular_lil_kim)
In [23]:
for artist_name in more_popular_lil_kim:
print(artist_name)
more_popular_string = ", ".join(more_popular_lil_kim)
print("artists more popular than lil kim are:", more_popular_string)
In [24]:
for two_lil in Lil_info:
print(two_lil['name'], "ID:", two_lil['id'])
In [25]:
import requests
response = requests.get('https://api.spotify.com/v1/artists/6JUnsP7jmvYmdhbg7lTMQj/top-tracks?country=US')
lil_fate = response.json()
print(lil_fate)
In [26]:
print(type(lil_fate))
In [27]:
print(lil_fate.keys())
In [28]:
print(type(lil_fate['tracks']))
In [29]:
lil_fate_top_tracks = lil_fate['tracks']
In [30]:
print(lil_fate_top_tracks)
In [31]:
for top_tracks in lil_fate_top_tracks:
print("Lil Fate top track is/are", top_tracks['name'])
In [32]:
import requests
response = requests.get('https://api.spotify.com/v1/artists/6L3x3if9RVimruryD9LoFb/top-tracks?country=US')
king_lil = response.json()
print(type(king_lil))
In [33]:
print(king_lil.keys())
In [34]:
king_lil_top_tracks = king_lil['tracks']
print(king_lil_top_tracks)
In [35]:
print("King Lil top track is/are:")
for top_tracks in king_lil_top_tracks:
print(top_tracks['name'])
In [36]:
for top_tracks in lil_fate_top_tracks:
print("Lil Fate top track is/are:")
print("-",top_tracks['name'])
print("King Lil top track is/are:")
for top_tracks in king_lil_top_tracks:
print("-",top_tracks['name'])
In [37]:
import requests
response = requests.get('https://api.spotify.com/v1/artists/6JUnsP7jmvYmdhbg7lTMQj/top-tracks?country=US')
lil_fate = response.json()
explicit_count = 0
non_explicit_count = 0
popularity_explicit = 0
popularity_non_explicit= 0
minutes_explicit = 0
minutes_non_explicit = 0
for track in lil_fate_top_tracks:
if track['explicit'] == True:
explicit_count = explicit_count + 1
popularity_explicit = popularity_explicit + track['popularity']
minutes_explicit = minutes_explicit + track['duration_ms']
print("There is", explicit_count, "explicit track with a popularity of", popularity_explicit)
elif track['explicit'] == False:
non_explicit_count = non_explicit_count + 1
popularity_non_explicit = popularity_non_explicit + track['popularity']
minutes_non_explicit = minutes_non_explicit + track['duration_ms']
print("There is", non_explicit_count ,"non-explicit track with a popularity of", popularity_non_explicit)
print("The average popularity of Lil Fate explicits songs is", popularity_explicit/explicit_count)
#Code below--> dividing it by zero because there are no non-explicit counts so it does not work
#print("The average popularity of Lil Fate non-explicits songs is", popularity_non_explicit/non_explicit_count)
print("Lil fate has", (minutes_explicit / 1000) /60 , "minutes of explicit music") #sounds weird. Not sure why I get this result
print("Lil fate has", (minutes_non_explicit / 1000) / 60, "minutes of non-explicit music")#sounds weird. Not sure why I get this result
In [38]:
import requests
response = requests.get('https://api.spotify.com/v1/artists/6L3x3if9RVimruryD9LoFb/top-tracks?country=US')
king_lil = response.json()
explicit_count = 0
non_explicit_count = 0
popularity_explicit = 0
popularity_non_explicit= 0
minutes_explicit = 0
minutes_non_explicit = 0
for track in king_lil_top_tracks:
if track['explicit'] == True:
explicit_count = explicit_count + 1
popularity_explicit = popularity_explicit + track['popularity']
minutes_explicit = minutes_explicit + track['duration_ms']
print("There is", explicit_count, "explicit track with a popularity of", popularity_explicit)
elif track['explicit'] == False:
non_explicit_count = non_explicit_count + 1
popularity_non_explicit = popularity_non_explicit + track['popularity']
minutes_non_explicit = minutes_non_explicit + track['duration_ms']
print("There is", non_explicit_count ,"non-explicit track with a popularity of", popularity_non_explicit)
print("The average popularity of King lil explicits songs is", popularity_explicit/explicit_count)
print("The average popularity of King lil non-explicits songs is", popularity_non_explicit/non_explicit_count)
print("Lil King has", (minutes_explicit / 1000) / 60, "minutes of explicit music") #this number does not make sense but not sure what is wrong
print("Lil King has", (minutes_non_explicit /1000) / 60, "minutes of non-explicit music")#sounds weird. Not sure why I get this result
In [39]:
response = requests.get('https://api.spotify.com/v1/search?q=Lil&type=artist&market=US')
all_lil = response.json()
print(all_lil.keys())
In [40]:
print(all_lil['artists'].keys())
In [41]:
print(all_lil['artists']['total'])
In [42]:
response = requests.get('https://api.spotify.com/v1/search?q=Biggie&type=artist&market=US')
all_biggies = response.json()
print(all_biggies['artists']['total'])
In [43]:
response = requests.get('https://api.spotify.com/v1/search?q=Lil&type=artist&market=US')
all_lil = response.json()
total_lils= all_lil['artists']['total']
#print(total_lils)
print("It would take",(total_lils/20) * 5, "to download all the Lils")
In [44]:
response = requests.get('https://api.spotify.com/v1/search?q=Biggie&type=artist&market=US')
all_biggies = response.json()
total_biggies= all_biggies['artists']['total']
#print(total_biggies)
print("It would take",(total_biggies/20) * 5, "to download all the Lils")
In [45]:
response = requests.get('https://api.spotify.com/v1/search?q=Biggie&type=artist&market=US&limit=50')
biggies = response.json()
print(biggies['artists'].keys())
In [46]:
print(biggies['artists']['items'][0])
In [47]:
import math
biggies_info = biggies['artists']['items']
biggie_total= 0
biggie_popularity= 0
for biggie in biggies_info:
biggie_total= biggie_total + 1
biggie_popularity = biggie_popularity + biggie['popularity']
print("The top 50 Biggies have an average score of popularity of", math.ceil(biggie_popularity / biggie_total))
In [48]:
import requests
response = requests.get('https://api.spotify.com/v1/search?q=Lil&type=artist&market=US&limit=50')
Lil = response.json()
In [49]:
import math
Lil_info = Lil['artists']['items']
lil_total= 0
lil_popularity= 0
for lil in Lil_info:
lil_total= lil_total + 1
lil_popularity = lil_popularity + lil['popularity']
print("The top 50 Lils have an average score of popularity of", math.ceil(lil_popularity / lil_total))
In [50]:
#This is the link to my graphic of the various popularities.
#https://infogr.am/fd7c85b9-f59b-498d-829d-a6ead4fb6862
In [ ]:
In [ ]: