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() writer.writeheader()
for track in playlist.tracks: for track in playlist.tracks:
writer.writerow({
trackdict = { 'name': track.name,
'name':track.name, 'album': track.album.name,
'album':track.album.name, 'added': track.added_at,
'added':track.added_at, 'track id': track.uri.object_id if track.uri is not None else 'none',
'track id':track.spotify_id, 'album id': track.album.uri.object_id if track.album.uri is not None else 'none',
'album id':track.album.spotify_id, 'added by': track.added_by.username,
'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)
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)

View File

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

View File

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

View File

@ -14,6 +14,7 @@ class Uri:
self.object_type = None self.object_type = None
self.object_id = None self.object_id = None
self.username = None self.username = None
self.is_local = False
parts = input_string.split(':') parts = input_string.split(':')
@ -28,6 +29,12 @@ class Uri:
raise ValueError('malformed uri') raise ValueError('malformed uri')
self.object_type = self.ObjectType[parts[3]] self.object_type = self.ObjectType[parts[3]]
self.object_id = parts[4] 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: else:
raise ValueError(f'malformed uri: {len(parts)} parts') raise ValueError(f'malformed uri: {len(parts)} parts')