add 'ext_from_url' to text.py

This commit is contained in:
Mike Fährmann
2019-01-31 12:23:25 +01:00
parent bfbbac4495
commit e1d3e9a926
2 changed files with 25 additions and 2 deletions

View File

@@ -55,15 +55,22 @@ def split_html(txt, sep=None):
def filename_from_url(url):
"""Extract the last part of an url to use as a filename"""
"""Extract the last part of an URL to use as a filename"""
try:
return urllib.parse.urlsplit(url).path.rpartition("/")[2]
except (TypeError, AttributeError):
return ""
def ext_from_url(url):
"""Extract the filename extension of an URL"""
filename = filename_from_url(url)
ext = os.path.splitext(filename)[1]
return ext[1:].lower()
def nameext_from_url(url, data=None):
"""Extract the last part of an url and fill 'data' accordingly"""
"""Extract the last part of an URL and fill 'data' accordingly"""
if data is None:
data = {}
data["filename"] = unquote(filename_from_url(url))