fleshed out uri types, can now count all uris

This commit is contained in:
aj 2019-10-06 11:57:02 +01:00
parent 2528af06b1
commit 28c76bbd83

View File

@ -1,6 +1,8 @@
from spotframework.net.network import Network as SpotifyNetwork from spotframework.net.network import Network as SpotifyNetwork
from spotframework.model.playlist import SpotifyPlaylist from spotframework.model.playlist import SpotifyPlaylist
from spotframework.model.track import SpotifyTrack from spotframework.model.track import SpotifyTrack
from spotframework.model.album import SpotifyAlbum
from spotframework.model.artist import SpotifyArtist
from spotframework.model.uri import Uri from spotframework.model.uri import Uri
from fmframework.net.network import Network as FMNetwork from fmframework.net.network import Network as FMNetwork
@ -20,10 +22,16 @@ class Counter:
def count(self, uri: Uri): def count(self, uri: Uri):
if uri.object_type == Uri.ObjectType.playlist: if uri.object_type == Uri.ObjectType.playlist:
return self.count_playlist(uri=uri) return self.count_playlist(uri=uri)
elif uri.object_type == Uri.ObjectType.track:
return self.count_track(uri=uri)
elif uri.object_type == Uri.ObjectType.album:
return self.count_album(uri=uri)
elif uri.object_type == Uri.ObjectType.artist:
return self.count_artist(uri=uri)
else: else:
logger.error('cannot process uri') logger.error('cannot process uri')
def count_playlist(self, username: str = None, uri: Uri = None, playlist: SpotifyPlaylist = None): def count_playlist(self, username: str = None, uri: Uri = None, playlist: SpotifyPlaylist = None) -> int:
if uri is None and playlist is None: if uri is None and playlist is None:
raise ValueError('no input playlist to count') raise ValueError('no input playlist to count')
@ -33,6 +41,8 @@ class Counter:
playlist.tracks = self.spotnet.get_playlist_tracks(uri=playlist.uri) playlist.tracks = self.spotnet.get_playlist_tracks(uri=playlist.uri)
if uri is not None: if uri is not None:
if uri.object_type != Uri.ObjectType.playlist:
raise ValueError('uri not a playlist')
playlist = self.spotnet.get_playlist(uri=uri) playlist = self.spotnet.get_playlist(uri=uri)
scrobble_count = 0 scrobble_count = 0
@ -59,3 +69,63 @@ class Counter:
logger.error(f'no last.fm track returned for {song}') logger.error(f'no last.fm track returned for {song}')
return scrobble_count return scrobble_count
def count_track(self, username: str = None, uri: Uri = None, track: SpotifyTrack = None) -> int:
if uri is None and track is None:
raise ValueError('no track to count')
if uri is not None:
if uri.object_type != Uri.ObjectType.track:
raise ValueError('uri not a track')
track = self.spotnet.get_track(uri=uri)
if username is not None:
fmtrack = self.fmnet.get_track(name=track.name, artist=track.artists[0].name, username=username)
else:
fmtrack = self.fmnet.get_track(name=track.name, artist=track.artists[0].name, username=self.fmnet.username)
if fmtrack is not None:
return fmtrack.user_scrobbles
else:
return 0
def count_album(self, username: str = None, uri: Uri = None, album: SpotifyAlbum = None) -> int:
if uri is None and album is None:
raise ValueError('no album to count')
if uri is not None:
if uri.object_type != Uri.ObjectType.album:
raise ValueError('uri not an album')
album = self.spotnet.get_album(uri=uri)
if username is not None:
fmalbum = self.fmnet.get_album(name=album.name, artist=album.artists[0].name, username=username)
else:
fmalbum = self.fmnet.get_album(name=album.name, artist=album.artists[0].name, username=self.fmnet.username)
if fmalbum is not None:
return fmalbum.user_scrobbles
else:
return 0
def count_artist(self, username: str = None, uri: Uri = None, artist: SpotifyArtist = None) -> int:
if uri is None and artist is None:
raise ValueError('no artist to count')
if uri is not None:
if uri.object_type != Uri.ObjectType.artist:
raise ValueError('uri not an artist')
artist = self.spotnet.get_artist(uri=uri)
if username is not None:
fmartist = self.fmnet.get_artist(name=artist.name, username=username)
else:
fmartist = self.fmnet.get_artist(name=artist.name, username=self.fmnet.username)
if fmartist is not None:
return fmartist.user_scrobbles
else:
return 0