handling for no uri, compacted csv code

This commit is contained in:
aj 2019-10-10 22:38:22 +01:00
parent f868d1425a
commit 2863525767
4 changed files with 23 additions and 18 deletions

View File

@ -22,17 +22,13 @@ def export_playlist(playlist, path, name=None):
writer.writeheader()
for track in playlist.tracks:
trackdict = {
'name':track.name,
'album':track.album.name,
'added':track.added_at,
'track id':track.spotify_id,
'album id':track.album.spotify_id,
'added by':track.added_by.username}
trackdict['album artist'] = ', '.join(x.name for x in track.album.artists)
trackdict['artist'] = ', '.join(x.name for x in track.artists)
writer.writerow(trackdict)
writer.writerow({
'name': track.name,
'album': track.album.name,
'added': track.added_at,
'track id': track.uri.object_id if track.uri is not None else 'none',
'album id': track.album.uri.object_id if track.album.uri is not None else 'none',
'added by': track.added_by.username,
'album artist': ', '.join(x.name for x in track.album.artists),
'artist': ', '.join(x.name for x in track.artists)
})

View File

@ -69,8 +69,9 @@ class SpotifyAlbum(Album):
else:
self.uri = uri
if self.uri.object_type != Uri.ObjectType.album:
raise TypeError('provided uri not for an album')
if self.uri:
if self.uri.object_type != Uri.ObjectType.album:
raise TypeError('provided uri not for an album')
self.genres = genres

View File

@ -34,8 +34,9 @@ class SpotifyArtist(Artist):
else:
self.uri = uri
if self.uri.object_type != Uri.ObjectType.artist:
raise TypeError('provided uri not for an artist')
if self.uri:
if self.uri.object_type != Uri.ObjectType.artist:
raise TypeError('provided uri not for an artist')
self.genres = genres

View File

@ -14,6 +14,7 @@ class Uri:
self.object_type = None
self.object_id = None
self.username = None
self.is_local = False
parts = input_string.split(':')
@ -28,6 +29,12 @@ class Uri:
raise ValueError('malformed uri')
self.object_type = self.ObjectType[parts[3]]
self.object_id = parts[4]
elif len(parts) == 6:
if parts[1] == 'local':
self.object_type = self.ObjectType.track
self.is_local = True
else:
raise ValueError(f'malformed uri: {len(parts)} parts')
else:
raise ValueError(f'malformed uri: {len(parts)} parts')