logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

utils.py (1644B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import argparse
  4. import functools
  5. import os.path
  6. import subprocess
  7. import sys
  8. dirn = os.path.dirname
  9. sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
  10. from youtube_dl.compat import (
  11. compat_kwargs,
  12. compat_open as open,
  13. )
  14. def read_file(fname):
  15. with open(fname, encoding='utf-8') as f:
  16. return f.read()
  17. def write_file(fname, content, mode='w'):
  18. with open(fname, mode, encoding='utf-8') as f:
  19. return f.write(content)
  20. def read_version(fname='youtube_dl/version.py'):
  21. """Get the version without importing the package"""
  22. exec(compile(read_file(fname), fname, 'exec'))
  23. return locals()['__version__']
  24. def get_filename_args(has_infile=False, default_outfile=None):
  25. parser = argparse.ArgumentParser()
  26. if has_infile:
  27. parser.add_argument('infile', help='Input file')
  28. kwargs = {'nargs': '?', 'default': default_outfile} if default_outfile else {}
  29. kwargs['help'] = 'Output file'
  30. parser.add_argument('outfile', **compat_kwargs(kwargs))
  31. opts = parser.parse_args()
  32. if has_infile:
  33. return opts.infile, opts.outfile
  34. return opts.outfile
  35. def compose_functions(*functions):
  36. return lambda x: functools.reduce(lambda y, f: f(y), functions, x)
  37. def run_process(*args, **kwargs):
  38. kwargs.setdefault('text', True)
  39. kwargs.setdefault('check', True)
  40. kwargs.setdefault('capture_output', True)
  41. if kwargs['text']:
  42. kwargs.setdefault('encoding', 'utf-8')
  43. kwargs.setdefault('errors', 'replace')
  44. kwargs = compat_kwargs(kwargs)
  45. return subprocess.run(args, **kwargs)