Skip to content

Instantly share code, notes, and snippets.

@agumonkey agumonkey/url.py
Created Jun 7, 2019

Embed
What would you like to do?
from urllib.parse import urlparse, urlunparse
"""
url helpers
"""
def isurl(o):
return o.startswith('http://') \
or o.startswith('https://')
def isabsolute(url):
p = urlparse(url)
return p.scheme is not '' and p.netloc is not ''
def reconciliate_path(np, op, sep='/'):
if np.startswith(sep):
return np
else:
lnp = np.split(sep)
lop = op.split(sep)
rnp = lop[:]
for e in lnp:
if e == '.':
pass
elif e == '..':
if len(rnp) > 1:
rnp.pop()
else:
rnp.append(e)
# crude fix
r = sep.join(rnp)
return r if r.startswith(sep) else sep + r
def normalize(new, old):
"""turns an href into an absolute url if possible"""
if isabsolute(new):
return new
else:
np = urlparse(new)
op = urlparse(old)
rp = reconciliate_path(np.path, op.path)
nu = f'{op.scheme}://{op.netloc}{rp}{np.params}{np.query}{np.fragment}'
return nu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.