removed user refresh by default, added URI instance checking

This commit is contained in:
aj 2019-09-27 10:34:09 +01:00
parent 933e70ee44
commit da8d65e8ba
8 changed files with 24 additions and 5 deletions

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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,

View File

@ -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 + \
@ -43,11 +46,17 @@ class NetworkUser(User):
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: