Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Glastonbury Sampler Playlist
FOO FIGHTERS
KANYE WEST
THE WHO
FLORENCE & THE MACHINE
PHARRELL WILLIAMS
PAUL WELLER
ALT-J
LIONEL RICHIE
MOTÖRHEAD
MARY J. BLIGE
ALABAMA SHAKES
PALOMA FAITH
PATTI SMITH
THE CHEMICAL BROTHERS
RUDIMENTAL
DEADMAU5
JAMIE T
BEN HOWARD
HOT CHIP
THE MOTHERSHIP RETURNS – GEORGE CLINTON, PARLIAMENT, FUNKADELIC & THE FAMILY STONE
FLYING LOTUS
FKA TWIGS
CARIBOU
ROY AYERS
RUN THE JEWELS
MARK RONSON
SUPER FURRY ANIMALS
BELLE & SEBASTIAN
THE VACCINES
RYAN ADAMS
CHRONIXX
FUTURE ISLANDS
GEORGE EZRA
THE WATERBOYS
THE MOODY BLUES
SUEDE
FFS (FRANZ FERDINAND & SPARKS)
THE MACCABEES
ENTER SHIKARI
MODESTEP
CLEAN BANDIT
JESSIE WARE
COURTNEY BARNETT
JON HOPKINS
SPIRITUALIZED
JAMIE XX
MAVIS STAPLES
HOZIER
FATHER JOHN MISTY
THE FALL
TODD TERJE & THE OLSENS
GREGORY PORTER
JUNGLE
SLEAFORD MODS
CATFISH & THE BOTTLEMEN
AZEALIA BANKS
YOUNG FATHERS
CHARLI XCX
KASAI ALLSTARS
SHARON VAN ETTEN
KATE TEMPEST
GOAT
WOLF ALICE
PERFUME GENIUS
FAT WHITE FAMILY
YEARS & YEARS
LA ROUX
DEATH CAB FOR CUTIE
LIANNE LA HAVAS
DEATH FROM ABOVE 1979
CIRCA WAVES
PEACE
THE POP GROUP
IBEYI
ELLA EYRE
RAE MORRIS
{
"metadata": {
"name": "",
"signature": "sha256:eda79d906c8326f0c1f1307e484ab316d9387e714348f6cf5d8450b5889d27f7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import spotipy\n",
"sp = spotipy.Spotify()\n",
"from pprint import pprint\n",
"import spotipy.util as util"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Get the lineup list\n",
"\n",
"artist_list_file = r'artists.txt'\n",
"with open(artist_list_file) as f:\n",
" artist_list = f.read().splitlines()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Eyeball the artist list\n",
"print(len(artist_list))\n",
"print(artist_list[0:10])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"76\n",
"['FOO FIGHTERS', 'KANYE WEST', 'THE WHO', 'FLORENCE & THE MACHINE', 'PHARRELL WILLIAMS', 'PAUL WELLER', 'ALT-J', 'LIONEL RICHIE', 'MOT\\xd6RHEAD', 'MARY J. BLIGE']\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Run the list through Spotify's artist search API - keep the top result each time\n",
"\n",
"spotify_artist_list = []\n",
"\n",
"for artist_name in artist_list:\n",
" artist_results = sp.search(q=artist_name, limit=1, type='artist')\n",
" if len(artist_results['artists']['items']) > 0:\n",
" spotify_artist_list.append(artist_results['artists']['items'][0])\n",
" else:\n",
" print('Couldn\\'t find {0}'.format(artist_name))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Couldn't find MOT\ufffdRHEAD\n",
"Couldn't find THE MOTHERSHIP RETURNS \ufffd GEORGE CLINTON, PARLIAMENT, FUNKADELIC & THE FAMILY STONE"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...1secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...2secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Couldn't find FFS (FRANZ FERDINAND & SPARKS)"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Couldn't find TODD TERJE & THE OLSENS"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...1secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...2secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...3secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...4secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Eyeball the spotify results \n",
"\n",
"[(artist['name'], artist['popularity']) for artist in spotify_artist_list][0:10]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"[(u'Foo Fighters', 85),\n",
" (u'Kanye West', 97),\n",
" (u'The Who', 76),\n",
" (u'Florence + The Machine', 86),\n",
" (u'Pharrell Williams', 87),\n",
" (u'Paul Weller', 64),\n",
" (u'alt-J', 85),\n",
" (u'Lionel Richie', 73),\n",
" (u'Mary J. Blige', 77),\n",
" (u'Alabama Shakes', 81)]"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Optionally, sort the list by decreasing popularity\n",
"\n",
"spotify_artist_list = sorted(spotify_artist_list, key=lambda artist: artist['popularity'], reverse=True)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# For each artist, get the top 3 tracks and put them in spotify_track_list\n",
"\n",
"spotify_track_list = []\n",
"\n",
"for artist in spotify_artist_list:\n",
"\n",
" track_results = sp.artist_top_tracks(artist['uri'])\n",
" \n",
" for track in track_results['tracks'][0:3]:\n",
" spotify_track_list.append(track)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"retrying ...1secs\n",
"retrying ...2secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"retrying ...3secs"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# As before, eyeball the tracks we've pulled down\n",
"\n",
"[(t['id'], t['name'], t['popularity']) for t in spotify_track_list][0:10]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 10,
"text": [
"[(u'7x8dCjCr0x6x2lXKujYD34', u'The Pretender', 71),\n",
" (u'5UWwZ5lm5PKu6eKsHAGxOk', u'Everlong', 69),\n",
" (u'3QmesrvdbPjwf7i40nht1D', u'Everlong - Acoustic Version', 69),\n",
" (u'78TTtXnFQPzwqlbtbwqN0y', u'FourFiveSeconds', 85),\n",
" (u'79reBHqpRoWXaQbjBBY3UC', u'All Day', 80),\n",
" (u'2Zb7wnGUnNPCas2E0wWSQ5', u'Ni**as In Paris', 74),\n",
" (u'02DurCgOvDdX0uKEjqcl3W', u'Who Are You', 60),\n",
" (u'3qiyyUfYe7CRYLucrPmulD', u\"Baba O'Riley\", 55),\n",
" (u'1W1GpfPujmgp2vQqcpUhtU', u\"Won't Get Fooled Again - Remix\", 59),\n",
" (u'64mfy9l7LTCdV07i28s5FZ', u'What Kind Of Man', 70)]"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Set up the user connection so we can create playlists (you would need to put your own details in here for username->return_uri)\n",
"\n",
"scope = 'playlist-modify-public'\n",
"username = 'xxx'\n",
"client_id = 'xxx'\n",
"secret_key = 'xxx'\n",
"return_uri = 'xxx'"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Get the token - You need to click a link and paste an address back in\n",
"\n",
"token = util.prompt_for_user_token(username, scope, client_id, secret_key , return_uri)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Reconnect with the auth token\n",
"\n",
"sp = spotipy.Spotify(auth=token)\n",
"sp.trace = False"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Create the new playlist and get it's ID\n",
"\n",
"new_playlist = sp.user_playlist_create(username, 'Glastonbury 3 Track Sampler')\n",
"playlist_id = new_playlist['id']\n",
"print(playlist_id)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 40,
"text": [
"u'3nwGbBxWxzomtn9amcokHO'"
]
}
],
"prompt_number": 40
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# the 'user_playlist_add_tracks' function takes a list of track_ids - so make that with some list comprehension\n",
"\n",
"track_ids = [t['id'] for t in spotify_tracks]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 35
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(track_ids)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 38,
"text": [
"216"
]
}
],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# the 'user_playlist_add_tracks' only allows adding 100 tracks at a time - hence we loop in 100 item chunks:\n",
"\n",
"chunksize = 100\n",
"for chunk in [track_ids[i:i + chunksize] for i in range(0, len(track_ids), chunksize)]:\n",
" results = sp.user_playlist_add_tracks(username, playlist_id, chunk)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 43
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment