Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-schmidt/013dc35842ab9154b387e0984105c381 to your computer and use it in GitHub Desktop.
Save d-schmidt/013dc35842ab9154b387e0984105c381 to your computer and use it in GitHub Desktop.
import csv
import requests
import glob
# Quick and dirty script to convert a CSV file of album names to a Spotify playlist
SEARCH = "https://api.spotify.com/v1/search"
ALBUMS = "https://api.spotify.com/v1/albums"
AUTH = "Bearer xxxxxxxx" # Fill in with OAUTH token
PLAYLIST = "https://api.spotify.com/v1/users/xxxx/playlists/xxxxxxxx/tracks" # fill in
for csvfilename in glob.glob('Music 2015 *.csv'):
print "Reading", csvfilename
with open() as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['Title'], row['Artist'])
r = requests.get(SEARCH,
headers={'Authorization': AUTH},
params={'type': 'album',
'q': 'artist:"%(Artist)s" album:"%(Title)s"' % row})
data = r.json()
if data['albums']['items']:
print "Found", data['albums']['items'][0]['id']
r = requests.get("%s/%s" % (ALBUMS, data['albums']['items'][0]['id']),
headers={'Authorization': AUTH})
tracks = ",".join(track['uri'] for track in r.json()['tracks']['items'])
r = requests.post(PLAYLIST,
headers={'Authorization': AUTH},
params={'uris': tracks})
print r
else:
print "Didn't find album"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment