From 2013b498d653c955ed195ec8ae26b38edab4153b Mon Sep 17 00:00:00 2001 From: croneter Date: Thu, 28 Mar 2019 20:07:40 +0100 Subject: [PATCH 1/2] Add function to urlencode unicode --- resources/lib/utils.py | 74 ++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/resources/lib/utils.py b/resources/lib/utils.py index a54997bd..06c09229 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -9,6 +9,7 @@ from sqlite3 import connect, OperationalError from datetime import datetime from unicodedata import normalize from threading import Lock +import urllib # Originally tried faster cElementTree, but does NOT work reliably with Kodi import xml.etree.ElementTree as etree 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). value (any): value to be cast and returned. """ - if value is not None: - if func == bool: - return bool(int(value)) - elif func == unicode: - if isinstance(value, (int, long, float)): - return unicode(value) - else: - return value.decode('utf-8') - elif func == str: - if isinstance(value, (int, long, float)): - return str(value) - else: - return value.encode('utf-8') - elif func in (int, float): - try: - return func(value) - except ValueError: - return float('nan') - return func(value) - return value + if value is None: + return value + elif func == bool: + return bool(int(value)) + elif func == unicode: + if isinstance(value, (int, long, float)): + return unicode(value) + elif isinstance(value, unicode): + return value + else: + return value.decode('utf-8') + elif func == str: + if isinstance(value, (int, long, float)): + return str(value) + elif isinstance(value, str): + return value + else: + return value.encode('utf-8') + elif func in (int, float): + 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 + '' + 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'): From 90f445cc753c846ae7dabac61a7cc3d1f78fc0df Mon Sep 17 00:00:00 2001 From: croneter Date: Thu, 28 Mar 2019 20:08:45 +0100 Subject: [PATCH 2/2] Fix UnicodeEncodeError --- resources/lib/plex_api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/lib/plex_api.py b/resources/lib/plex_api.py index 370a62ba..8d40824b 100644 --- a/resources/lib/plex_api.py +++ b/resources/lib/plex_api.py @@ -152,9 +152,9 @@ class API(object): def directory_path(self, section_id=None, plex_type=None, old_key=None, synched=True): - key = self.item.get('fastKey') + key = cast(unicode, self.item.get('fastKey')) if not key: - key = self.item.get('key') + key = cast(unicode, self.item.get('key')) if old_key: key = '%s/%s' % (old_key, key) elif not key.startswith('/'): @@ -169,10 +169,10 @@ class API(object): params['synched'] = 'false' if self.item.get('prompt'): # 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: 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): """