diff --git a/alarm.py b/alarm.py index 3180de1..e317a8b 100644 --- a/alarm.py +++ b/alarm.py @@ -45,6 +45,7 @@ if __name__ == '__main__': network = Network(NetworkUser(os.environ['SPOTCLIENT'], os.environ['SPOTSECRET'], os.environ['SPOTREFRESH'])) + network.user.refresh_token() found = False diff --git a/backup.py b/backup.py index a6b807c..b1948ca 100644 --- a/backup.py +++ b/backup.py @@ -32,6 +32,7 @@ if __name__ == '__main__': network = Network(NetworkUser(os.environ['SPOTCLIENT'], os.environ['SPOTSECRET'], os.environ['SPOTREFRESH'])) + network.user.refresh_token() playlists = network.get_user_playlists() for playlist in playlists: diff --git a/generate_playlists.py b/generate_playlists.py index e0d4beb..9299c16 100644 --- a/generate_playlists.py +++ b/generate_playlists.py @@ -125,9 +125,9 @@ def go(): net = Network(NetworkUser(os.environ['SPOTCLIENT'], os.environ['SPOTSECRET'], os.environ['SPOTREFRESH'])) + net.user.refresh_token() engine = PlaylistEngine(net) - engine.load_user_playlists() for super_playlist in to_execute: update_super_playlist(engine, super_playlist) diff --git a/getaccesstoken.py b/getaccesstoken.py index b46159b..90b65ff 100644 --- a/getaccesstoken.py +++ b/getaccesstoken.py @@ -18,5 +18,6 @@ if __name__ == '__main__': network = Network(NetworkUser(os.environ['SPOTCLIENT'], os.environ['SPOTSECRET'], os.environ['SPOTREFRESH'])) + network.user.refresh_token() print(network.user.access_token) diff --git a/sort_playlist.py b/sort_playlist.py index da84841..dd514e1 100644 --- a/sort_playlist.py +++ b/sort_playlist.py @@ -31,6 +31,7 @@ def go(playlist_name): net = Network(NetworkUser(os.environ['SPOTCLIENT'], os.environ['SPOTSECRET'], os.environ['SPOTREFRESH'])) + net.user.refresh_token() engine = PlaylistEngine(net) engine.reorder_playlist_by_added_date(playlist_name) diff --git a/spotframework/model/service.py b/spotframework/model/service.py index 2173b5f..023f14b 100644 --- a/spotframework/model/service.py +++ b/spotframework/model/service.py @@ -15,6 +15,8 @@ class Context: self.uri = Uri(uri) else: self.uri = uri + if self.uri.object_type not in [Uri.ObjectType.album, Uri.ObjectType.artist, Uri.ObjectType.playlist]: + raise TypeError('context uri must be one of album, artist, playlist') self.object_type = object_type self.href = href self.external_spot = external_spot diff --git a/spotframework/model/track.py b/spotframework/model/track.py index b8ea4fd..e03e1b3 100644 --- a/spotframework/model/track.py +++ b/spotframework/model/track.py @@ -86,6 +86,10 @@ class SpotifyTrack(Track): return Color.BOLD + Color.YELLOW + 'SpotifyTrack' + Color.END + \ f': {self.name}, ({self.album}), {self.artists}, {self.uri}' + @staticmethod + def get_uri_shell(uri): + return SpotifyTrack(name=None, album=None, artists=None, uri=uri) + class LibraryTrack(SpotifyTrack): def __init__(self, diff --git a/spotframework/net/user.py b/spotframework/net/user.py index c934988..cf94419 100644 --- a/spotframework/net/user.py +++ b/spotframework/net/user.py @@ -5,6 +5,7 @@ from base64 import b64encode import logging import time from typing import Optional +from datetime import datetime, timezone logger = logging.getLogger(__name__) @@ -20,8 +21,10 @@ class NetworkUser(User): self.client_id = client_id self.client_secret = client_secret - self.refresh_token() - self.refresh_info() + self.last_refreshed = None + self.token_expiry = None + + self.on_refresh = [] def __repr__(self): return Color.RED + Color.BOLD + 'NetworkUser' + Color.END + \ @@ -42,12 +45,18 @@ class NetworkUser(User): headers = {'Authorization': 'Basic %s' % idsecret} data = {"grant_type": "refresh_token", "refresh_token": self.refreshtoken} - + + now = datetime.now(timezone.utc) req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers) if 200 <= req.status_code < 300: logger.debug('token refreshed') - self.accesstoken = req.json()['access_token'] + resp = req.json() + self.accesstoken = resp['access_token'] + self.token_expiry = resp['expires_in'] + self.last_refreshed = now + for func in self.on_refresh: + func(self) else: if req.status_code == 429: