Created
June 7, 2019 16:12
-
-
Save agumonkey/f3265ef0df811f2b250b021ce3154b89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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