You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

  1. from __future__ import unicode_literals, print_function
  2. from inspect import getsource
  3. import os
  4. from os.path import dirname as dirn
  5. import sys
  6. print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
  7. sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
  8. lazy_extractors_filename = sys.argv[1]
  9. if os.path.exists(lazy_extractors_filename):
  10. os.remove(lazy_extractors_filename)
  11. from youtube_dl.extractor import _ALL_CLASSES
  12. from youtube_dl.extractor.common import InfoExtractor
  13. with open('devscripts/lazy_load_template.py', 'rt') as f:
  14. module_template = f.read()
  15. module_contents = [module_template + '\n' + getsource(InfoExtractor.suitable)]
  16. ie_template = '''
  17. class {name}(LazyLoadExtractor):
  18. _VALID_URL = {valid_url!r}
  19. _module = '{module}'
  20. '''
  21. make_valid_template = '''
  22. @classmethod
  23. def _make_valid_url(cls):
  24. return {valid_url!r}
  25. '''
  26. def build_lazy_ie(ie, name):
  27. valid_url = getattr(ie, '_VALID_URL', None)
  28. s = ie_template.format(
  29. name=name,
  30. valid_url=valid_url,
  31. module=ie.__module__)
  32. if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
  33. s += '\n' + getsource(ie.suitable)
  34. if hasattr(ie, '_make_valid_url'):
  35. # search extractors
  36. s += make_valid_template.format(valid_url=ie._make_valid_url())
  37. return s
  38. names = []
  39. for ie in list(sorted(_ALL_CLASSES[:-1], key=lambda cls: cls.ie_key())) + _ALL_CLASSES[-1:]:
  40. name = ie.ie_key() + 'IE'
  41. src = build_lazy_ie(ie, name)
  42. module_contents.append(src)
  43. names.append(name)
  44. module_contents.append(
  45. '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
  46. module_src = '\n'.join(module_contents) + '\n'
  47. with open(lazy_extractors_filename, 'wt') as f:
  48. f.write(module_src)