def verifyAccount(self, account): """ Check if account credentials are good """ print "-- Calling Service: Authentication" try: resp = self.call("initClient", account, "POST") if (self.debug): print resp if ('content' in resp): deviceList = getJSONValue(resp, ['content']) print " -> Your account has %s associated devices." % len( deviceList) print " -> Authentication successful. Adding account credentials to OpenHab." self.accounts[account]['name'] = getJSONValue( resp, ['userInfo', 'firstName']) + ' ' + getJSONValue( resp, ['userInfo', 'lastName']) self.saveCredentials() else: print " \033[91m-> Error connecting to Apple: Invalid Credentials\033[0;0m" self.loadCredentials() except: print " \033[91m-> Error Authentication: " + str( sys.exc_info()[1]) + "\033[0;0m" self.loadCredentials()
def update(self): """ Get a current player state. """ print "-- Calling Service: Update" try: resp = self.call("") if (self.debug): print resp if ('item' in resp): self.oh.sendCommand('spotify_current_track', getJSONValue(resp, ['item','name'])) self.oh.sendCommand('spotify_current_artist', getJSONValue(resp, ['item', 'artists', 0, 'name'])) self.oh.sendCommand('spotify_current_cover', getJSONValue(resp, ['item', 'album', 'images', 1, 'url'])) self.oh.sendCommand('spotify_current_duration', getJSONValue(resp, ['item', 'duration_ms'])) self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms'])) self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms'])) self.oh.sendCommand('spotify_current_playing', mapValues(getJSONValue(resp, ['is_playing']), { 'True': 'ON', 'False': 'OFF' })) self.oh.sendCommand('spotify_current_device', getJSONValue(resp, ['device', 'name'])) self.oh.sendCommand('spotify_current_volume', getJSONValue(resp, ['device', 'volume_percent'])) self.oh.sendCommand('spotify_current_device_id', getJSONValue(resp, ['device', 'id'])) print " -> Success" else: print " -> Item node missing from response :(" except: print " -> Failure: ", sys.exc_info()[0] resp = "" return resp
def updateDevices(self): """ Get List of Devices. """ print "-- Calling Service: Update Devices" try: devices = {} for account in self.accounts: print "Getting devices for " + account + ":" resp = self.call("initClient", account, "POST") if (self.debug): print resp if ('content' in resp): deviceList = getJSONValue(resp, ['content']) print "Apple responded with %s devices." % len(deviceList) devices[account] = { 'name': self.accounts[account]['name'], 'deviceList': deviceList } else: print " \033[91m-> Error connecting to Apple: Invalid Credentials\033[0;0m" self.devices = devices self.oh.sendCommand( 'apple_device_list', json.dumps(devices, sort_keys=True, indent=4, separators=(',', ': '))) except: print " -> Error Get Devices: " + str(sys.exc_info()[1])
def devices(self, name=None, idNum=None, devIndex=None): """ Get a current player devices and helper function to play device. """ print "-- Calling Service: get devices" exitStatus = " -> Success" selected_device = "" if (name) or (idNum) or (devIndex): exitStatus = "" if (devIndex): iIndex = int(devIndex) else: iIndex = -1 arrayDesc = [name, idNum, iIndex] try: resp = self.call("devices") if (self.debug): print resp if ('devices' in resp): self.oh.sendCommand('spotify_devices', json.dumps(resp)) initOrder = 0 partial = "" j = 0 k = 1 while (exitStatus == ""): idx = 1 searchName = arrayDesc[(j % 3)] print "Match :", searchName, " or ", arrayDesc[( (1 + j) % 3)], "index: ", iIndex intmed = str(searchName) print intmed print json.dumps(searchName) for i in resp['devices']: loopName = getJSONValue(i, ['name']) searchid = getJSONValue(i, ['id']) print idx, "Device : ", loopName, "id :", searchid if (arrayDesc[(j % 3)] == loopName) or (arrayDesc[( (1 + j) % 3)] == searchid) or (iIndex == idx): self.oh.sendCommand('spotify_device_name', loopName) self.oh.sendCommand('spotify_device_id', searchid) self.oh.sendCommand('spotify_device_index', idx) exitStatus = "Match Sucess" selected_device = searchid return selected_device # Performs partial match serach if no results found if (searchName): if (loopName.lower() in "cain cane kain"): loopName = "cain cane kain" if (searchName.lower() in loopName.lower()): partial = searchid idx = idx + 1 #Looks at the various inputs and seraches on each input in various fields iIndex = "" j += 1 if (j > 3): exitStatus = "FAILED" if (j == k): k = 2 dev_desp = "" if (idNum): dev_desp = idNum idNum = "" j = 0 initOrder = 1 print "idNum serach" elif (devIndex): dev_desp = devIndex devIndex = "" j = 0 iIndex = -1 initOrder = 2 print "devinx serach" elif (name): dev_desp = name j = 0 name = "" initOrder = 0 print "name serach" if (dev_desp): smart_index = self.device_match(dev_desp) if (smart_index > 0): arrayDesc[smart_index] = dev_desp if (smart_index == 2): iIndex = int(dev_desp) if (smart_index == initOrder): j = j + 1 elif (partial): k = 10 j = 3 arrayDesc[1] = partial print "partial serach" print exitStatus else: print " -> Device list error :(" except: print " -> Failure: ", sys.exc_info()[0] resp = "" return selected_device
def update(self): """ Get a current player state. Dont run this straight after a change, you get back old results overwriting anything done locally """ print "-- Calling Service: Update" try: resp = self.call("") if (self.debug): print resp if ('item' in resp): self.oh.sendCommand('spotify_current_track', getJSONValue(resp, ['item', 'name'])) self.oh.sendCommand( 'spotify_current_artist', getJSONValue(resp, ['item', 'artists', 0, 'name'])) self.oh.sendCommand( 'spotify_current_cover', getJSONValue(resp, ['item', 'album', 'images', 1, 'url'])) self.oh.sendCommand( 'spotify_current_duration', getJSONValue(resp, ['item', 'duration_ms'])) self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms'])) self.oh.sendCommand( 'spotify_current_update_time', time.strftime("%Y-%m-%dT%H:%M:%S.000+0000", time.gmtime(time.time()))) self.oh.sendCommand( 'spotify_current_playing', mapValues(getJSONValue(resp, ['is_playing']), { 'True': 'ON', 'False': 'OFF' })) self.oh.sendCommand('spotify_current_device', getJSONValue(resp, ['device', 'name'])) self.oh.sendCommand( 'spotify_current_volume', getJSONValue(resp, ['device', 'volume_percent'])) self.oh.sendCommand('spotify_current_device_id', getJSONValue(resp, ['device', 'id'])) self.oh.sendCommand('spotify_current_context_uri', getJSONValue(resp, ['context', 'uri'])) self.oh.sendCommand('spotify_playing', getJSONValue(resp, ['context', 'type'])) print " -> Success" else: print " -> Playing local file no details available" except: print " -> Failure: ", sys.exc_info()[0] resp = "" return resp
def update(self): """ Get a current player state. """ try: resp = self.call("") if ('item' in resp): self.oh.sendCommand('spotify_current_track', getJSONValue(resp, ['item', 'name'])) self.oh.sendCommand( 'spotify_current_artist', getJSONValue(resp, ['item', 'artists', 0, 'name'])) self.oh.sendCommand( 'spotify_current_cover', getJSONValue(resp, ['item', 'album', 'images', 1, 'url'])) self.oh.sendCommand( 'spotify_current_duration', getJSONValue(resp, ['item', 'duration_ms'])) self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms'])) self.oh.sendCommand( 'spotify_current_playing', mapValues(getJSONValue(resp, ['is_playing']), { 'True': 'ON', 'False': 'OFF' })) self.oh.sendCommand('spotify_current_device', getJSONValue(resp, ['device', 'name'])) self.oh.sendCommand( 'spotify_current_volume', getJSONValue(resp, ['device', 'volume_percent'])) self.oh.sendCommand('spotify_current_context_uri', getJSONValue(resp, ['context', 'uri'])) self.oh.sendCommand('spotify_current_device_id', getJSONValue(resp, ['device', 'id'])) duration = getJSONValue(resp, ['item', 'duration_ms']) progress = getJSONValue(resp, ['progress_ms']) if (duration is not None and progress is not None): progress_percent = round( float(progress) / float(duration) * 100, 2) else: progress_percent = 0 self.oh.sendCommand('spotify_current_progress_percent', progress_percent) else: pass except: resp = "" return resp
def update(self): """ Get a current player state. """ print("-- Calling Service: Update") try: resp = self.call("") if (self.debug): print(resp) if ('item' in resp): self.oh.sendCommand('spotify_current_track', getJSONValue(resp, ['item', 'name'])) self.oh.sendCommand( 'spotify_current_artist', getJSONValue(resp, ['item', 'artists', 0, 'name'])) self.oh.sendCommand( 'spotify_current_cover', getJSONValue(resp, ['item', 'album', 'images', 1, 'url'])) self.oh.sendCommand( 'spotify_current_duration', getJSONValue(resp, ['item', 'duration_ms'])) self.oh.sendCommand('spotify_current_progress', getJSONValue(resp, ['progress_ms'])) self.oh.sendCommand( 'spotify_current_playing', mapValues(getJSONValue(resp, ['is_playing']), { 'True': 'ON', 'False': 'OFF' })) self.oh.sendCommand('spotify_current_device', getJSONValue(resp, ['device', 'name'])) self.oh.sendCommand( 'spotify_current_volume', getJSONValue(resp, ['device', 'volume_percent'])) self.oh.sendCommand('spotify_current_context_uri', getJSONValue(resp, ['context', 'uri'])) self.oh.sendCommand('spotify_current_device_id', getJSONValue(resp, ['device', 'id'])) duration = getJSONValue(resp, ['item', 'duration_ms']) progress = getJSONValue(resp, ['progress_ms']) if (duration is not None and progress is not None): progress_percent = round( float(progress) / float(duration) * 100, 2) else: progress_percent = 0 self.oh.sendCommand('spotify_current_progress_percent', progress_percent) print(" -> Success") else: print(" -> Item node missing from response :(") except: print(" -> Failure: ", sys.exc_info()[0]) resp = "" return resp
def devices(self, name = None, idNum = None, devIndex = None): """ Get a current player devices. """ print "-- Calling Service: get devices" exitStatus=" -> Success" selected_device="" if (name) or (idNum) or (devIndex): exitStatus = "" if (devIndex) : iIndex = int(devIndex) else: iIndex = -1 arrayDesc=[name,idNum,iIndex] try: resp = self.call("devices") if (self.debug): print resp if ('devices' in resp): self.oh.sendCommand('spotify_devices', json.dumps(resp)) initOrder = 0 partial = "" j = 0 k = 1 while(exitStatus == ""): idx = 1 for i in resp['devices']: print "debug startz\n" print getJSONValue(i, ['name']) print getJSONValue(i, ['id']) print idx loopName = getJSONValue(i, ['name']) searchName = arrayDesc[(j%3)] searchid = getJSONValue(i, ['id']) if (arrayDesc[(j%3)] == loopName) or (arrayDesc[((1 + j)%3)] == searchid) or (iIndex == idx ): self.oh.sendCommand('spotify_device_name', loopName) self.oh.sendCommand('spotify_device_id', searchid) self.oh.sendCommand('spotify_device_index', idx) exitStatus="Match Sucess" selected_device = searchid return selected_device if (searchName): if searchName.lower() in loopName.lower(): partial = searchid idx = idx + 1 iIndex="" j+=1 if( j > 3): exitStatus="FAILED" if (j == k): k = 2 dev_desp="" if (idNum): dev_desp = idNum idNum="" j = 0 initOrder = 1 print "idNum serach" elif (devIndex) : dev_desp = devIndex devIndex="" j = 0 iIndex=-1 initOrder = 2 print "devinx serach" elif (name) : dev_desp = name j = 0 name = "" initOrder = 0 print "name serach" if (dev_desp): smart_index = self.device_match(dev_desp) if(smart_index > 0): arrayDesc[smart_index] = dev_desp if (smart_index == 2): iIndex = int(dev_desp) if (smart_index == initOrder): j = j + 1 elif (partial): k = 10 j = 3 arrayDesc[1] = partial print "partial serach" print exitStatus else: print " -> Device list error :(" except: print " -> Failure: ", sys.exc_info()[0] resp = "" return selected_device