updated variable names, added to auth process, added device_name to player

This commit is contained in:
aj 2019-10-01 19:20:22 +01:00
parent da8d65e8ba
commit 47a7f74c98
8 changed files with 25 additions and 17 deletions

View File

@ -45,7 +45,7 @@ if __name__ == '__main__':
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
network.user.refresh_token()
network.user.refresh_access_token()
found = False

View File

@ -32,7 +32,7 @@ if __name__ == '__main__':
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
network.user.refresh_token()
network.user.refresh_access_token()
playlists = network.get_user_playlists()
for playlist in playlists:

View File

@ -125,7 +125,7 @@ def go():
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
net.user.refresh_token()
net.user.refresh_access_token()
engine = PlaylistEngine(net)

View File

@ -18,6 +18,6 @@ if __name__ == '__main__':
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
network.user.refresh_token()
network.user.refresh_access_token()
print(network.user.access_token)

View File

@ -31,7 +31,7 @@ def go(playlist_name):
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
os.environ['SPOTSECRET'],
os.environ['SPOTREFRESH']))
net.user.refresh_token()
net.user.refresh_access_token()
engine = PlaylistEngine(net)
engine.reorder_playlist_by_added_date(playlist_name)

View File

@ -31,7 +31,7 @@ class Network:
if headers is None:
headers = dict()
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
headers['Authorization'] = 'Bearer ' + self.user.access_token
if whole_url:
req = requests.get(whole_url, params=params, headers=headers)
@ -73,7 +73,7 @@ class Network:
if headers is None:
headers = dict()
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
headers['Authorization'] = 'Bearer ' + self.user.access_token
req = requests.post(const.api_url + url, params=params, json=json, headers=headers)
@ -108,7 +108,7 @@ class Network:
if headers is None:
headers = dict()
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
headers['Authorization'] = 'Bearer ' + self.user.access_token
req = requests.put(const.api_url + url, params=params, json=json, headers=headers)

View File

@ -13,10 +13,10 @@ logger = logging.getLogger(__name__)
class NetworkUser(User):
def __init__(self, client_id, client_secret, refresh_token, access_token=None):
super().__init__('')
super().__init__(None)
self.accesstoken = access_token
self.refreshtoken = refresh_token
self.access_token = access_token
self.refresh_token = refresh_token
self.client_id = client_id
self.client_secret = client_secret
@ -30,9 +30,9 @@ class NetworkUser(User):
return Color.RED + Color.BOLD + 'NetworkUser' + Color.END + \
f': {self.username}, {self.display_name}, {self.uri}'
def refresh_token(self) -> None:
def refresh_access_token(self) -> None:
if self.refreshtoken is None:
if self.refresh_token is None:
raise NameError('no refresh token to query')
if self.client_id is None:
@ -44,7 +44,7 @@ class NetworkUser(User):
idsecret = b64encode(bytes(self.client_id + ':' + self.client_secret, "utf-8")).decode("ascii")
headers = {'Authorization': 'Basic %s' % idsecret}
data = {"grant_type": "refresh_token", "refresh_token": self.refreshtoken}
data = {"grant_type": "refresh_token", "refresh_token": self.refresh_token}
now = datetime.now(timezone.utc)
req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)
@ -52,7 +52,9 @@ class NetworkUser(User):
if 200 <= req.status_code < 300:
logger.debug('token refreshed')
resp = req.json()
self.accesstoken = resp['access_token']
self.access_token = resp['access_token']
if resp.get('refresh_token', None):
self.refresh_token = resp['refresh_token']
self.token_expiry = resp['expires_in']
self.last_refreshed = now
for func in self.on_refresh:
@ -94,7 +96,7 @@ class NetworkUser(User):
def get_info(self) -> Optional[dict]:
headers = {'Authorization': 'Bearer %s' % self.accesstoken}
headers = {'Authorization': 'Bearer %s' % self.access_token}
req = requests.get('https://api.spotify.com/v1/me', headers=headers)

View File

@ -36,7 +36,13 @@ class Player:
def play(self,
context: Union[Context, SpotifyAlbum, SpotifyPlaylist] = None,
tracks: List[SpotifyTrack] = None,
device: Device = None):
device: Device = None,
device_name: str = None):
if device_name:
searched_device = next((i for i in self.available_devices if i.name == device_name), None)
if searched_device:
device = searched_device
if context and tracks:
raise Exception('cant execute context and track list')
if context: