Created
January 4, 2016 13:43
-
-
Save nicholas-gh/da2fe7de3e2d6551d726 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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