Merge pull request #802 from croneter/quick-fix-direct-paths

Fix UnicodeEncodeError for Direct Paths and some PKC video nodes
This commit is contained in:
croneter 2019-03-29 14:25:37 +01:00 committed by GitHub
commit 068379c0eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 24 deletions

View file

@ -152,9 +152,9 @@ class API(object):
def directory_path(self, section_id=None, plex_type=None, old_key=None, def directory_path(self, section_id=None, plex_type=None, old_key=None,
synched=True): synched=True):
key = self.item.get('fastKey') key = cast(unicode, self.item.get('fastKey'))
if not key: if not key:
key = self.item.get('key') key = cast(unicode, self.item.get('key'))
if old_key: if old_key:
key = '%s/%s' % (old_key, key) key = '%s/%s' % (old_key, key)
elif not key.startswith('/'): elif not key.startswith('/'):
@ -169,10 +169,10 @@ class API(object):
params['synched'] = 'false' params['synched'] = 'false'
if self.item.get('prompt'): if self.item.get('prompt'):
# User input needed, e.g. search for a movie or episode # User input needed, e.g. search for a movie or episode
params['prompt'] = self.item.get('prompt') params['prompt'] = cast(unicode, self.item.get('prompt'))
if section_id: if section_id:
params['id'] = section_id params['id'] = section_id
return 'plugin://%s/?%s' % (v.ADDON_ID, urlencode(params)) return utils.extend_url('plugin://%s/' % v.ADDON_ID, params)
def path_and_plex_id(self): def path_and_plex_id(self):
""" """

View file

@ -9,6 +9,7 @@ from sqlite3 import connect, OperationalError
from datetime import datetime from datetime import datetime
from unicodedata import normalize from unicodedata import normalize
from threading import Lock from threading import Lock
import urllib
# Originally tried faster cElementTree, but does NOT work reliably with Kodi # Originally tried faster cElementTree, but does NOT work reliably with Kodi
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import defusedxml.ElementTree as defused_etree # etree parse unsafe import defusedxml.ElementTree as defused_etree # etree parse unsafe
@ -269,26 +270,59 @@ def cast(func, value):
func (func): Calback function to used cast to type (int, bool, float). func (func): Calback function to used cast to type (int, bool, float).
value (any): value to be cast and returned. value (any): value to be cast and returned.
""" """
if value is not None: if value is None:
if func == bool: return value
return bool(int(value)) elif func == bool:
elif func == unicode: return bool(int(value))
if isinstance(value, (int, long, float)): elif func == unicode:
return unicode(value) if isinstance(value, (int, long, float)):
else: return unicode(value)
return value.decode('utf-8') elif isinstance(value, unicode):
elif func == str: return value
if isinstance(value, (int, long, float)): else:
return str(value) return value.decode('utf-8')
else: elif func == str:
return value.encode('utf-8') if isinstance(value, (int, long, float)):
elif func in (int, float): return str(value)
try: elif isinstance(value, str):
return func(value) return value
except ValueError: else:
return float('nan') return value.encode('utf-8')
return func(value) elif func in (int, float):
return value try:
return func(value)
except ValueError:
return float('nan')
return func(value)
def extend_url(url, params):
"""
Pass in an url [unicode] and params [dict]. Returns the extended url
'<url><? or &><urllib.urlencode(params)>'
in unicode
"""
params = encode_dict(params) if params else {}
params = urllib.urlencode(params).decode('utf-8')
if '?' in url:
return '%s&%s' % (url, params)
else:
return '%s?%s' % (url, params)
def encode_dict(dictionary):
"""
Pass in a dict. Will return the same dict with all keys and values encoded
in utf-8 - as long as they are unicode. Ignores all non-unicode entries
Useful for urllib.urlencode or urllib.(un)quote
"""
for key, value in dictionary.iteritems():
if isinstance(key, unicode):
dictionary[key.encode('utf-8')] = dictionary.pop(key)
if isinstance(value, unicode):
dictionary[key] = value.encode('utf-8')
return dictionary
def try_encode(input_str, encoding='utf-8'): def try_encode(input_str, encoding='utf-8'):