updated variable names, added to auth process, added device_name to player
This commit is contained in:
parent
da8d65e8ba
commit
47a7f74c98
2
alarm.py
2
alarm.py
@ -45,7 +45,7 @@ if __name__ == '__main__':
|
|||||||
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
||||||
os.environ['SPOTSECRET'],
|
os.environ['SPOTSECRET'],
|
||||||
os.environ['SPOTREFRESH']))
|
os.environ['SPOTREFRESH']))
|
||||||
network.user.refresh_token()
|
network.user.refresh_access_token()
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ if __name__ == '__main__':
|
|||||||
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
||||||
os.environ['SPOTSECRET'],
|
os.environ['SPOTSECRET'],
|
||||||
os.environ['SPOTREFRESH']))
|
os.environ['SPOTREFRESH']))
|
||||||
network.user.refresh_token()
|
network.user.refresh_access_token()
|
||||||
playlists = network.get_user_playlists()
|
playlists = network.get_user_playlists()
|
||||||
|
|
||||||
for playlist in playlists:
|
for playlist in playlists:
|
||||||
|
@ -125,7 +125,7 @@ def go():
|
|||||||
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
||||||
os.environ['SPOTSECRET'],
|
os.environ['SPOTSECRET'],
|
||||||
os.environ['SPOTREFRESH']))
|
os.environ['SPOTREFRESH']))
|
||||||
net.user.refresh_token()
|
net.user.refresh_access_token()
|
||||||
|
|
||||||
engine = PlaylistEngine(net)
|
engine = PlaylistEngine(net)
|
||||||
|
|
||||||
|
@ -18,6 +18,6 @@ if __name__ == '__main__':
|
|||||||
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
network = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
||||||
os.environ['SPOTSECRET'],
|
os.environ['SPOTSECRET'],
|
||||||
os.environ['SPOTREFRESH']))
|
os.environ['SPOTREFRESH']))
|
||||||
network.user.refresh_token()
|
network.user.refresh_access_token()
|
||||||
|
|
||||||
print(network.user.access_token)
|
print(network.user.access_token)
|
||||||
|
@ -31,7 +31,7 @@ def go(playlist_name):
|
|||||||
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
net = Network(NetworkUser(os.environ['SPOTCLIENT'],
|
||||||
os.environ['SPOTSECRET'],
|
os.environ['SPOTSECRET'],
|
||||||
os.environ['SPOTREFRESH']))
|
os.environ['SPOTREFRESH']))
|
||||||
net.user.refresh_token()
|
net.user.refresh_access_token()
|
||||||
|
|
||||||
engine = PlaylistEngine(net)
|
engine = PlaylistEngine(net)
|
||||||
engine.reorder_playlist_by_added_date(playlist_name)
|
engine.reorder_playlist_by_added_date(playlist_name)
|
||||||
|
@ -31,7 +31,7 @@ class Network:
|
|||||||
if headers is None:
|
if headers is None:
|
||||||
headers = dict()
|
headers = dict()
|
||||||
|
|
||||||
headers['Authorization'] = 'Bearer ' + self.user.accesstoken
|
headers['Authorization'] = 'Bearer ' + self.user.access_token
|
||||||
|
|
||||||
if whole_url:
|
if whole_url:
|
||||||
req = requests.get(whole_url, params=params, headers=headers)
|
req = requests.get(whole_url, params=params, headers=headers)
|
||||||
@ -73,7 +73,7 @@ class Network:
|
|||||||
if headers is None:
|
if headers is None:
|
||||||
headers = dict()
|
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)
|
req = requests.post(const.api_url + url, params=params, json=json, headers=headers)
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ class Network:
|
|||||||
if headers is None:
|
if headers is None:
|
||||||
headers = dict()
|
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)
|
req = requests.put(const.api_url + url, params=params, json=json, headers=headers)
|
||||||
|
|
||||||
|
@ -13,10 +13,10 @@ logger = logging.getLogger(__name__)
|
|||||||
class NetworkUser(User):
|
class NetworkUser(User):
|
||||||
|
|
||||||
def __init__(self, client_id, client_secret, refresh_token, access_token=None):
|
def __init__(self, client_id, client_secret, refresh_token, access_token=None):
|
||||||
super().__init__('')
|
super().__init__(None)
|
||||||
|
|
||||||
self.accesstoken = access_token
|
self.access_token = access_token
|
||||||
self.refreshtoken = refresh_token
|
self.refresh_token = refresh_token
|
||||||
|
|
||||||
self.client_id = client_id
|
self.client_id = client_id
|
||||||
self.client_secret = client_secret
|
self.client_secret = client_secret
|
||||||
@ -30,9 +30,9 @@ class NetworkUser(User):
|
|||||||
return Color.RED + Color.BOLD + 'NetworkUser' + Color.END + \
|
return Color.RED + Color.BOLD + 'NetworkUser' + Color.END + \
|
||||||
f': {self.username}, {self.display_name}, {self.uri}'
|
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')
|
raise NameError('no refresh token to query')
|
||||||
|
|
||||||
if self.client_id is None:
|
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")
|
idsecret = b64encode(bytes(self.client_id + ':' + self.client_secret, "utf-8")).decode("ascii")
|
||||||
headers = {'Authorization': 'Basic %s' % idsecret}
|
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)
|
now = datetime.now(timezone.utc)
|
||||||
req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)
|
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:
|
if 200 <= req.status_code < 300:
|
||||||
logger.debug('token refreshed')
|
logger.debug('token refreshed')
|
||||||
resp = req.json()
|
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.token_expiry = resp['expires_in']
|
||||||
self.last_refreshed = now
|
self.last_refreshed = now
|
||||||
for func in self.on_refresh:
|
for func in self.on_refresh:
|
||||||
@ -94,7 +96,7 @@ class NetworkUser(User):
|
|||||||
|
|
||||||
def get_info(self) -> Optional[dict]:
|
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)
|
req = requests.get('https://api.spotify.com/v1/me', headers=headers)
|
||||||
|
|
||||||
|
@ -36,7 +36,13 @@ class Player:
|
|||||||
def play(self,
|
def play(self,
|
||||||
context: Union[Context, SpotifyAlbum, SpotifyPlaylist] = None,
|
context: Union[Context, SpotifyAlbum, SpotifyPlaylist] = None,
|
||||||
tracks: List[SpotifyTrack] = 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:
|
if context and tracks:
|
||||||
raise Exception('cant execute context and track list')
|
raise Exception('cant execute context and track list')
|
||||||
if context:
|
if context:
|
||||||
|
Loading…
Reference in New Issue
Block a user