def _send_to_kodi(self, command, host=None, username=None, password=None): """Handles communication to KODI servers via HTTP API Args: command: Dictionary of field/data pairs, encoded via urllib and passed to the KODI API via HTTP host: KODI webserver host:port username: KODI webserver username password: KODI webserver password Returns: Returns response.result for successful commands or False if there was an error """ # fill in omitted parameters if not username: username = sickrage.app.config.kodi_username if not password: password = sickrage.app.config.kodi_password if not host: sickrage.app.log.warning('No KODI host passed, aborting update') return False enc_command = urlencode(command) sickrage.app.log.debug("KODI encoded API command: " + enc_command) url = 'http://%s/kodiCmds/kodiHttp/?%s' % (host, enc_command) headers = {} # if we have a password, use authentication if password: authheader = "Basic {}".format( base64.b64encode( bytes('{}:{}'.format(username, password).replace('\n', ''), 'utf-8')).decode('ascii')) headers["Authorization"] = authheader sickrage.app.log.debug( "Contacting KODI (with auth header) via url: " + url) else: sickrage.app.log.debug("Contacting KODI via url: " + url) try: result = WebSession().get(url, headers=headers).text except Exception as e: sickrage.app.log.debug("Couldn't contact KODI HTTP at %r : %r" % (url, e)) return False sickrage.app.log.debug("KODI HTTP response: " + result.replace('\n', '')) return result
def _send_to_plex(self, command, host, username=None, password=None): """Handles communication to Plex hosts via HTTP API Args: command: Dictionary of field/data pairs, encoded via urllib and passed to the legacy xbmcCmds HTTP API host: Plex host:port username: Plex API username password: Plex API password Returns: Returns 'OK' for successful commands or False if there was an error """ # fill in omitted parameters if not username: username = sickrage.app.config.plex_client_username if not password: password = sickrage.app.config.plex_client_password if not host: sickrage.app.log.warning('PLEX: No host specified, check your settings') return False enc_command = urlencode(command) sickrage.app.log.debug('PLEX: Encoded API command: ' + enc_command) url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command) headers = {} # if we have a password, use authentication if password: base64string = base64.b64encode(bytes('{}:{}'.format(username, password).replace('\n', ''), 'utf-8')) authheader = "Basic {}".format(base64string.decode('ascii')) headers['Authorization'] = authheader sickrage.app.log.debug('PLEX: Contacting (with auth header) via url: ' + url) else: sickrage.app.log.debug('PLEX: Contacting via url: ' + url) try: result = WebSession().get(url, headers=headers).text except Exception as e: sickrage.app.log.warning('PLEX: Warning: Couldn\'t contact Plex at {}: {}'.format(url, e)) return False sickrage.app.log.debug('PLEX: HTTP response: ' + result.replace('\n', '')) return 'OK'