From e32fa567bc0819e02059bb5cdbb553831ecc5a61 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 19 Dec 2020 08:10:08 +0100 Subject: [PATCH] Adjust urllib.parse unquote, quote, quote_plus, unquote --- resources/lib/playlists/pms.py | 8 +-- resources/lib/utils.py | 53 +++++++------------- resources/lib/windows/direct_path_sources.py | 12 ++--- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/resources/lib/playlists/pms.py b/resources/lib/playlists/pms.py index 372a7a5b..a22dee7d 100644 --- a/resources/lib/playlists/pms.py +++ b/resources/lib/playlists/pms.py @@ -50,12 +50,12 @@ def initialize(playlist, plex_id): """ LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s', plex_id, playlist) + url_path = utils.quote(f'/library/metadata/{plex_id}', safe='') params = { 'type': v.PLEX_PLAYLIST_TYPE_FROM_KODI[playlist.kodi_type], 'title': playlist.plex_name, 'smart': 0, - 'uri': ('library://None/item/%s' % (utils.quote('/library/metadata/%s' - % plex_id, safe=''))) + 'uri': (f'library://None/item/{url_path}') } xml = DU().downloadUrl(url='{server}/playlists', action_type='POST', @@ -77,9 +77,9 @@ def add_item(playlist, plex_id): Will set playlist.plex_updatedat Raises PlaylistError if that did not work out. """ + url_path = utils.quote(f'/library/metadata/{plex_id}', safe='') params = { - 'uri': ('library://None/item/%s' % (utils.quote('/library/metadata/%s' - % plex_id, safe=''))) + 'uri': f'library://None/item/{url_path}' } xml = DU().downloadUrl(url='{server}/playlists/%s/items' % playlist.plex_id, action_type='PUT', diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 70da790d..6efb6a19 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -398,61 +398,46 @@ def escape_path(path, safe_url_char=SAFE_URL_CHARACTERS): # using RegExp and using safe_url_char as safe characters not to be escaped protocol = is_http_dav_ftp.group(1) user = is_http_dav_ftp.group(6) - psswd = is_http_dav_ftp.group(7) - if user and psswd: - user = urllib.parse.quote(user.encode('utf-8'), safe=safe_url_char).decode('utf-8') - psswd = urllib.parse.quote(psswd.encode('utf-8'), safe=safe_url_char).decode('utf-8') + passwd = is_http_dav_ftp.group(7) + if user and passwd: + user = quote(user, safe=safe_url_char) + passwd = quote(passwd, safe=safe_url_char) host = is_http_dav_ftp.group(8) port = is_http_dav_ftp.group(10) url_path = path.replace(is_http_dav_ftp.group(), '', 1) if url_path: - url_path = urllib.parse.quote(path.replace(is_http_dav_ftp.group(), '', 1).encode('utf-8'), - safe=safe_url_char).decode('utf-8') - return protocol + \ - u'://' + \ - (user + u':' + psswd + u'@' if (user and psswd) else u'') + \ + url_path = quote(path.replace(is_http_dav_ftp.group(), '', 1), + safe=safe_url_char) + return f'{protocol}://' + \ + (f'{user}:{passwd}@' if user and passwd else '') + \ host + \ - (u':' + port if port else u'') + \ - u'/' + \ - (url_path if url_path else u'') + (f':{port}' if port else '') + \ + (f'/{url_path}' if url_path else '') else: - # If paths does not seem to be a http(s), dav(s) or (s)ftp url (e.g. plugin://) - # escape path as before - return urllib.parse.quote(path.encode('utf-8'), - safe=SAFE_URL_CHARACTERS).decode('utf-8') + # If paths does not seem to be a http(s), dav(s) or (s)ftp url (e.g. + # plugin://) escape path as before + return quote(path, safe=SAFE_URL_CHARACTERS) def quote(s, safe='/'): """ - unicode-safe way to use urllib.quote(). Pass in either str or unicode - Returns unicode + Pass in unicode, returns unicode """ - if isinstance(s, str): - s = s.encode('utf-8') - s = urllib.parse.quote(s, safe.encode('utf-8')) - return s.decode('utf-8') + return urllib.parse.quote(s, safe) def quote_plus(s, safe=''): """ - unicode-safe way to use urllib.quote(). Pass in either str or unicode - Returns unicode + Pass in unicode, returns unicode """ - if isinstance(s, str): - s = s.encode('utf-8') - s = urllib.parse.quote_plus(s, safe.encode('utf-8')) - return s.decode('utf-8') + return urllib.parse.quote_plus(s, safe) def unquote(s): """ - unicode-safe way to use urllib.unquote(). Pass in either str or unicode - Returns unicode + Pass in unicode, returns unicode """ - if isinstance(s, str): - s = s.encode('utf-8') - s = urllib.parse.unquote(s) - return s.decode('utf-8') + return urllib.parse.unquote(s) def try_encode(input_str, encoding='utf-8'): diff --git a/resources/lib/windows/direct_path_sources.py b/resources/lib/windows/direct_path_sources.py index fc49816e..48716ddb 100644 --- a/resources/lib/windows/direct_path_sources.py +++ b/resources/lib/windows/direct_path_sources.py @@ -155,8 +155,7 @@ def start(): xml.write_xml = False return user = user.strip() - user = urllib.parse.quote(user) - user = user.decode('utf-8') + user = utils.quote(user) # "Password" # May also be blank!! (=user aborts dialog) password = utils.dialog('input', @@ -164,16 +163,15 @@ def start(): '', type='{alphanum}', option='{hide}') - password = urllib.parse.quote(password) - password = password.decode('utf-8') + password = utils.quote(password) utils.etree.SubElement(entry, 'from', - attrib={'pathversion': '1'}).text = '%s/' % path - login = '%s://%s:%s@%s/' % (protocol, user, password, hostname) + attrib={'pathversion': '1'}).text = f'{path}/' + login = f'{protocol}://{user}:{password}@{hostname}/' utils.etree.SubElement(entry, 'to', attrib={'pathversion': '1'}).text = login xml.write_xml = True except utils.ParseError: return - LOG.info('Successfully completed editing sources.xml and padsswords.xml') + LOG.info('Successfully completed editing sources.xml and passwords.xml')