From b9c1b7f8983ddac625dc2a0d53ca59f8d6e03975 Mon Sep 17 00:00:00 2001 From: sigma67 Date: Fri, 23 Apr 2021 15:53:22 +0200 Subject: [PATCH] Update SpotifyExport.py --- SpotifyExport.py | 61 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/SpotifyExport.py b/SpotifyExport.py index 629c268..0d8618a 100644 --- a/SpotifyExport.py +++ b/SpotifyExport.py @@ -3,42 +3,29 @@ import settings import html + class Spotify: def __init__(self): conf = settings['spotify'] client_credentials_manager = SpotifyClientCredentials(client_id=conf['client_id'], client_secret=conf['client_secret']) self.api = spotipy.Spotify(client_credentials_manager=client_credentials_manager) - def build_results(self, tracks): - results = [] - for track in tracks['items']: - if track['track'] is not None: - results.append({ - 'artist': ' '.join([artist['name'] for artist in track['track']['artists']]), - 'name': track['track']['name'], - 'album': track['track']['album']['name'], - 'duration': track['track']['duration_ms']/1000 - }) - - return results - def getSpotifyPlaylist(self, url): - url_parts = url.split('/') - playlistId = url_parts[4].split('?')[0] + playlistId = get_id_from_url(url) if len(playlistId) != 22: raise Exception('Bad playlist id: ' + playlistId) results = self.api.playlist(playlistId) name = results['name'] - tracks = self.build_results(results['tracks']) + tracks = build_results(results['tracks']['items']) count = 1 more = len(results['tracks']['items']) == 100 while more: - items = self.api.playlist_tracks(playlistId, offset = count * 100, limit=100) + more_tracks = self.api.playlist_items(playlistId, offset = count * 100, limit=100) print('requested from ' + str(count * 100)) - tracks += self.build_results(items) - more = len(items["items"]) == 100 + tracks += build_results(more_tracks['items']) + more = len(more_tracks["items"]) == 100 count = count + 1 return {'tracks': tracks, 'name': name, 'description': html.unescape(results['description'])} @@ -53,4 +40,38 @@ def getUserPlaylists(self, user): more = len(results) == 50 count = count + 1 - return [p for p in pl if p['owner']['display_name'] == user and p['tracks']['total'] > 0] \ No newline at end of file + return [p for p in pl if p['owner']['display_name'] == user and p['tracks']['total'] > 0] + + def get_tracks(self, url): + tracks = [] + id = get_id_from_url(url) + if 'album' in url: + album = self.api.album(id) + tracks.extend(build_results(album['tracks']['items'], album['name'])) + elif 'track' in url: + track = self.api.track(id) + tracks.extend(build_results([track])) + return tracks + + +def build_results(tracks, album=None): + results = [] + for track in tracks: + if 'track' in track: + track = track['track'] + if not track: + continue + album_name = album if album else track['album']['name'] + results.append({ + 'artist': ' '.join([artist['name'] for artist in track['artists']]), + 'name': track['name'], + 'album': album_name, + 'duration': track['duration_ms']/1000 + }) + + return results + + +def get_id_from_url(url): + url_parts = url.split('/') + return url_parts[4].split('?')[0] \ No newline at end of file