def fetch_related_tracks(self, track, related_by): """Fetch a set of Track namedtuples that are related to TRACK by RELATED_BY.""" if related_by == 'artist': related_track_data = self._spotify.artist_top_tracks(track.artist)['tracks'] related_tracks = {pluck_track(track_record, artist=track.artist) for track_record in related_track_data} elif related_by == 'album': related_track_data = self._spotify.album_tracks(track.album)['items'] related_tracks = {pluck_track(track_record, album=track.album) for track_record in related_track_data} else: raise SouffleParameterError('Invalid souffle type "{}".'.format(related_by)) return related_tracks
def test_pluck_track_simple(self, spotify_track_object): """Simple test case for plucking a Track namedtuple from a spotify track object.""" # Given / When plucked_track = pluck_track(spotify_track_object) # Then expected_track = Track('TRACK_ID', 'ARTIST_ID', 'ALBUM_ID') assert plucked_track == expected_track
def test_pluck_track_with_two_albums(self, spotify_track_object_two_artists): """Pluck a track with two artists. Only the first artist should be plucked.""" # Given / When plucked_track = pluck_track(spotify_track_object_two_artists) # Then expected_track = Track('TRACK_ID', 'ARTIST_ID_1', 'ALBUM_ID') assert plucked_track == expected_track
def test_pluck_track_provide_album(self, spotify_track_object): """Provide an 'album' field when plucking a track.""" # Given / When custom_album = 'CUSTOM_ALBUM' plucked_track = pluck_track(spotify_track_object, album=custom_album) # Then expected_track = Track('TRACK_ID', 'ARTIST_ID', custom_album) assert plucked_track == expected_track
def test_pluck_track_provide_artist(self, spotify_track_object): """Provide an 'artist' field when plucking a track.""" # Given / When custom_artist = 'CUSTOM_ARTIST' plucked_track = pluck_track(spotify_track_object, artist=custom_artist) # Then expected_track = Track('TRACK_ID', custom_artist, 'ALBUM_ID') assert plucked_track == expected_track
def fetch_playlist(self, playlist_uri: str): """Fetch a Playlist namedtuple representation of a spotify playlist given a playlist_uri.""" playlist_id = playlist_uri.split(':')[-1] name, description = fetch_playlist_metadata(playlist_id, self._access_token) track_data = fetch_playlist_track_data(playlist_id, self._access_token) tracks = [pluck_track(track_record) for track_record in track_data] return Playlist( name=name, tracks=tracks, description=description )