import spotipy
sp = spotipy.Spotify()
from pprint import pprint
import spotipy.util as util
# Get the lineup list
artist_list_file = r'artists.txt'
with open(artist_list_file) as f:
artist_list = f.read().splitlines()
# Eyeball the artist list
print(len(artist_list))
print(artist_list[0:10])
76 ['FOO FIGHTERS', 'KANYE WEST', 'THE WHO', 'FLORENCE & THE MACHINE', 'PHARRELL WILLIAMS', 'PAUL WELLER', 'ALT-J', 'LIONEL RICHIE', 'MOT\xd6RHEAD', 'MARY J. BLIGE']
# Run the list through Spotify's artist search API - keep the top result each time
spotify_artist_list = []
for artist_name in artist_list:
artist_results = sp.search(q=artist_name, limit=1, type='artist')
if len(artist_results['artists']['items']) > 0:
spotify_artist_list.append(artist_results['artists']['items'][0])
else:
print('Couldn\'t find {0}'.format(artist_name))
Couldn't find MOT�RHEAD Couldn't find THE MOTHERSHIP RETURNS � GEORGE CLINTON, PARLIAMENT, FUNKADELIC & THE FAMILY STONE retrying ...1secs retrying ...2secs Couldn't find FFS (FRANZ FERDINAND & SPARKS) Couldn't find TODD TERJE & THE OLSENS retrying ...1secs retrying ...2secs retrying ...3secs retrying ...4secs
# Eyeball the spotify results
[(artist['name'], artist['popularity']) for artist in spotify_artist_list][0:10]
[(u'Foo Fighters', 85), (u'Kanye West', 97), (u'The Who', 76), (u'Florence + The Machine', 86), (u'Pharrell Williams', 87), (u'Paul Weller', 64), (u'alt-J', 85), (u'Lionel Richie', 73), (u'Mary J. Blige', 77), (u'Alabama Shakes', 81)]
# Optionally, sort the list by decreasing popularity
spotify_artist_list = sorted(spotify_artist_list, key=lambda artist: artist['popularity'], reverse=True)
# For each artist, get the top 3 tracks and put them in spotify_track_list
spotify_track_list = []
for artist in spotify_artist_list:
track_results = sp.artist_top_tracks(artist['uri'])
for track in track_results['tracks'][0:3]:
spotify_track_list.append(track)
retrying ...1secs retrying ...2secs retrying ...3secs
# As before, eyeball the tracks we've pulled down
[(t['id'], t['name'], t['popularity']) for t in spotify_track_list][0:10]
[(u'7x8dCjCr0x6x2lXKujYD34', u'The Pretender', 71), (u'5UWwZ5lm5PKu6eKsHAGxOk', u'Everlong', 69), (u'3QmesrvdbPjwf7i40nht1D', u'Everlong - Acoustic Version', 69), (u'78TTtXnFQPzwqlbtbwqN0y', u'FourFiveSeconds', 85), (u'79reBHqpRoWXaQbjBBY3UC', u'All Day', 80), (u'2Zb7wnGUnNPCas2E0wWSQ5', u'Ni**as In Paris', 74), (u'02DurCgOvDdX0uKEjqcl3W', u'Who Are You', 60), (u'3qiyyUfYe7CRYLucrPmulD', u"Baba O'Riley", 55), (u'1W1GpfPujmgp2vQqcpUhtU', u"Won't Get Fooled Again - Remix", 59), (u'64mfy9l7LTCdV07i28s5FZ', u'What Kind Of Man', 70)]
# Set up the user connection so we can create playlists (you would need to put your own details in here for username->return_uri)
scope = 'playlist-modify-public'
username = 'xxx'
client_id = 'xxx'
secret_key = 'xxx'
return_uri = 'xxx'
# Get the token - You need to click a link and paste an address back in
token = util.prompt_for_user_token(username, scope, client_id, secret_key , return_uri)
# Reconnect with the auth token
sp = spotipy.Spotify(auth=token)
sp.trace = False
# Create the new playlist and get it's ID
new_playlist = sp.user_playlist_create(username, 'Glastonbury 3 Track Sampler')
playlist_id = new_playlist['id']
print(playlist_id)
u'3nwGbBxWxzomtn9amcokHO'
# the 'user_playlist_add_tracks' function takes a list of track_ids - so make that with some list comprehension
track_ids = [t['id'] for t in spotify_tracks]
len(track_ids)
216
# the 'user_playlist_add_tracks' only allows adding 100 tracks at a time - hence we loop in 100 item chunks:
chunksize = 100
for chunk in [track_ids[i:i + chunksize] for i in range(0, len(track_ids), chunksize)]:
results = sp.user_playlist_add_tracks(username, playlist_id, chunk)