-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
31 lines (27 loc) · 1.15 KB
/
spotify.py
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
import spotipy
import spotipy.util as util
import spotipy.oauth2 as oauth
def Spotify(client_id, client_secret, redirect_uri, username, scope, auto=False):
spo = oauth.SpotifyOAuth(
client_id = client_id,
client_secret = client_secret,
redirect_uri = redirect_uri,
scope = scope
)
sp = spotipy.Spotify(auth_manager=spo)
return sp, spo
def refresh_token(spo):
cached_token = spo.get_cached_token()
refreshed_token = cached_token['refresh_token']
new_token = spo.refresh_access_token(refreshed_token)
# also we need to specifically pass `auth=new_token['access_token']`
sp = spotipy.Spotify(auth=new_token['access_token'])
return sp
def makePlaylist(sp, spo, title, track_ids, public = False):
try:
result_playlist = sp.user_playlist_create(sp.me()["id"], title, public=public)
except spotipy.client.SpotifyException:
sp = refresh_token(spo)
result_playlist = sp.user_playlist_create(sp.me()["id"], title, public=public)
sp.user_playlist_add_tracks(sp.me()["id"], result_playlist['id'], track_ids)
return result_playlist['id']