def getAppicon(self, auid, name): # if not self._check_paired(pair_reason='getAppicon'): return False # # auid = This is the unique ID of the app, expressed as an 8-byte-long hexadecimal string. # name = App name uri = "/udap/api/data?target=appicon_get&auid={auid}&appname={appname}".format(auid = auid, appname = name) headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8'} url = 'http://{ipaddress}:{port}{uri}'.format(ipaddress=self._ipaddress(), port=str(self._port()), uri=uri) # r = requests.get(url, headers=headers) print_command('getAppicon', self.dvc_or_acc_id(), self._type, self._ipaddress(), r.status_code) # if not r.status_code == requests.codes.ok: self.is_paired = False if not self._check_paired(pair_reason='getAppicon'): return False r = requests.post(url, headers=headers) print_command('getAppicon', self.dvc_or_acc_id(), self._type, self._ipaddress(), r.status_code) # if r.status_code == requests.codes.ok: return r.content else: return False
def _retrieve_recordings(self, recurse, itemCount=''): try: r = requests.get('https://{ipaddress}/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying&Recurse={recurse}{itemCount}'.format(ipaddress=self._ipaddress(), recurse=recurse, itemCount=itemCount), auth=HTTPDigestAuth('tivo', self._accesskey()), verify=False) print_command('retrieve listings (recurse={recurse})'.format(recurse=recurse), self.dvc_or_acc_id(), self._type, self._ipaddress(), r.status_code) if r.status_code == requests.codes.ok: return r.content else: return False except Exception as e: return False
def showPairingkey(self): # STRxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envelope><api type=\"pairing\"><name>showKey</name></api></envelope>" headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8', 'content-type': 'text/xml; charset=utf-8'} url = 'http://{ipaddress}:{port}{uri}'.format(ipaddress=self._ipaddress(), port=str(self._port()), uri=str(self.STRtv_PATHpair)) # r = requests.post(url, STRxml, headers=headers) print_command('showPairingkey', self.dvc_or_acc_id(), self._type, url, r.status_code) # r_pass = True if r.status_code == requests.codes.ok else False # return r_pass
def _getApplist(self, APPtype=3, APPindex=0, APPnumber=0): try: # if not self._check_paired(pair_reason='getApplist'): return False # uri = "/udap/api/data?target=applist_get&type={type}&index={index}&number={number}".format(type=str(APPtype), index=str(APPindex), number=str(APPnumber)) headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8'} url = 'http://{ipaddress}:{port}{uri}'.format(ipaddress=self._ipaddress(), port=str(self._port()), uri=uri) # r = requests.get(url, headers=headers) # print_command('getApplist', self.dvc_or_acc_id(), self._type, self._ipaddress(), r.status_code) # if not r.status_code == requests.codes.ok: self.is_paired = False if not self._check_paired(pair_reason='getApplist'): return False r = requests.post(url, headers=headers) print_command('getApplist', self.dvc_or_acc_id(), self._type, self._ipaddress(), r.status_code) # if r.status_code == requests.codes.ok: return r.content else: return False except: return False
def sendCmd(self, request): # try: code = False response = False # # if request['command'] == 'getHtml_recordings': # response = self._getHtml_recordings() # el if request['command'] == 'getchannel': chan_no = self._getChan() if bool(chan_no): # chan_name = get_channel_name_from_devicekey(self._type, chan_no) # chan_logo = get_channel_logo_from_devicekey(self._type, chan_no) chan_logo = chan_logo if not chan_logo=='-' else 'ic_blank.png' # response = '{"chan_no": ' + str(chan_no) + ', ' + \ '"chan_name": "' + chan_name + '", ' + \ '"chan_logo": "' + chan_logo + '"}' else: response = False elif request['command'] == 'channel': response = self._send_telnet(ipaddress=self._ipaddress(), port=self._port(), data=("SETCH {}\r").format(request['chan']), response=True) if response.startswith('CH_FAILED'): print_command('channel', self.dvc_or_acc_id(), self._type, self._ipaddress(), response) return False elif request['command'] == 'command': code = self.commands[request['code']] try: response = self._send_telnet(self._ipaddress(), self._port(), data=code) except: response = False # x = request['code'] if code else request['command'] print_command (x, self.dvc_or_acc_id(), self._type, self._ipaddress(), response) return response except: print_command(request['command'], self.dvc_or_acc_id(), self._type, self._ipaddress(), 'ERROR') return False
def _pairDevice(self, pair_reason=''): # command = 'Device pairing' if not pair_reason=='': command += '\' - \'{pair_reason}'.format(pair_reason=pair_reason) # STRxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><envelope><api type=\"pairing\"><name>hello</name><value>{}</value><port>{}</port></api></envelope>".format(self._pairingkey(), str(self._port())) headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8', 'content-type': 'text/xml; charset=utf-8'} url = 'http://{ipaddress}:{port}{uri}'.format(ipaddress=self._ipaddress(), port=str(self._port()), uri=str(self.STRtv_PATHpair)) # try: r = requests.post(url, STRxml, headers=headers) print_command(command, self.dvc_or_acc_id(), self._type, url, r.status_code) # r_pass = True if r.status_code == requests.codes.ok else False self.is_paired = r_pass # return r_pass except requests.exceptions.ConnectionError as e: print_command(command, self.dvc_or_acc_id(), self._type, self._ipaddress(), 'ERROR: connection error') return False except Exception as e: print_command(command, self.dvc_or_acc_id(), self._type, self._ipaddress(), 'ERROR: {error}'.format(error=e)) return False
def sendCmd(self, request): # try: # if not self._check_paired(pair_reason=request['command']): return False # if request['command'] == 'image': response = self.getAppicon(request['auid'], request['name'].replace(' ','%20')) return response # else: if request['command'] == 'app': STRxml = ('<?xml version="1.0" encoding="utf-8"?>' + '<envelope>' + '<api type="command">' + '<name>AppExecute</name>' + '<auid>{auid}</auid>' + '<appname>{app_name}</appname>' + #'<contentId>Content ID</contentId>' + '</api>' + '</envelope>').format(auid = request['auid'], app_name = request['name'].replace(' ','%20')) headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8', 'content-type': 'text/xml; charset=utf-8'} cmd = request['command'] else: code = self.commands[request['command']] STRxml = ('<?xml version="1.0" encoding="utf-8"?>' + '<envelope>' + '<api type="command">' + '<name>HandleKeyInput</name>' + '<value>{value}</value>' + '</api>' + '</envelope>').format(value=code) headers = {'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8', 'content-type': 'text/xml; charset=utf-8'} cmd = request['command'] # url = 'http://{ipaddress}:{port}{uri}'.format(ipaddress=self._ipaddress(), port=str(self._port()), uri=str(self.STRtv_PATHcommand)) r = requests.post(url, STRxml, headers=headers) print_command('command', self.dvc_or_acc_id(), self._type, url, r.status_code) # if not r.status_code == requests.codes.ok: self.is_paired = False if not self._check_paired(pair_reason='command'): return False r = requests.post(url, STRxml, headers=headers) print_command('command', self.dvc_or_acc_id(), self._type, url, r.status_code) # response = (r.status_code == requests.codes.ok) print_command (cmd, self.dvc_or_acc_id(), self._type, url, response) return response # except: print_command (request['command'], self.dvc_or_acc_id(), self._type, self._ipaddress(), 'ERROR: Exception encountered') return False
def sendCmd(self, request): # try: # cmd = self.commands[request['command']] # ipencoded = base64.b64encode(my_ip().encode('ascii')) # macencoded = base64.b64encode(my_mac()) # # Open Socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((self._ipaddress(), 55000)) # ################################ # Authentication sock.send(chr(0x00)) sock.send(self.writeText(self.appstring)) sock.send(chr(0x64) + chr(0x00)) sock.send(self.writeText(ipencoded)) sock.send(self.writeBase64Text(self.appname)) sock.send(self.writeBase64Text(self.appname)) # data = sock.recv(4096) # print(data.encode("ascii")) # # TODO - check against ALLOWED_BYTES, DENIED_BYTES and TIMEOUT_BYTES # # msg = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \ # + chr(0x00) + ipencoded + chr(len(macencoded)) \ # + chr(0x00) + macencoded + chr(len(base64.b64encode(self.appname))) \ # + chr(0x00) + base64.b64encode(self.appname) # msg = chr(0x00) + chr(len(self.appstring)) + chr(0x00) + self.appstring + chr(len(msg)) + chr(0x00) + msg # sock.send(msg) # # # data = sock.recv(4096) # data = data.encode("UTF-8") # ################################ # Send command sock.send(chr(0x00)) sock.send(self.writeText(self.tvappstring)) sock.send(chr(0x00)) sock.send(chr(0x00)) sock.send(chr(0x00)) sock.send(self.writeBase64Text(cmd)) # data = sock.recv(4096) # print(data.encode("ascii")) # # # messagepart2 = chr(0xc8) + chr(0x00) # part2 = chr(0x00) + chr(len(self.appstring)) + chr(0x00) + self.appstring + chr(len(messagepart2)) + chr(0x00) + messagepart2 # sock.send(part2) # # # messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(base64.b64encode(_skey))) + chr(0x00) + base64.b64encode(_skey) # part3 = chr(0x00) + chr(len(self.appstring)) + chr(0x00) + self.appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3 # sock.send(part3) # ################################ # sock.close() # response = True print_command (cmd, self.dvc_or_acc_id(), self._type, self._ipaddress(), response) return response # ################################ except: print_command (request['command'], self.dvc_or_acc_id(), self._type, self._ipaddress(), 'ERROR: Exception encountered') return False