diff --git a/spotify/api/player.py b/spotify/api/player.py index d0bceeb..51c064c 100644 --- a/spotify/api/player.py +++ b/spotify/api/player.py @@ -33,12 +33,8 @@ def play(username=None): net = database.get_authed_network(username) player = Player(net) - device = None - if 'device_name' in request_json: - devices = net.get_available_devices() - device = next((i for i in devices if i.name == request_json['device_name']), None) + player.play(context=context, device_name=request_json.get('device_name', None)) - player.play(context=context, device=device) logger.info(f'played {uri}') return jsonify({'message': 'played', 'status': 'success'}), 200 else: @@ -53,12 +49,8 @@ def play(username=None): if playlist_to_play is not None: player = Player(net) - device = None - if 'device_name' in request_json: - devices = net.get_available_devices() - device = next((i for i in devices if i.name == request_json['device_name']), None) + player.play(context=Context(playlist_to_play.uri), device_name=request_json.get('device_name', None)) - player.play(context=Context(playlist_to_play.uri), device=device) logger.info(f'played {request_json["playlist_name"]}') return jsonify({'message': 'played', 'status': 'success'}), 200 else: @@ -74,12 +66,8 @@ def play(username=None): net = database.get_authed_network(username) player = Player(net) - device = None - if 'device_name' in request_json: - devices = net.get_available_devices() - device = next((i for i in devices if i.name == request_json['device_name']), None) + player.play(tracks=uris, device_name=request_json.get('device_name', None)) - player.play(tracks=uris, device=device) logger.info(f'played tracks') return jsonify({'message': 'played', 'status': 'success'}), 200 else: diff --git a/spotify/auth/auth.py b/spotify/auth/auth.py index 0ab6349..336cdfe 100644 --- a/spotify/auth/auth.py +++ b/spotify/auth/auth.py @@ -178,6 +178,8 @@ def token(): user_reference.update({ 'access_token': resp['access_token'], 'refresh_token': resp['refresh_token'], + 'last_refreshed': datetime.datetime.now(datetime.timezone.utc), + 'token_expiry': resp['expires_in'], 'spotify_linked': True }) @@ -200,6 +202,8 @@ def deauth(): user_reference.update({ 'access_token': None, 'refresh_token': None, + 'last_refreshed': datetime.datetime.now(datetime.timezone.utc), + 'token_expiry': None, 'spotify_linked': False }) diff --git a/spotify/db/database.py b/spotify/db/database.py index bb40dae..2d816c6 100644 --- a/spotify/db/database.py +++ b/spotify/db/database.py @@ -17,7 +17,8 @@ def refresh_token_database_callback(user): user_ref = get_user_doc_ref(user.user_id) user_ref.update({ - 'access_token': user.accesstoken, + 'access_token': user.access_token, + 'refresh_token': user.refresh_token, 'last_refreshed': user.last_refreshed, 'token_expiry': user.token_expiry }) @@ -44,7 +45,7 @@ def get_authed_network(username): if user_dict['last_refreshed'] + timedelta(seconds=user_dict['token_expiry'] - 1) \ < datetime.now(timezone.utc): - user_obj.refresh_token() + user_obj.refresh_access_token() user_obj.refresh_info() return Network(user_obj) diff --git a/spotify/db/user.py b/spotify/db/user.py index d742d7f..6957abc 100644 --- a/spotify/db/user.py +++ b/spotify/db/user.py @@ -1,3 +1,4 @@ +from spotframework.util.console import Color from spotframework.net.user import NetworkUser @@ -7,3 +8,7 @@ class DatabaseUser(NetworkUser): super().__init__(client_id=client_id, client_secret=client_secret, refresh_token=refresh_token, access_token=access_token) self.user_id = user_id + + def __repr__(self): + return Color.RED + Color.BOLD + 'DatabaseUser' + Color.END + \ + f': {self.user_id}, {self.username}, {self.display_name}, {self.uri}'