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.

188 lines
7.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python2
  2. """Youtubedlg module responsible for parsing the options. """
  3. import os.path
  4. from .utils import remove_shortcuts
  5. class OptionHolder(object):
  6. """Simple data structure that holds informations for the given option.
  7. Args:
  8. name (string): Option name. Must be a valid option name
  9. from the optionsmanager.OptionsManager class.
  10. See optionsmanager.OptionsManager load_default() method.
  11. flag (string): The option command line switch.
  12. See https://github.com/rg3/youtube-dl/#options
  13. default_value (any): The option default value. Must be the same type
  14. with the corresponding option from the optionsmanager.OptionsManager
  15. class.
  16. requirements (list): The requirements for the given option. This
  17. argument is a list of strings with the name of all the options
  18. that this specific option needs. For example 'subs_lang' needs the
  19. 'write_subs' option to be enabled.
  20. """
  21. def __init__(self, name, flag, default_value, requirements=None):
  22. self.name = name
  23. self.flag = flag
  24. self.requirements = requirements
  25. self.default_value = default_value
  26. def is_boolean(self):
  27. """Returns True if the option is a boolean switch else False. """
  28. return type(self.default_value) is bool
  29. def check_requirements(self, options_dict):
  30. """Check if the required options are enabled.
  31. Args:
  32. options_dict (dictionary): Dictionary with all the options.
  33. Returns:
  34. True if any of the required options is enabled else False.
  35. """
  36. if self.requirements is None:
  37. return True
  38. return any([options_dict[req] for req in self.requirements])
  39. class OptionsParser(object):
  40. """Parse optionsmanager.OptionsManager options.
  41. This class is responsible for turning some of the youtube-dlg options
  42. to youtube-dl command line options.
  43. """
  44. def __init__(self):
  45. self._ydl_options = [
  46. OptionHolder('playlist_start', '--playlist-start', 1),
  47. OptionHolder('playlist_end', '--playlist-end', 0),
  48. OptionHolder('max_downloads', '--max-downloads', 0),
  49. OptionHolder('username', '-u', ''),
  50. OptionHolder('password', '-p', ''),
  51. OptionHolder('video_password', '--video-password', ''),
  52. OptionHolder('retries', '-R', 10),
  53. OptionHolder('proxy', '--proxy', ''),
  54. OptionHolder('user_agent', '--user-agent', ''),
  55. OptionHolder('referer', '--referer', ''),
  56. OptionHolder('ignore_errors', '-i', False),
  57. OptionHolder('write_description', '--write-description', False),
  58. OptionHolder('write_info', '--write-info-json', False),
  59. OptionHolder('write_thumbnail', '--write-thumbnail', False),
  60. OptionHolder('min_filesize', '--min-filesize', 0),
  61. OptionHolder('max_filesize', '--max-filesize', 0),
  62. OptionHolder('write_all_subs', '--all-subs', False),
  63. OptionHolder('write_auto_subs', '--write-auto-sub', False),
  64. OptionHolder('write_subs', '--write-sub', False),
  65. OptionHolder('keep_video', '-k', False),
  66. OptionHolder('restrict_filenames', '--restrict-filenames', False),
  67. OptionHolder('save_path', '-o', ''),
  68. OptionHolder('embed_subs', '--embed-subs', False, ['write_auto_subs', 'write_subs']),
  69. OptionHolder('to_audio', '-x', False),
  70. OptionHolder('audio_format', '--audio-format', '', ['to_audio']),
  71. OptionHolder('video_format', '-f', '0'),
  72. OptionHolder('subs_lang', '--sub-lang', '', ['write_subs']),
  73. OptionHolder('audio_quality', '--audio-quality', '5', ['to_audio'])
  74. ]
  75. def parse(self, options_dictionary):
  76. """Parse optionsmanager.OptionsManager options.
  77. Parses the given options to youtube-dl command line arguments.
  78. Args:
  79. options_dictionary (dictionary): Dictionary with all the options.
  80. Returns:
  81. List of strings with all the youtube-dl command line options.
  82. """
  83. options_list = ['--newline']
  84. # Create a copy of options_dictionary
  85. # We don't want to edit the original options dictionary
  86. # and change some of the options values like 'save_path' etc..
  87. options_dict = options_dictionary.copy()
  88. self._build_savepath(options_dict)
  89. self._build_videoformat(options_dict)
  90. self._build_filesizes(options_dict)
  91. # Parse basic youtube-dl command line options
  92. for option in self._ydl_options:
  93. if option.check_requirements(options_dict):
  94. value = options_dict[option.name]
  95. if value != option.default_value:
  96. options_list.append(option.flag)
  97. if not option.is_boolean():
  98. options_list.append(str(value))
  99. # Parse cmd_args
  100. for option in options_dict['cmd_args'].split():
  101. options_list.append(option)
  102. return options_list
  103. def _build_savepath(self, options_dict):
  104. """Build the save path.
  105. We use this method to build the value of the 'save_path' option and
  106. store it back to the options dictionary.
  107. Args:
  108. options_dict (dictionary): Copy of the original options dictionary.
  109. """
  110. save_path = remove_shortcuts(options_dict['save_path'])
  111. if options_dict['output_format'] == 'id':
  112. save_path = os.path.join(save_path, '%(id)s.%(ext)s')
  113. elif options_dict['output_format'] == 'title':
  114. save_path = os.path.join(save_path, '%(title)s.%(ext)s')
  115. else:
  116. save_path = os.path.join(save_path, options_dict['output_template'])
  117. options_dict['save_path'] = save_path
  118. def _build_videoformat(self, options_dict):
  119. """Build the video format.
  120. We use this method to build the value of the 'video_format' option and
  121. store it back to the options dictionary.
  122. Args:
  123. options_dict (dictionary): Copy of the original options dictionary.
  124. """
  125. if options_dict['video_format'] != '0' and options_dict['second_video_format'] != '0':
  126. options_dict['video_format'] = options_dict['video_format'] + '+' + options_dict['second_video_format']
  127. def _build_filesizes(self, options_dict):
  128. """Build the filesize options values.
  129. We use this method to build the values of 'min_filesize' and
  130. 'max_filesize' options and store them back to options dictionary.
  131. Args:
  132. options_dict (dictionary): Copy of the original options dictionary.
  133. """
  134. if options_dict['min_filesize']:
  135. options_dict['min_filesize'] = str(options_dict['min_filesize']) + options_dict['min_filesize_unit']
  136. if options_dict['max_filesize']:
  137. options_dict['max_filesize'] = str(options_dict['max_filesize']) + options_dict['max_filesize_unit']