implemented alarm and network device methods

This commit is contained in:
aj 2019-04-30 23:02:29 +01:00
parent b95ca24f28
commit 08a07800dc
2 changed files with 40 additions and 5 deletions

9
alarm.py Normal file
View File

@ -0,0 +1,9 @@
import spotframework.net.user as userclass
import spotframework.net.network as networkclass
import os
if __name__ == '__main__':
network = networkclass.network(userclass.User())
network.play(os.environ['SPOTALARMURI'], network.getDeviceID(os.environ['SPOTALARMDEVICENAME']))

View File

@ -93,18 +93,44 @@ class network:
else:
raise ValueError("Couldn't Pull Playlist " + str(playlistid) + ' ' + str(req.status_code))
def getAvailableDevices(self):
headers = {'Authorization': 'Bearer ' + self.user.access_token}
req = requests.get(const.api_url + 'me/player/devices', headers=headers)
if req.status_code == 200:
return req.json()
else:
return None
def getPlayer(self):
headers = {'Authorization': 'Bearer ' + self.user.access_token}
req = requests.get(const.api_url + 'me/player', headers=headers)
print(req.status_code)
print(req.text)
if req.status_code == 200:
return req.json()
else:
return None
def play(self, context, contexttype, deviceid=None):
def getDeviceID(self, devicename):
return next((i for i in self.getAvailableDevices()['devices'] if i['name'] == devicename), None)['id']
def play(self, uri, deviceid=None):
headers = {'Authorization': 'Bearer ' + self.user.access_token}
params = {'limit': limit}
req = requests.put(const.api_url + 'me/player/play', params=params, headers=headers)
if deviceid is not None:
params = {'device_id': deviceid}
else:
params = None
payload = {'context_uri': uri}
req = requests.put(const.api_url + 'me/player/play', params=params, json=payload, headers=headers)
print(req.status_code)
print(req.text)