audiogen_http.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. import logging
  3. import posixpath
  4. from tempfile import mkstemp
  5. import urllib.request
  6. from urllib.parse import urlparse
  7. log = logging.getLogger(__name__)
  8. def put(url, destdir=None, copy=False):
  9. if url.split(':')[0] not in ('http', 'https'):
  10. log.warning('Not a valid URL: %s', url)
  11. return None
  12. ext = url.split('.')[-1]
  13. if ext.lower() not in ('mp3', 'ogg', 'oga', 'wma', 'm4a'):
  14. log.warning('Invalid format (%s) for "%s"', ext, url)
  15. return None
  16. if not copy:
  17. return url
  18. fname = posixpath.basename(urlparse(url).path)
  19. # sanitize
  20. fname = "".join(c for c in fname
  21. if c.isalnum() or c in list('._-')).rstrip()
  22. tmp = mkstemp(suffix='.' + ext, prefix='http-%s-' % fname, dir=destdir)
  23. os.close(tmp[0])
  24. log.info("downloading %s -> %s", url, tmp[1])
  25. fname, headers = urllib.request.urlretrieve(url, tmp[1])
  26. return 'file://%s' % os.path.realpath(tmp[1])
  27. def generate(spec):
  28. '''
  29. resolves audiospec-static
  30. Recognized argument is "paths" (list of static paths)
  31. '''
  32. if 'urls' not in spec:
  33. raise ValueError("Malformed audiospec: missing 'paths'")
  34. for url in spec['urls']:
  35. ret = put(url, copy=True)
  36. if ret is None:
  37. continue
  38. yield ret
  39. generate.description = 'Fetch audio from an URL'