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 = {
writer.writerow({
'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)
'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,6 +69,7 @@ class SpotifyAlbum(Album):
else:
self.uri = uri
if self.uri:
if self.uri.object_type != Uri.ObjectType.album:
raise TypeError('provided uri not for an album')

View File

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

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')