cli_to_api.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  6. import yt_dlp
  7. import yt_dlp.options
  8. create_parser = yt_dlp.options.create_parser
  9. def parse_patched_options(opts):
  10. patched_parser = create_parser()
  11. patched_parser.defaults.update({
  12. 'ignoreerrors': False,
  13. 'retries': 0,
  14. 'fragment_retries': 0,
  15. 'extract_flat': False,
  16. 'concat_playlist': 'never',
  17. })
  18. yt_dlp.options.create_parser = lambda: patched_parser
  19. try:
  20. return yt_dlp.parse_options(opts)
  21. finally:
  22. yt_dlp.options.create_parser = create_parser
  23. default_opts = parse_patched_options([]).ydl_opts
  24. def cli_to_api(opts, cli_defaults=False):
  25. opts = (yt_dlp.parse_options if cli_defaults else parse_patched_options)(opts).ydl_opts
  26. diff = {k: v for k, v in opts.items() if default_opts[k] != v}
  27. if 'postprocessors' in diff:
  28. diff['postprocessors'] = [pp for pp in diff['postprocessors']
  29. if pp not in default_opts['postprocessors']]
  30. return diff
  31. if __name__ == '__main__':
  32. from pprint import pprint
  33. print('\nThe arguments passed translate to:\n')
  34. pprint(cli_to_api(sys.argv[1:]))
  35. print('\nCombining these with the CLI defaults gives:\n')
  36. pprint(cli_to_api(sys.argv[1:], True))