list comprehension on export

This commit is contained in:
aj 2019-04-30 16:48:46 +01:00
parent d4b39fe9f2
commit 0726e15c9d
2 changed files with 5 additions and 16 deletions

View File

@ -1,21 +1,18 @@
import csv
import spotframework.net.network as network
import datetime
headers = ['name', 'artist', 'album', 'album artist', 'added', 'track id', 'album id', 'added by']
def exportPlaylist(playlist, path, name=None):
#playlist = network.getPlaylistTracks(user, playlistid)
date = str(datetime.datetime.now())
if name is None:
name = playlist.name
with open('{}/{}_{}.csv'.format(path, name.replace('/', '_'), date.split('.')[0]), 'w') as fileobj:
with open('{}/{}_{}.csv'.format(path, name.replace('/', '_'), date.split('.')[0]), 'w', newline='') as fileobj:
writer = csv.DictWriter(fileobj, fieldnames = headers)
writer = csv.DictWriter(fileobj, fieldnames=headers)
writer.writeheader()
for track in playlist.tracks:
@ -28,16 +25,8 @@ def exportPlaylist(playlist, path, name=None):
'album id':track['track']['album']['id'],
'added by':track['added_by']['id']}
alart = []
for albumartist in track['track']['album']['artists']:
alart.append(albumartist['name'])
trackdict['album artist'] = ', '.join(x['name'] for x in track['track']['album']['artists'])
trackdict['album artist'] = ', '.join(alart)
art = []
for artist in track['track']['artists']:
art.append(artist['name'])
trackdict['artist'] = ', '.join(art)
trackdict['artist'] = ', '.join(x['name'] for x in track['track']['artists'])
writer.writerow(trackdict)

View File

@ -24,7 +24,7 @@ class network:
return playlist
def getPlaylists(self, offset=0):
print('getting user playlists')
print('getting user playlists {}'.format(offset))
headers = {'Authorization': 'Bearer ' + self.user.access_token}