-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.py
executable file
·74 lines (66 loc) · 2.52 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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys;
import os;
import spotipy;
import spotipy.util as util;
os.environ['SPOTIPY_CLIENT_ID']='6c046a5a012c406e9677413fc844809e'
os.environ['SPOTIPY_CLIENT_SECRET']='8f84bde73dc4477da4da9648c07885a1'
os.environ['SPOTIPY_REDIRECT_URI']='http://localhost/'
username = 'nhuck15';
scope = 'playlist-read-private playlist-modify-private playlist-modify-public'
def search(song):
token = util.prompt_for_user_token(username,scope);
sp = spotipy.Spotify(auth=token);
results = sp.search(song);
#print results
vals = [];
for x in results['tracks']['items']:
vals.append("%s,%s,%s" % (x['name'],x['artists'][0]['name'],x['album']['name']))
return vals;
def getPlaylist(name):
token = util.prompt_for_user_token(username,scope);
sp = spotipy.Spotify(auth=token);
playlists = sp.user_playlists(username)
vals = [];
for playlist in playlists['items']:
#print playlist['name']
if playlist['name'] == name:
results = sp.user_playlist(username, playlist['id'])
tracks = results['tracks'];
for x in tracks['items']:
x = x['track']
vals.append("%s,%s,%s" % (x['name'],x['artists'][0]['name'],x['album']['name']))
return vals;
def add(song, name):
token = util.prompt_for_user_token(username,scope);
sp = spotipy.Spotify(auth=token);
results = sp.search(song);
playlists = sp.user_playlists(username)
songID = results['tracks']['items'][0]['id']
playlistID = None;
for playlist in playlists['items']:
#print playlist['name']
if playlist['name'] == name:
playlistID = playlist['id'];
sp.user_playlist_add_tracks(username, playlistID, [songID]);
def remove(song, name):
token = util.prompt_for_user_token(username,scope);
sp = spotipy.Spotify(auth=token);
results = sp.search(song);
playlists = sp.user_playlists(username)
songID = results['tracks']['items'][0]['id']
playlistID = None;
for playlist in playlists['items']:
#print playlist['name']
if playlist['name'] == name:
playlistID = playlist['id'];
sp.user_playlist_remove_all_occurrences_of_tracks(username, playlistID, [songID]);
def getAllPlaylists():
token = util.prompt_for_user_token(username,scope);
sp = spotipy.Spotify(auth=token);
playlists = sp.user_playlists(username)
vals = [];
for playlist in playlists['items']:
vals.append(playlist['name'])
return vals;
#print(getAllPlaylists())
#remove('breaking the girl', 'etc')