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.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /usr/bin/env python
  2. from .version import __version__
  3. from .Utils import (
  4. get_HOME,
  5. file_exist,
  6. get_os_type,
  7. fix_path,
  8. makedir,
  9. abs_path
  10. )
  11. SETTINGS_FILENAME = 'settings'
  12. LINUX_FILES_PATH = get_HOME() + '/.youtube-dl-gui'
  13. WINDOWS_FILES_PATH = get_HOME() + '\\youtube-dl-gui'
  14. class OptionsHandler():
  15. settings_abs_path = ''
  16. def __init__(self, statusBarWrite):
  17. self.statusBarWrite = statusBarWrite
  18. self.set_settings_path()
  19. self.load_default()
  20. self.load_settings()
  21. def load_settings(self):
  22. if not file_exist(self.get_config_path()):
  23. makedir(self.get_config_path())
  24. if file_exist(self.settings_abs_path):
  25. self.load_from_file()
  26. def load_default(self):
  27. self.savePath = get_HOME()
  28. self.videoFormat = "default"
  29. self.dashAudioFormat = "NO SOUND"
  30. self.clearDashFiles = False
  31. self.toAudio = False
  32. self.keepVideo = False
  33. self.audioFormat = "mp3"
  34. self.audioQuality = "mid"
  35. self.outputFormat = "title"
  36. self.outputTemplate = '%(uploader)s/%(title)s.%(ext)s'
  37. self.startTrack = 1
  38. self.endTrack = 0
  39. self.maxDownloads = 0
  40. self.minFileSize = "0"
  41. self.maxFileSize = "0"
  42. self.writeSubs = False
  43. self.writeAllSubs = False
  44. self.writeAutoSubs = False
  45. self.embedSubs = False
  46. self.subsLang = "English"
  47. self.openDownloadDir = False
  48. self.ignoreErrors = True
  49. self.writeDescription = False
  50. self.writeInfo = False
  51. self.writeThumbnail = False
  52. self.retries = 10
  53. self.userAgent = ""
  54. self.referer = ""
  55. self.proxy = ""
  56. self.username = ""
  57. self.password = ""
  58. self.videoPass = ""
  59. self.updatePath = self.get_config_path()
  60. self.cmdArgs = ""
  61. self.enableLog = True
  62. self.writeTimeToLog = True
  63. def get_config_path(self):
  64. if get_os_type() == 'nt':
  65. return WINDOWS_FILES_PATH
  66. else:
  67. return LINUX_FILES_PATH
  68. def set_settings_path(self):
  69. self.settings_abs_path = self.get_config_path()
  70. self.settings_abs_path = fix_path(self.settings_abs_path)
  71. self.settings_abs_path += SETTINGS_FILENAME
  72. def read_from_file(self):
  73. f = open(self.settings_abs_path, 'r')
  74. options = f.readlines()
  75. f.close()
  76. return options
  77. def extract_options(self, options):
  78. opts = []
  79. for option in options:
  80. opt = option.split('=')
  81. if not len(opt) < 2:
  82. opts.append(opt[1].rstrip('\n'))
  83. return opts
  84. def check_settings_file_version(self, options):
  85. data = options.pop(0).rstrip('\n')
  86. name, version = data.split('=')
  87. if name == 'Version' and version == __version__:
  88. return True
  89. else:
  90. return False
  91. def load_from_file(self):
  92. options = self.read_from_file()
  93. if self.check_settings_file_version(options):
  94. opts = self.extract_options(options)
  95. try:
  96. self.savePath = opts[0].decode('utf8')
  97. self.videoFormat = opts[1]
  98. self.dashAudioFormat = opts[2]
  99. self.clearDashFiles = opts[3] in ['True']
  100. self.toAudio = opts[4] in ['True']
  101. self.keepVideo = opts[5] in ['True']
  102. self.audioFormat = opts[6]
  103. self.audioQuality = opts[7]
  104. self.outputFormat = opts[8]
  105. self.outputTemplate = opts[9]
  106. self.startTrack = int(opts[10])
  107. self.endTrack = int(opts[11])
  108. self.maxDownloads = int(opts[12])
  109. self.minFileSize = opts[13]
  110. self.maxFileSize = opts[14]
  111. self.writeSubs = opts[15] in ['True']
  112. self.writeAllSubs = opts[16] in ['True']
  113. self.writeAutoSubs = opts[17] in ['True']
  114. self.embedSubs = opts[18] in ['True']
  115. self.subsLang = opts[19]
  116. self.openDownloadDir = opts[20] in ['True']
  117. self.ignoreErrors = opts[21] in ['True']
  118. self.writeDescription = opts[22] in ['True']
  119. self.writeInfo = opts[23] in ['True']
  120. self.writeThumbnail = opts[24] in ['True']
  121. self.retries = int(opts[25])
  122. self.userAgent = opts[26]
  123. self.referer = opts[27]
  124. self.proxy = opts[28]
  125. self.username = opts[29]
  126. self.cmdArgs = opts[30]
  127. self.enableLog = opts[31] in ['True']
  128. self.writeTimeToLog = opts[32] in ['True']
  129. except:
  130. self.statusBarWrite('Error while loading settings file. Loading default settings.')
  131. self.load_default()
  132. else:
  133. self.statusBarWrite('Settings file version problem. Loading default settings.')
  134. self.load_default()
  135. def save_to_file(self):
  136. if not file_exist(self.get_config_path()):
  137. makedir(self.get_config_path())
  138. f = open(self.settings_abs_path, 'w')
  139. f.write('Version='+__version__+'\n')
  140. f.write('SavePath='+self.savePath.encode('utf-8')+'\n')
  141. f.write('VideoFormat='+str(self.videoFormat)+'\n')
  142. f.write('DashAudioFormat='+str(self.dashAudioFormat)+'\n')
  143. f.write('ClearDashFiles='+str(self.clearDashFiles)+'\n')
  144. f.write('ToAudio='+str(self.toAudio)+'\n')
  145. f.write('KeepVideo='+str(self.keepVideo)+'\n')
  146. f.write('AudioFormat='+str(self.audioFormat)+'\n')
  147. f.write('AudioQuality='+str(self.audioQuality)+'\n')
  148. f.write('OutputFormat='+str(self.outputFormat)+'\n')
  149. f.write('OutputTemplate='+str(self.outputTemplate)+'\n')
  150. f.write('StartTrack='+str(self.startTrack)+'\n')
  151. f.write('EndTrack='+str(self.endTrack)+'\n')
  152. f.write('MaxDownloads='+str(self.maxDownloads)+'\n')
  153. f.write('MinFileSize='+str(self.minFileSize)+'\n')
  154. f.write('MaxFileSize='+str(self.maxFileSize)+'\n')
  155. f.write('WriteSubtitles='+str(self.writeSubs)+'\n')
  156. f.write('WriteAllSubtitles='+str(self.writeAllSubs)+'\n')
  157. f.write('WriteAutoSubtitles='+str(self.writeAutoSubs)+'\n')
  158. f.write('EmbedSubs='+str(self.embedSubs)+'\n')
  159. f.write('SubtitlesLanguage='+str(self.subsLang)+'\n')
  160. f.write('OpenDownloadDirectory='+str(self.openDownloadDir)+'\n')
  161. f.write('IgnoreErrors='+str(self.ignoreErrors)+'\n')
  162. f.write('WriteDescription='+str(self.writeDescription)+'\n')
  163. f.write('WriteInfo='+str(self.writeInfo)+'\n')
  164. f.write('WriteThumbnail='+str(self.writeThumbnail)+'\n')
  165. f.write('Retries='+str(self.retries)+'\n')
  166. f.write('UserAgent='+str(self.userAgent)+'\n')
  167. f.write('Referer='+str(self.referer)+'\n')
  168. f.write('Proxy='+str(self.proxy)+'\n')
  169. f.write('Username='+str(self.username)+'\n')
  170. # We dont store password & videoPass for security reasons
  171. f.write('CmdArgs='+str(self.cmdArgs)+'\n')
  172. f.write('EnableLog='+str(self.enableLog)+'\n')
  173. f.write('WriteTimeToLog='+str(self.writeTimeToLog)+'\n')
  174. f.close()