Create class to manage user session on the spotify interface
A lot of the methods on the spotify interface have session object (named spfy) from the spotipy module as a dependency. This could be removed from the API by making it an internal field of a proxy class to the spotipy library.
Current behavior:
def login_user(username, scope=None):
# Logs user to spotify
return spfy # Returns a session object from spotify
# Then it's used on other methods
def get_genres(spfy, artists_ids):
# Returns genres
def get_features(spfy, tracks):
# Returns features
# plus other methods that use spfy
The idea is to make a class like this:
class SpotifySession:
def __init__(self, username):
self._spfy = self._login_user(username)
def _login_user(username, scope=None):
# Logs user to spotify
return spfy # Returns a session object from spotify
def get_genres(self, artists_ids):
# Returns genres using self._spfy
def get_features(self, tracks):
# Returns features using self_spfy
In this way the user doesn't need to keep passing the session object around and it forces them to login before calling the methods.
This might clash with the _for_all() function that kinda uses high order functions to make repeated requests with paging objects from the spotify API, but I imagine it's not hard to refactor.
@edujtm Hey can I take this up?
Sure, but keep in mind that this is change that will probably affect other files as well (e.g. diversify.py and genetic.py) because the spotify interface is used by almost all of them. And the way I implement the _for_all method might give some headaches. This change might not be as simple as it seems.
I suggest you to wait until I fix #7, it's easier than I thought to fix it, so I'll do it myself.
@edujtm sorry, I have done it few hours ago, but didn't push :)
@bsr3000 Cool, I'll be reviewing it asap.