Forked from nicholas-gh/create-spotify-playlist-from-csv-album-list.py
Created
November 8, 2020 10:45
-
-
Save d-schmidt/013dc35842ab9154b387e0984105c381 to your computer and use it in GitHub Desktop.
This file contains 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