Skip to content

Commit

Permalink
Update SpotifyExport.py
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Apr 23, 2021
1 parent 7c80118 commit b9c1b7f
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions SpotifyExport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])}
Expand All @@ -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]
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]

0 comments on commit b9c1b7f

Please sign in to comment.