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.

277 lines
11 KiB

10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
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 to handle settings. """
  3. import json
  4. import os.path
  5. from .utils import check_path
  6. class OptionsManager(object):
  7. """Handles youtubedlg options.
  8. This class is responsible for storing and retrieving the options.
  9. Attributes:
  10. SETTINGS_FILENAME (string): Filename of the settings file.
  11. SENSITIVE_KEYS (tuple): Contains the keys that we don't want
  12. to store on the settings file. (SECURITY ISSUES).
  13. Args:
  14. config_path (string): Absolute path where OptionsManager
  15. should store the settings file.
  16. Note:
  17. See load_default() method for available options.
  18. Example:
  19. Access the options using the 'options' variable.
  20. opt_manager = OptionsManager('.')
  21. opt_manager.options['save_path'] = '~/Downloads'
  22. """
  23. SETTINGS_FILENAME = 'settings.json'
  24. SENSITIVE_KEYS = ('sudo_password', 'password', 'video_password')
  25. def __init__(self, config_path):
  26. self.config_path = config_path
  27. self.settings_file = os.path.join(config_path, self.SETTINGS_FILENAME)
  28. self.options = dict()
  29. self.load_default()
  30. self.load_from_file()
  31. def load_default(self):
  32. """Load the default options.
  33. Note:
  34. This method is automatically called by the constructor.
  35. Options Description:
  36. save_path (string): Path where youtube-dl should store the
  37. downloaded file. Default is $HOME.
  38. video_format (string): Video format to download. For available
  39. video formats see 'parsers' module (VIDEO_FORMATS attribute).
  40. When this options is set to 'default' youtube-dl will choose
  41. the best video format available for the given URL.
  42. second_video_format (string): Video format to mix with the first
  43. one. (-f 18+17). For available video formats see 'video_format'
  44. option. (This option must NOT be 'default').
  45. to_audio (boolean): If True the youtube-dl will post process the
  46. video file.
  47. keep_video (boolen): If True youtube-dl will keep the video file
  48. after post processing it.
  49. audio_format (string): Audio format of the post processed file.
  50. Available values are "mp3", "wav", "aac", "m4a", "vorbis".
  51. audio_quality (string): Audio quality of the post processed file.
  52. Available values are "low", "mid", "high".
  53. restrict_filenames (boolean): If True youtube-dl will restrict
  54. the downloaded file filename to ASCII characters only.
  55. output_format (string): This option sets the downloaded file
  56. output template. Available values are 'id', 'title', 'custom'
  57. 'id' -> '%(id)s.%(ext)s'
  58. 'title' -> '%(title)s.%(ext)s'
  59. 'custom' -> Loads the template from the 'output_template'
  60. option.
  61. output_template (string): Can be any output template supported
  62. by youtube-dl.
  63. playlist_start (int): Playlist index to start downloading.
  64. playlist_end (int): Playlist index to stop downloading.
  65. max_downloads (int): Maximum number of video files to download
  66. from the given playlist.
  67. min_filesize (float): Minimum file size of the video file.
  68. If the video file is smaller than the given size then
  69. youtube-dl will abort the download process.
  70. max_filesize (float): Maximum file size of the video file.
  71. If the video file is larger than the given size then
  72. youtube-dl will abort the download process.
  73. min_filesize_unit (string): Minimum file size unit.
  74. Available values 'Bytes', 'Kilobytes', 'Megabytes',
  75. 'Gigabytes', 'Terabytes', 'Petabytes', 'Exabytes',
  76. 'Zettabytes', 'Yottabytes'.
  77. max_filesize_unit (string): Maximum file size unit.
  78. See 'min_filesize_unit' option for available values.
  79. write_subs (boolean): If True youtube-dl will try downloading
  80. the subtitles file for the given URL.
  81. write_all_subs (boolean): If True youtube-dl will try downloading
  82. all the available subtitles files for the given URL.
  83. write_auto_subs (boolean): If True youtube-dl will try downloading
  84. the automatic subtitles file for the given URL.
  85. embed_subs (boolean): If True youtube-dl will merge the subtitles
  86. file with the video. (ONLY mp4 files).
  87. subs_lang (string): Language of the subtitles file to download.
  88. Needs 'write_subs' option. For available subtitles see
  89. 'parsers' module. (SUBS_LANG attribute).
  90. ignore_errors (boolean): If True youtube-dl will ignore the errors
  91. and continue the download process.
  92. open_dl_dir (boolean): If True youtube-dlg will open the
  93. destination folder after download process has been completed.
  94. write_description (boolean): If True youtube-dl will write video
  95. description to a .description file.
  96. write_info (boolean): If True youtube-dl will write video
  97. metadata to a .info.json file.
  98. write_thumbnail (boolean): If True youtube-dl will write
  99. thumbnail image to disk.
  100. retries (int): Number of youtube-dl retries.
  101. user_agent (string): Specify a custom user agent for youtube-dl.
  102. referer (string): Specify a custom referer to use if the video
  103. access is restricted to one domain.
  104. proxy (string): Use the specified HTTP/HTTPS proxy.
  105. shutdown (boolean): If True youtube-dlg will turn the computer
  106. off after the download process has been completed.
  107. sudo_password (string): SUDO password for the shutdown process if
  108. the user does not have root access.
  109. username (string): Username to login with.
  110. password (string): Password to login with.
  111. video_password (string): Video password for the given URL.
  112. youtubedl_path (string): Absolute path to the youtube-dl binary.
  113. Default is the self.config_path. You can change this option
  114. to point on /usr/local/bin etc.. if you want to use the
  115. youtube-dl binary on your system. This is also the directory
  116. where youtube-dlg will auto download the youtube-dl if not
  117. exists so you should make sure you have write access if you
  118. want to update the youtube-dl binary from within youtube-dlg.
  119. cmd_args (string): String that contains extra youtube-dl options
  120. seperated by spaces.
  121. enable_log (boolean): If True youtube-dlg will enable
  122. the LogManager. See main() function under __init__().
  123. log_time (boolean): See logmanager.LogManager add_time attribute.
  124. """
  125. self.options = {
  126. 'save_path': os.path.expanduser('~'),
  127. 'video_format': 'default',
  128. 'second_video_format': 'none',
  129. 'to_audio': False,
  130. 'keep_video': False,
  131. 'audio_format': 'mp3',
  132. 'audio_quality': 'mid',
  133. 'restrict_filenames': False,
  134. 'output_format': 'title',
  135. 'output_template': '%(uploader)s/%(title)s.%(ext)s',
  136. 'playlist_start': 1,
  137. 'playlist_end': 0,
  138. 'max_downloads': 0,
  139. 'min_filesize': 0,
  140. 'max_filesize': 0,
  141. 'min_filesize_unit': 'Bytes',
  142. 'max_filesize_unit': 'Bytes',
  143. 'write_subs': False,
  144. 'write_all_subs': False,
  145. 'write_auto_subs': False,
  146. 'embed_subs': False,
  147. 'subs_lang': 'English',
  148. 'ignore_errors': True,
  149. 'open_dl_dir': True,
  150. 'write_description': False,
  151. 'write_info': False,
  152. 'write_thumbnail': False,
  153. 'retries': 10,
  154. 'user_agent': '',
  155. 'referer': '',
  156. 'proxy': '',
  157. 'shutdown': False,
  158. 'sudo_password': '',
  159. 'username': '',
  160. 'password': '',
  161. 'video_password': '',
  162. 'youtubedl_path': self.config_path,
  163. 'cmd_args': '',
  164. 'enable_log': True,
  165. 'log_time': False
  166. }
  167. def load_from_file(self):
  168. """Load options from settings file. """
  169. if not os.path.exists(self.settings_file):
  170. return
  171. with open(self.settings_file, 'rb') as settings_file:
  172. try:
  173. options = json.load(settings_file)
  174. except:
  175. self.load_default()
  176. if self._settings_are_valid(options):
  177. self.options = options
  178. def save_to_file(self):
  179. """Save options to settings file. """
  180. check_path(self.config_path)
  181. with open(self.settings_file, 'wb') as settings_file:
  182. options = self._get_options()
  183. json.dump(options,
  184. settings_file,
  185. indent=4,
  186. separators=(',', ': '))
  187. def _settings_are_valid(self, settings_dictionary):
  188. """Check settings.json dictionary.
  189. Args:
  190. settings_dictionary (dictionary): Options dictionary loaded
  191. from the settings file. See load_from_file() method.
  192. Returns:
  193. True if settings.json dictionary is valid, else False.
  194. """
  195. for key in self.options:
  196. if key not in settings_dictionary:
  197. return False
  198. return True
  199. def _get_options(self):
  200. """Return options dictionary without SENSITIVE_KEYS. """
  201. temp_options = self.options.copy()
  202. for key in self.SENSITIVE_KEYS:
  203. temp_options[key] = ''
  204. return temp_options