本文整理汇总了Python中sickbeard.encodingKludge.fixStupidEncodings函数的典型用法代码示例。如果您正苦于以下问题:Python fixStupidEncodings函数的具体用法?Python fixStupidEncodings怎么用?Python fixStupidEncodings使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fixStupidEncodings函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ex
def ex(e):
"""
Returns a unicode string from the exception text if it exists.
"""
e_message = u""
if not e or not e.args:
return e_message
for arg in e.args:
if arg is not None:
if isinstance(arg, (str, unicode)):
fixed_arg = fixStupidEncodings(arg, True)
else:
try:
fixed_arg = u"error " + fixStupidEncodings(str(arg), True)
except:
fixed_arg = None
if fixed_arg:
if not e_message:
e_message = fixed_arg
else:
e_message = e_message + " : " + fixed_arg
return e_message
示例2: _send_to_xbmc_json
def _send_to_xbmc_json(self, command, host=None, username=None, password=None):
"""Handles communication to XBMC servers via JSONRPC
Args:
command: Dictionary of field/data pairs, encoded via urllib and passed to the XBMC JSON-RPC via HTTP
host: XBMC webserver host:port
username: XBMC webserver username
password: XBMC webserver password
Returns:
Returns response.result for successful commands or False if there was an error
"""
# fill in omitted parameters
if not username:
username = sickbeard.XBMC_USERNAME
if not password:
password = sickbeard.XBMC_PASSWORD
if not host:
logger.log(u"No XBMC host passed, aborting update", logger.DEBUG)
return False
command = command.encode("utf-8")
logger.log(u"XBMC JSON command: " + command, logger.DEBUG)
url = "http://%s/jsonrpc" % (host)
try:
req = urllib2.Request(url, command)
req.add_header("Content-type", "application/json")
# if we have a password, use authentication
if password:
base64string = base64.encodestring("%s:%s" % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"Contacting XBMC (with auth header) via url: " + fixStupidEncodings(url), logger.DEBUG)
else:
logger.log(u"Contacting XBMC via url: " + fixStupidEncodings(url), logger.DEBUG)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
logger.log(
u"Error while trying to retrieve XBMC API version for " + host + ": " + ex(e), logger.WARNING
)
return False
# parse the json result
try:
result = json.load(response)
response.close()
logger.log(u"XBMC JSON response: " + str(result), logger.DEBUG)
return result # need to return response for parsing
except ValueError, e:
logger.log(u"Unable to decode JSON: " + response, logger.WARNING)
return False
示例3: _send_to_xbmc
def _send_to_xbmc(self, command, host=None, username=None, password=None):
"""Handles communication to XBMC servers via HTTP API
Args:
command: Dictionary of field/data pairs, encoded via urllib and passed to the XBMC API via HTTP
host: XBMC webserver host:port
username: XBMC webserver username
password: XBMC webserver password
Returns:
Returns response.result for successful commands or False if there was an error
"""
# fill in omitted parameters
if not username:
username = sickbeard.XBMC_USERNAME
if not password:
password = sickbeard.XBMC_PASSWORD
if not host:
logger.log(u"No XBMC host passed, aborting update", logger.DEBUG)
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode("utf-8")
enc_command = urllib.urlencode(command)
logger.log(u"XBMC encoded API command: " + enc_command, logger.DEBUG)
url = "http://%s/xbmcCmds/xbmcHttp/?%s" % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
base64string = base64.encodestring("%s:%s" % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"Contacting XBMC (with auth header) via url: " + fixStupidEncodings(url), logger.DEBUG)
else:
logger.log(u"Contacting XBMC via url: " + fixStupidEncodings(url), logger.DEBUG)
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
logger.log(u"XBMC HTTP response: " + result.replace("\n", ""), logger.DEBUG)
return result
except (urllib2.URLError, IOError), e:
logger.log(
u"Warning: Couldn't contact XBMC HTTP at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING
)
return False
示例4: _send_to_xbmc_json
def _send_to_xbmc_json(self, command, host=None, username=None, password=None):
"""Handles communication to XBMC servers via JSONRPC
Args:
command: Dictionary of field/data pairs, encoded via urllib and passed to the XBMC JSON-RPC via HTTP
host: XBMC webserver host:port
username: XBMC webserver username
password: XBMC webserver password
Returns:
Returns response.result for successful commands or False if there was an error
"""
if not host:
self._log_debug(u'No host passed, aborting update')
return False
username = self._choose(username, sickbeard.XBMC_USERNAME)
password = self._choose(password, sickbeard.XBMC_PASSWORD)
command = command.encode('utf-8')
self._log_debug(u'JSON command: ' + command)
url = 'http://%s/jsonrpc' % host
try:
req = urllib2.Request(url, command)
req.add_header('Content-type', 'application/json')
# if we have a password, use authentication
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = 'Basic %s' % base64string
req.add_header('Authorization', authheader)
self._log_debug(u'Contacting (with auth header) via url: ' + fixStupidEncodings(url))
else:
self._log_debug(u'Contacting via url: ' + fixStupidEncodings(url))
try:
response = urllib2.urlopen(req)
except urllib2.URLError as e:
self._log_warning(u'Error while trying to retrieve API version for "%s": %s' % (host, ex(e)))
return False
# parse the json result
try:
result = json.load(response)
response.close()
self._log_debug(u'JSON response: ' + str(result))
return result # need to return response for parsing
except ValueError:
self._log_warning(u'Unable to decode JSON: ' + response)
return False
except IOError as e:
self._log_warning(u'Couldn\'t contact JSON API at ' + fixStupidEncodings(url) + ' ' + ex(e))
return False
示例5: _send_to_xbmc
def _send_to_xbmc(self, command, host=None, username=None, password=None):
"""Handles communication to XBMC servers via HTTP API
Args:
command: Dictionary of field/data pairs, encoded via urllib and passed to the XBMC API via HTTP
host: XBMC webserver host:port
username: XBMC webserver username
password: XBMC webserver password
Returns:
Returns response.result for successful commands or False if there was an error
"""
if not host:
self._log_debug(u'No host passed, aborting update')
return False
username = self._choose(username, sickbeard.XBMC_USERNAME)
password = self._choose(password, sickbeard.XBMC_PASSWORD)
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
self._log_debug(u'Encoded API command: ' + enc_command)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = 'Basic %s' % base64string
req.add_header('Authorization', authheader)
self._log_debug(u'Contacting (with auth header) via url: ' + fixStupidEncodings(url))
else:
self._log_debug(u'Contacting via url: ' + fixStupidEncodings(url))
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
self._log_debug(u'HTTP response: ' + result.replace('\n', ''))
return result
except (urllib2.URLError, IOError) as e:
self._log_warning(u'Couldn\'t contact HTTP at %s %s' % (fixStupidEncodings(url), ex(e)))
return False
示例6: _sendXBMCAPIRequest
def _sendXBMCAPIRequest(self, host, url, username=None, password=None):
'''
Handles HTTP communication with XBMC servers
url - Preformatted HTTP / JSON API Url
'''
if not username:
username = self._username()
if not password:
password = self._password()
try:
# If we have a password, use authentication
req = urllib2.Request(url)
if password:
logger.log(u"Adding Password to XBMC url", logger.DEBUG)
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"Contacting XBMC via url: " + url, logger.DEBUG)
handle = urllib2.urlopen(req, None, 5)
response = handle.read()
logger.log(u"response: " + response, logger.DEBUG)
except IOError, e:
logger.log(u"Warning: Couldn't contact XBMC HTTP server at " + fixStupidEncodings(host) + ": " + ex(e))
response = ''
示例7: _send_to_plex
def _send_to_plex(self, command, host, username=None, password=None):
"""Handles communication to Plex hosts via HTTP API
def notify_subtitle_download(self, ep_name, lang):
if sickbeard.PLEX_NOTIFY_ONSUBTITLEDOWNLOAD:
self._notifyXBMC(ep_name + ": " + lang, common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD])
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 = sickbeard.PLEX_USERNAME
if not password:
password = sickbeard.PLEX_PASSWORD
if not host:
logger.log(u"No Plex host specified, check your settings", logger.DEBUG)
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
logger.log(u"Plex encoded API command: " + enc_command, logger.DEBUG)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"Contacting Plex (with auth header) via url: " + url, logger.DEBUG)
else:
logger.log(u"Contacting Plex via url: " + url, logger.DEBUG)
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
logger.log(u"Plex HTTP response: " + result.replace('\n', ''), logger.DEBUG)
# could return result response = re.compile('<html><li>(.+\w)</html>').findall(result)
return 'OK'
except (urllib2.URLError, IOError), e:
logger.log(u"Warning: Couldn't contact Plex at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING)
return False
示例8: _send_to_kodi
def _send_to_kodi(self, command, host=None, username=None, password=None):
# fill in omitted parameters
if not username:
username = sickbeard.KODI_USERNAME
if not password:
password = sickbeard.KODI_PASSWORD
if not host:
logger.log(u'KODI: No host specified, check your settings', logger.ERROR)
return False
data = json.dumps(command)
logger.log(u'KODI: JSON command: %s' % data, logger.DEBUG)
url = 'http://%s/jsonrpc' % host
headers = {'Content-type': 'application/json'}
# if we have a password, use authentication
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = 'Basic %s' % base64string
headers['Authorization'] = authheader
logger.log(u'KODI: Contacting (with auth header) via url: %s' % fixStupidEncodings(url), logger.DEBUG)
else:
logger.log(u'KODI: Contacting via url: %s' % fixStupidEncodings(url), logger.DEBUG)
try:
response = requests.post(url, data=data, headers=headers)
except Exception as e:
logger.log(u'KODI: Warning: Couldn\'t contact Kodi at %s - %s' % (host, ex(e)), logger.WARNING)
return False
if response.status_code == 401:
logger.log(u'KODI: Invalid login credentials', logger.ERROR)
return False
# parse the json result
try:
result = response.json()
logger.log(u'KODI: JSON response: %s' % result, logger.DEBUG)
return result # need to return response for parsing
except ValueError as e:
logger.log(u'KODI: Unable to decode JSON response: %s' % response.text, logger.WARNING)
return False
示例9: _send_to_plex
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 = sickbeard.PLEX_USERNAME
if not password:
password = sickbeard.PLEX_PASSWORD
if not host:
logger.log(u"PLEX: No host specified, check your settings", logger.ERROR)
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode("utf-8")
enc_command = urllib.urlencode(command)
logger.log(u"PLEX: Encoded API command: " + enc_command, logger.DEBUG)
url = "http://%s/xbmcCmds/xbmcHttp/?%s" % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
base64string = base64.encodestring("%s:%s" % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"PLEX: Contacting (with auth header) via url: " + url, logger.DEBUG)
else:
logger.log(u"PLEX: Contacting via url: " + url, logger.DEBUG)
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
logger.log(u"PLEX: HTTP response: " + result.replace("\n", ""), logger.DEBUG)
# could return result response = re.compile('<html><li>(.+\w)</html>').findall(result)
return "OK"
except (urllib2.URLError, IOError), e:
logger.log(
u"PLEX: Warning: Couldn't contact Plex at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING
)
return False
示例10: _send_to_kodi
def _send_to_kodi(self, command, host=None, username=None, password=None):
# fill in omitted parameters
if not username:
username = sickbeard.KODI_USERNAME
if not password:
password = sickbeard.KODI_PASSWORD
if not host:
logger.log(u'KODI: No host specified, check your settings', logger.ERROR)
return False
command = command.encode('utf-8')
logger.log(u'KODI: JSON command: ' + command, logger.DEBUG)
url = 'http://%s/jsonrpc' % (host)
try:
req = urllib2.Request(url, command)
req.add_header('Content-type', 'application/json')
# if we have a password, use authentication
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = 'Basic %s' % base64string
req.add_header('Authorization', authheader)
logger.log(u'KODI: Contacting (with auth header) via url: ' + fixStupidEncodings(url), logger.DEBUG)
else:
logger.log(u'KODI: Contacting via url: ' + fixStupidEncodings(url), logger.DEBUG)
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
logger.log(u'KODI: Warning: Couldn\'t contact Kodi at ' + host + '- ' + ex(e), logger.WARNING)
return False
# parse the json result
try:
result = json.load(response)
response.close()
logger.log(u'KODI: JSON response: ' + str(result), logger.DEBUG)
return result # need to return response for parsing
except ValueError, e:
logger.log(u'KODI: Unable to decode JSON response: ' + response, logger.WARNING)
return False
示例11: _send_to_plex
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 = sickbeard.PLEX_USERNAME
if not password:
password = sickbeard.PLEX_PASSWORD
if not host:
logger.log(u"PLEX: No host specified, check your settings", logger.ERROR)
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
logger.log(u"PLEX: Encoded API command: " + enc_command, logger.DEBUG)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
pw_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pw_mgr.add_password(None, url, username, password)
else:
pw_mgr = None
response = sickbeard.helpers.getURLFileLike(req, password_mgr=pw_mgr)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
logger.log(u"PLEX: HTTP response: " + result.replace('\n', ''), logger.DEBUG)
# could return result response = re.compile('<html><li>(.+\w)</html>').findall(result)
return 'OK'
except (urllib2.URLError, IOError), e:
logger.log(u"PLEX: Warning: Couldn't contact Plex at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING)
return False
示例12: _send_to_xbmc
def _send_to_xbmc(self, command, host=None, username=None, password=None):
"""Handles communication to XBMC servers via HTTP API
Args:
command: Dictionary of field/data pairs, encoded via urllib and passed to the XBMC API via HTTP
host: XBMC webserver host:port
username: XBMC webserver username
password: XBMC webserver password
Returns:
Returns response.result for successful commands or False if there was an error
"""
# fill in omitted parameters
if not username:
username = sickbeard.XBMC_USERNAME
if not password:
password = sickbeard.XBMC_PASSWORD
if not host:
logger.log(u"XBMC: No host specified, check your settings", logger.DEBUG)
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
logger.log(u"XBMC: Encoded API command: " + enc_command, logger.DEBUG)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
req = urllib2.Request(url)
# if we have a password, use authentication
if password:
pw_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pw_mgr.add_password(None, url, username, password)
else:
pw_mgr = None
response = sickbeard.helpers.getURLFileLike(req, password_mgr=pw_mgr, throw_exc=True)
if response:
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
logger.log(u"XBMC: HTTP response: " + result.replace('\n', ''), logger.DEBUG)
return result
except (urllib2.URLError, IOError), e:
logger.log(u"XBMC: Could not contact XBMC HTTP at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING)
示例13: _send_to_plex
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 True for successful commands or False if there was an error
"""
if not host:
self._log_error(u'No host specified, check your settings')
return False
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
self._log_debug(u'Encoded API command: ' + enc_command)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
req = urllib2.Request(url)
if password:
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = 'Basic %s' % base64string
req.add_header('Authorization', authheader)
self._log_debug(u'Contacting (with auth header) via url: ' + url)
else:
self._log_debug(u'Contacting via url: ' + url)
response = urllib2.urlopen(req)
result = response.read().decode(sickbeard.SYS_ENCODING)
response.close()
self._log_debug(u'HTTP response: ' + result.replace('\n', ''))
return True
except (urllib2.URLError, IOError) as e:
self._log_warning(u'Couldn\'t contact Plex at ' + fixStupidEncodings(url) + ' ' + ex(e))
return False
示例14: ex
def ex(e):
"""
Returns a unicode string from the exception text if it exists.
"""
# sanity check
if not e.args or not e.args[0]:
return ""
e_message = fixStupidEncodings(e.args[0], True)
# if fixStupidEncodings doesn't fix it then maybe it's not a string, in which case we'll try printing it anyway
if not e_message:
try:
e_message = str(e.args[0])
except:
e_message = ""
return e_message
示例15: _sendToXBMC
def _sendToXBMC(self, command, host, username=None, password=None):
'''
Handles communication with XBMC servers
command - Dictionary of field/data pairs, encoded via urllib.urlencode and
passed to /xbmcCmds/xbmcHttp
host - host/ip + port (foo:8080)
'''
if not username:
username = self._username()
if not password:
password = self._password()
for key in command:
if type(command[key]) == unicode:
command[key] = command[key].encode('utf-8')
enc_command = urllib.urlencode(command)
logger.log(u"Encoded command is " + enc_command, logger.DEBUG)
# Web server doesn't like POST, GET is the way to go
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
try:
# If we have a password, use authentication
req = urllib2.Request(url)
if password:
logger.log(u"Adding Password to XBMC url", logger.DEBUG)
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
authheader = "Basic %s" % base64string
req.add_header("Authorization", authheader)
logger.log(u"Contacting XBMC via url: " + url, logger.DEBUG)
handle = urllib2.urlopen(req)
response = handle.read()
logger.log(u"response: " + response, logger.DEBUG)
except IOError, e:
logger.log(u"Warning: Couldn't contact XBMC HTTP server at " + fixStupidEncodings(host) + ": " + ex(e))
response = ''