Skip to content

Instantly share code, notes, and snippets.

@agumonkey
Created June 7, 2019 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agumonkey/f3265ef0df811f2b250b021ce3154b89 to your computer and use it in GitHub Desktop.
Save agumonkey/f3265ef0df811f2b250b021ce3154b89 to your computer and use it in GitHub Desktop.
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