-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotipy_utils.py
More file actions
61 lines (44 loc) · 1.47 KB
/
spotipy_utils.py
File metadata and controls
61 lines (44 loc) · 1.47 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import spotipy
import spotipy.util as util
from login import get_token
sp = spotipy.Spotify(auth= get_token())
sp.trace = False
def get_user():
user = sp.current_user()
user = user['uri'].split(':')
return user[2]
#creates new playlit and returns id
def create_playlist(name):
playlist = sp.user_playlist_create(get_user(), name, public=True)
return playlist['id']
def get_playlist(id):
return sp.user_playlist(get_user(), playlist_id=id, fields=None)
def add_to_playlist(track,playlist_id):
sp.user_playlist_add_tracks(get_user(), playlist_id, [track])
#remove a track from playlist
def remove_from_playlist(track,playlist_id):
sp.user_playlist_remove_all_occurrences_of_tracks(get_user(),playlist_id,[track],snapshot_id=None)
#return all IDs of the tracks from the playlist with given ID
def get_all_from_playlist(playlist_id):
playlist = get_playlist(playlist_id)
#get items
items = playlist['tracks']['items']
# print(items[1]['track']['id'])
id_list = []
for item in items:
id_list += [item['track']['id']]
return id_list
def remove_all_from_playlist(playlist_id):
track_id = get_all_from_playlist(playlist_id)
for id in track_id :
remove_from_playlist(id , playlist_id)
if __name__== '__main__':
#mock_id = create_playlist('Dummy')
#dummy tracks
#track_uri = '5CMjjywI0eZMixPeqNd75R'
mock_id = '1ouOPA7zXC3Rh0AAYOVErV'
#add_to_playlist(track_uri,mock_id)
#remove(track_uri,mock_id)
remove_all_from_playlist(mock_id)
#print get_user(get_token())