Adjust urllib.parse unquote, quote, quote_plus, unquote

This commit is contained in:
croneter 2020-12-19 08:10:08 +01:00
parent 599f134204
commit e32fa567bc
3 changed files with 28 additions and 45 deletions

View file

@ -50,12 +50,12 @@ def initialize(playlist, plex_id):
""" """
LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s', LOG.debug('Initializing the playlist with Plex id %s on the Plex side: %s',
plex_id, playlist) plex_id, playlist)
url_path = utils.quote(f'/library/metadata/{plex_id}', safe='')
params = { params = {
'type': v.PLEX_PLAYLIST_TYPE_FROM_KODI[playlist.kodi_type], 'type': v.PLEX_PLAYLIST_TYPE_FROM_KODI[playlist.kodi_type],
'title': playlist.plex_name, 'title': playlist.plex_name,
'smart': 0, 'smart': 0,
'uri': ('library://None/item/%s' % (utils.quote('/library/metadata/%s' 'uri': (f'library://None/item/{url_path}')
% plex_id, safe='')))
} }
xml = DU().downloadUrl(url='{server}/playlists', xml = DU().downloadUrl(url='{server}/playlists',
action_type='POST', action_type='POST',
@ -77,9 +77,9 @@ def add_item(playlist, plex_id):
Will set playlist.plex_updatedat Will set playlist.plex_updatedat
Raises PlaylistError if that did not work out. Raises PlaylistError if that did not work out.
""" """
url_path = utils.quote(f'/library/metadata/{plex_id}', safe='')
params = { params = {
'uri': ('library://None/item/%s' % (utils.quote('/library/metadata/%s' 'uri': f'library://None/item/{url_path}'
% plex_id, safe='')))
} }
xml = DU().downloadUrl(url='{server}/playlists/%s/items' % playlist.plex_id, xml = DU().downloadUrl(url='{server}/playlists/%s/items' % playlist.plex_id,
action_type='PUT', action_type='PUT',

View file

@ -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 # using RegExp and using safe_url_char as safe characters not to be escaped
protocol = is_http_dav_ftp.group(1) protocol = is_http_dav_ftp.group(1)
user = is_http_dav_ftp.group(6) user = is_http_dav_ftp.group(6)
psswd = is_http_dav_ftp.group(7) passwd = is_http_dav_ftp.group(7)
if user and psswd: if user and passwd:
user = urllib.parse.quote(user.encode('utf-8'), safe=safe_url_char).decode('utf-8') user = quote(user, safe=safe_url_char)
psswd = urllib.parse.quote(psswd.encode('utf-8'), safe=safe_url_char).decode('utf-8') passwd = quote(passwd, safe=safe_url_char)
host = is_http_dav_ftp.group(8) host = is_http_dav_ftp.group(8)
port = is_http_dav_ftp.group(10) port = is_http_dav_ftp.group(10)
url_path = path.replace(is_http_dav_ftp.group(), '', 1) url_path = path.replace(is_http_dav_ftp.group(), '', 1)
if url_path: if url_path:
url_path = urllib.parse.quote(path.replace(is_http_dav_ftp.group(), '', 1).encode('utf-8'), url_path = quote(path.replace(is_http_dav_ftp.group(), '', 1),
safe=safe_url_char).decode('utf-8') safe=safe_url_char)
return protocol + \ return f'{protocol}://' + \
u'://' + \ (f'{user}:{passwd}@' if user and passwd else '') + \
(user + u':' + psswd + u'@' if (user and psswd) else u'') + \
host + \ host + \
(u':' + port if port else u'') + \ (f':{port}' if port else '') + \
u'/' + \ (f'/{url_path}' if url_path else '')
(url_path if url_path else u'')
else: else:
# If paths does not seem to be a http(s), dav(s) or (s)ftp url (e.g. plugin://) # If paths does not seem to be a http(s), dav(s) or (s)ftp url (e.g.
# escape path as before # plugin://) escape path as before
return urllib.parse.quote(path.encode('utf-8'), return quote(path, safe=SAFE_URL_CHARACTERS)
safe=SAFE_URL_CHARACTERS).decode('utf-8')
def quote(s, safe='/'): def quote(s, safe='/'):
""" """
unicode-safe way to use urllib.quote(). Pass in either str or unicode Pass in unicode, returns unicode
Returns unicode
""" """
if isinstance(s, str): return urllib.parse.quote(s, safe)
s = s.encode('utf-8')
s = urllib.parse.quote(s, safe.encode('utf-8'))
return s.decode('utf-8')
def quote_plus(s, safe=''): def quote_plus(s, safe=''):
""" """
unicode-safe way to use urllib.quote(). Pass in either str or unicode Pass in unicode, returns unicode
Returns unicode
""" """
if isinstance(s, str): return urllib.parse.quote_plus(s, safe)
s = s.encode('utf-8')
s = urllib.parse.quote_plus(s, safe.encode('utf-8'))
return s.decode('utf-8')
def unquote(s): def unquote(s):
""" """
unicode-safe way to use urllib.unquote(). Pass in either str or unicode Pass in unicode, returns unicode
Returns unicode
""" """
if isinstance(s, str): return urllib.parse.unquote(s)
s = s.encode('utf-8')
s = urllib.parse.unquote(s)
return s.decode('utf-8')
def try_encode(input_str, encoding='utf-8'): def try_encode(input_str, encoding='utf-8'):

View file

@ -155,8 +155,7 @@ def start():
xml.write_xml = False xml.write_xml = False
return return
user = user.strip() user = user.strip()
user = urllib.parse.quote(user) user = utils.quote(user)
user = user.decode('utf-8')
# "Password" # "Password"
# May also be blank!! (=user aborts dialog) # May also be blank!! (=user aborts dialog)
password = utils.dialog('input', password = utils.dialog('input',
@ -164,16 +163,15 @@ def start():
'', '',
type='{alphanum}', type='{alphanum}',
option='{hide}') option='{hide}')
password = urllib.parse.quote(password) password = utils.quote(password)
password = password.decode('utf-8')
utils.etree.SubElement(entry, utils.etree.SubElement(entry,
'from', 'from',
attrib={'pathversion': '1'}).text = '%s/' % path attrib={'pathversion': '1'}).text = f'{path}/'
login = '%s://%s:%s@%s/' % (protocol, user, password, hostname) login = f'{protocol}://{user}:{password}@{hostname}/'
utils.etree.SubElement(entry, utils.etree.SubElement(entry,
'to', 'to',
attrib={'pathversion': '1'}).text = login attrib={'pathversion': '1'}).text = login
xml.write_xml = True xml.write_xml = True
except utils.ParseError: except utils.ParseError:
return return
LOG.info('Successfully completed editing sources.xml and padsswords.xml') LOG.info('Successfully completed editing sources.xml and passwords.xml')