def getlisting(src, value): if value != '': if src == 'radiotimes': return tvlisting_radiotimes.getlisting(value) else: print_msg('No known listing source available') else: print_msg('No code provided for listing source ' + src) return None
def _tokencheck(self): print_msg('Checking Auth Token', dvc_or_acc_id=self.dvc_or_acc_id()) if bool(self._pincode): if self._checkToken(): return True else: return self._getNewToken() else: return False
def get_apps(self): while self._active: # Reset values self._get_apps() # ############ # Potential to convert xml into array/list here instead of within HTML creation def ############ # print_msg('TV Apps list retrieved: {type}'.format(type=self._type), dvc_or_acc_id=self.dvc_or_acc_id()) time.sleep(600) # 600 = 10 minutes
def create_device_threads(q_devices, q_accounts, queues): # t = 0 thread = [] # r_list = get_cfg_idlist_rooms() r_num = 0 # while r_num < len(r_list): d_list = get_cfg_idlist_devices(r_list[r_num]) d_num = 0 # while d_num < len(d_list): # thread.append(threading.Thread(target=_create_device, args=(r_list[r_num], d_list[d_num], q_devices[r_num][d_num], queues))) thread[t].daemon = True thread[t].start() # print_msg('Thread created: {type}'.format(type=get_cfg_device_type(r_list[r_num], d_list[d_num])), dvc_or_acc_id=r_list[r_num]+':'+d_list[d_num]) # t += 1 d_num += 1 r_num += 1 # # a_list = get_cfg_idlist_accounts() a_num = 0 # while a_num < len(a_list): # thread.append(threading.Thread(target=_create_account, args=(a_list[a_num], q_accounts[a_num], queues))) thread[t].daemon = True thread[t].start() # print_msg('Thread created: {type}'.format(type=get_cfg_account_type(a_list[a_num])), dvc_or_acc_id=a_list[a_num]) # t += 1 a_num += 1 # x = 0 while x < t: thread[x].join()
def run(self): time.sleep(5) while self._active: # Keep in a loop time.sleep(0.1) qItem = self._getFromQueue() if bool(qItem): if qItem['response_queue'] == 'stop': self._active = False elif qItem['response_queue'] == cfg.key_q_response_web_device: self._q_response_web.put(self.getHtml(user=qItem['user'])) elif qItem['response_queue'] == cfg.key_q_response_command: self._q_response_cmd.put(self.sendCmd(qItem['request'])) else: # Code to go here to handle other items added to the queue!! True print_msg('Thread stopped: {type}'.format(type=self._type), dvc_or_acc_id=self.dvc_or_acc_id())
def get_recordings(self): while self._active: # Reset values self.recordings_timestamp = False self.recordings_folders = False self.recordings_files = [] # try: self.recordings_timestamp = datetime.datetime.now() self.recordings_folders = self._retrieve_recordings('No').replace(' xmlns="http://www.tivo.com/developer/calypso-protocol-1.6/"','') #self.recordings_files = self._retrieve_recordings('Yes', itemCount='&ItemCount=50').replace(' xmlns="http://www.tivo.com/developer/calypso-protocol-1.6/"','') # ############ # retrieve_items = 50 # files_repeat = True loop_count = 0 itemCount = '&ItemCount={retrieve_items}'.format(retrieve_items=retrieve_items) # while files_repeat: files_count = 0 files = self._retrieve_recordings('Yes', itemCount=itemCount).replace(' xmlns="http://www.tivo.com/developer/calypso-protocol-1.6/"','') xml_files = ET.fromstring(files) # Run through individual items for item in xml_files.iter('Item'): self.recordings_files.append(item.find('Details')) files_count += 1 # if files_count<50: files_repeat = False else: loop_count += 1 itemCount = '&ItemCount={retrieve_items}&AnchorOffset={AnchorOffset}'.format(retrieve_items=retrieve_items, AnchorOffset=(retrieve_items*loop_count)) # ############ # print_msg('TV recording information retrieved: {type}'.format(type=self._type), dvc_or_acc_id=self.dvc_or_acc_id()) except: self.recordings_timestamp = 0 self.recordings_folders = '' # time.sleep(600) # 600 = 10 minutes
def _getNewToken(self): # url = 'https://api.home.nest.com/oauth2/access_token?code={PIN_CODE}&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}&grant_type=authorization_code'.format(PIN_CODE=self._pincode, CLIENT_ID=self._clientid(), CLIENT_SECRET=self._clientsecret()) # headers = {'Connection': 'close', 'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8'} # r = requests.post(url, headers=headers) # if r.status_code != requests.codes.ok: print_error('Auth code not received by Nest server', dvc_or_acc_id=self.dvc_or_acc_id()) return False # try: response = r.content except Exception as e: print_error('Auth code not received by Nest server - ' + str(e), dvc_or_acc_id=self.dvc_or_acc_id()) return False # if response: try: data = json.loads(response) except Exception as e: print_error('Auth code not processed into json object - ' + str(e), dvc_or_acc_id=self.dvc_or_acc_id()) return False # exp = datetime.datetime.now() + datetime.timedelta(milliseconds=data['expires_in']) # set_cfg_account_detail(self._account_id, 'token', data['access_token']) set_cfg_account_detail(self._account_id, 'tokenexpiry', exp.strftime(self._dateformat)) # self._token = data['access_token'] self._tokenexpiry = exp # print_msg('Success retrieving new Access Token', dvc_or_acc_id=self.dvc_or_acc_id()) # return True else: return False
def run(self): time.sleep(5) while self._active: # Keep in a loop ''' Use of self._active allows for object to close itself, however may wish to take different approach of terminating the thread the object loop resides in ''' time.sleep(0.1) qItem = self._getFromQueue() if bool(qItem): if qItem['response_queue'] == 'stop': self._active = False elif qItem['response_queue'] == cfg.key_q_response_web_device: self._q_response_web.put(self.getHtml(user=qItem['user'])) elif qItem['response_queue'] == cfg.key_q_response_command: self._q_response_cmd.put(self.sendCmd(qItem['request'])) else: # Code to go here to handle other items added to the queue!! True print_msg('Thread stopped: {type}'.format(type=self._type), dvc_or_acc_id=self.dvc_or_acc_id())
def server_start(): # ################################ # Create response queue dict ################################ queues = {cfg.key_q_response_data: q_response_data, cfg.key_q_response_command: q_response_comand, cfg.key_q_tvlistings: q_listings} # ################################ # Process for port_listener ################################ print_msg('Starting process: "bottle" server for port {port}'.format(port=cfg.port_server)) process_bottle = Process(target=start_bottle, args=(device_queues, account_queues, queues, )) process_bottle.start() print_msg('Process started: "bottle" server for port {port}'.format(port=cfg.port_server)) # ################################ # Process for device threads ################################ print_msg('Starting process: Creation of device threads') process_devices = Process(target=create_device_threads, args=(device_queues, account_queues, queues, )) process_devices.start() print_msg('Process started: Creation of device threads') # ################################ # Process to retrieve TV listings. # !!!! Not currently in use due to decommision of radiotimes API !!!! ################################ # print_msg('Starting process: Retrieval of TV listings') # process_listings = Process(target=tvlistings_process) # process_listings.start() # print_msg('Process started: Retrieval of TV listings') # ################################ # Use .join() to ensure main process with Manager() items remains open ################################ process_bottle.join() process_devices.join()