-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspotify.py
More file actions
71 lines (54 loc) · 2.77 KB
/
spotify.py
File metadata and controls
71 lines (54 loc) · 2.77 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
62
63
64
65
66
67
68
69
70
71
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyOAuth
from textify import clean_string
class Spotify:
__redirect_uri = 'https://localhost/'
__scope = 'user-library-read'
def __init__(self, username, client_id, client_secret):
self.username = username
self.__sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id,client_secret=client_secret,scope=self.__scope,username=self.username,redirect_uri=self.__redirect_uri))
# Get all the saved tracks in the current user's library
def get_saved_tracks(self):
tracks = []
results = self.__sp.current_user_saved_tracks()
for item in results['items']:
clean_artist = clean_string(item['track']['artists'][0]['name'])
clean_track = clean_string(item['track']['name'])
tracks.append([clean_artist,clean_track,''])
while results['next']:
results = self.__sp.next(results)
for item in results['items']:
clean_artist = clean_string(item['track']['artists'][0]['name'])
clean_track = clean_string(item['track']['name'])
tracks.append([clean_artist,clean_track,''])
return tracks
# Get all playlists for user, if the supplied playlist exists get
# all tracks in that playlist
def get_playlist_tracks(self, playlist_name):
tracks = []
playlists = []
# Get list of all playlists
results = self.__sp.current_user_playlists()
for item in results['items']:
playlists.append([item['name'],item['id']])
while results['next']:
results = self.__sp.next(results)
for item in results['items']:
playlists.append([item['name'],item['id']])
# If the provided playlist name matches one of the playlists,
# grab all the tracks from that playlist
for title_id in playlists:
if playlist_name.lower() in title_id[0].lower():
results = self.__sp.user_playlist_tracks(self.username,title_id[1])
for item in results['items']:
clean_artist = clean_string(item['track']['artists'][0]['name'])
clean_track = clean_string(item['track']['name'])
tracks.append([clean_artist,clean_track,''])
while results['next']:
results = self.__sp.next(results)
for item in results['items']:
clean_artist = clean_string(item['track']['artists'][0]['name'])
clean_track = clean_string(item['track']['name'])
tracks.append([clean_artist,clean_track,''])
return tracks