-
Notifications
You must be signed in to change notification settings - Fork 16
/
normalise.py
59 lines (46 loc) · 1.66 KB
/
normalise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from urllib.parse import urlparse, urlunparse, ParseResult
from portality import regex
def normalise_url(url):
"""
Take a URL and turn it into a form which is suitable for normalised comparison with other normalised
URLs.
The function does the following:
* strips leading/trailing whitespace
* validates the URL is realistic
* strips the scheme (so, removes http, https, ftp, ftps, etc)
:param url:
:return:
"""
if url is None:
return url
schemes = ["http", "https", "ftp", "ftps"]
url = url.strip()
if url.startswith("//"):
url = "http:" + url
if "://" not in url:
url = "http://" + url
u = urlparse(url)
if u.netloc is None or u.netloc == "":
raise ValueError("Could not extract a normalised URL from '{x}'".format(x=url))
if u.scheme not in schemes:
raise ValueError("URL must be at http(s) or ftp(s), found '{x}'".format(x=u.netloc))
n = ParseResult(netloc=u.netloc, path=u.path, params=u.params, query=u.query, fragment=u.fragment, scheme='')
return urlunparse(n)
def normalise_doi(doi):
"""
Take a DOI and turn it into a form which is suitable for normalised comparison with other normalised
DOIs.
The function does the following:
* strips leading/trailing whitespace
* validates that the DOI meets the regex
* extracts only the 10.xxxx portion
:param doi:
:return:
"""
if doi is None:
return None
doi = doi.strip()
norm = regex.group_match(regex.DOI_COMPILED, doi, "id")
if norm is None:
raise ValueError("Could not extract a normalised DOI from '{x}'".format(x=doi))
return norm