-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathSpotify playlist generator.py
More file actions
38 lines (36 loc) · 1.42 KB
/
Spotify playlist generator.py
File metadata and controls
38 lines (36 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import os
import spotipy
import spotipy.util as util
from random import randint
#Enviromental variables
os.environ["SPOTIPY_CLIENT_ID"] = ""
os.environ["SPOTIPY_CLIENT_SECRET"] = ""
os.environ["SPOTIPY_REDIRECT_URI"] = "http://localhost/"
#The scopes that we need for read and write.
scope = 'user-library-read playlist-modify-public'
#We receive username when running the script, also you can set it manually.
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print ("Usage: %s username") % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope)
tracks=[]
if token:
sp = spotipy.Spotify(auth=token)
#The number in range, is the number of songs that you want in your playlist
for a in range(50):
#'library_size' represents the number of songs that you have in your library (not necesary the exact amount).
library_size=50
ran=randint(0,library_size)
results = sp.current_user_saved_tracks(limit=1, offset=ran)
for item in results['items']:
track = item['track']
tracks.append(track['id'])
#print (str(a+1)+".- "+track['id']+" "+str(track['name'].encode('ascii', 'ignore')) + ' - ' + str(track['artists'][0]['name'].encode('ascii', 'ignore')))
sp.trace = False
results = sp.user_playlist_replace_tracks(username, 'playlist_id', tracks)
print (results)
else:
print ("Can't get token for", username)