diff --git a/larigira/audiogen_http.py b/larigira/audiogen_http.py index 3d35ccd..746d9ee 100644 --- a/larigira/audiogen_http.py +++ b/larigira/audiogen_http.py @@ -1,31 +1,4 @@ -import os -import logging -import posixpath -from tempfile import mkstemp -import urllib.request -from urllib.parse import urlparse - -log = logging.getLogger(__name__) - - -def put(url, destdir=None, copy=False): - if url.split(":")[0] not in ("http", "https"): - log.warning("Not a valid URL: %s", url) - return None - ext = url.split(".")[-1] - if ext.lower() not in ("mp3", "ogg", "oga", "wma", "m4a"): - log.warning('Invalid format (%s) for "%s"', ext, url) - return None - if not copy: - return url - fname = posixpath.basename(urlparse(url).path) - # sanitize - fname = "".join(c for c in fname if c.isalnum() or c in list("._-")).rstrip() - tmp = mkstemp(suffix="." + ext, prefix="http-%s-" % fname, dir=destdir) - os.close(tmp[0]) - log.info("downloading %s -> %s", url, tmp[1]) - fname, headers = urllib.request.urlretrieve(url, tmp[1]) - return "file://%s" % os.path.realpath(tmp[1]) +from larigira.fsutils import download_http def generate(spec): @@ -35,10 +8,10 @@ def generate(spec): Recognized argument is "paths" (list of static paths) """ if "urls" not in spec: - raise ValueError("Malformed audiospec: missing 'paths'") + raise ValueError("Malformed audiospec: missing 'urls'") for url in spec["urls"]: - ret = put(url, copy=True) + ret = download_http(url, copy=True, prefix="http") if ret is None: continue yield ret diff --git a/larigira/fsutils.py b/larigira/fsutils.py index 9eae11c..95b8a2e 100644 --- a/larigira/fsutils.py +++ b/larigira/fsutils.py @@ -1,6 +1,13 @@ -import os import fnmatch +import logging import mimetypes +import os +import posixpath +import urllib.request +from tempfile import mkstemp +from urllib.parse import urlparse + +log = logging.getLogger(__name__) def scan_dir(dirname, extension=None): @@ -37,3 +44,27 @@ def shortname(path): name = name.rsplit(".", 1)[0] # no extension name = "".join(c for c in name if c.isalnum()) # no strange chars return name + + +def download_http(url, destdir=None, copy=False, prefix="httpdl"): + if url.split(":")[0] not in ("http", "https"): + log.warning("Not a valid URL: %s", url) + return None + ext = url.split(".")[-1] + if ext.lower() not in ("mp3", "ogg", "oga", "wma", "m4a"): + log.warning('Invalid format (%s) for "%s"', ext, url) + return None + if not copy: + return url + fname = posixpath.basename(urlparse(url).path) + # sanitize + fname = "".join( + c for c in fname if c.isalnum() or c in list("._-") + ).rstrip() + tmp = mkstemp( + suffix="." + ext, prefix="%s-%s-" % (prefix, fname), dir=destdir + ) + os.close(tmp[0]) + log.info("downloading %s -> %s", url, tmp[1]) + fname, headers = urllib.request.urlretrieve(url, tmp[1]) + return "file://%s" % os.path.realpath(tmp[1])