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.

191 lines
6.3 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
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
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. )
  10. SETTINGS_FILENAME = 'settings'
  11. LINUX_FILES_PATH = get_HOME() + '/.youtube-dl-gui'
  12. WINDOWS_FILES_PATH = get_HOME() + '\\youtube-dl-gui'
  13. class OptionsHandler():
  14. settings_abs_path = ''
  15. def __init__(self, statusBarWrite):
  16. self.statusBarWrite = statusBarWrite
  17. self.set_settings_path()
  18. self.load_default()
  19. self.load_settings()
  20. def load_settings(self):
  21. if not file_exist(self.get_config_path()):
  22. makedir(self.get_config_path())
  23. if file_exist(self.settings_abs_path):
  24. self.load_from_file()
  25. def load_default(self):
  26. self.savePath = get_HOME()
  27. self.videoFormat = "default"
  28. self.dashAudioFormat = "NO SOUND"
  29. self.clearDashFiles = False
  30. self.toAudio = False
  31. self.keepVideo = False
  32. self.audioFormat = "mp3"
  33. self.audioQuality = "mid"
  34. self.outputFormat = "title"
  35. self.outputTemplate = '%(uploader)s/%(title)s.%(ext)s'
  36. self.startTrack = 1
  37. self.endTrack = 0
  38. self.maxDownloads = 0
  39. self.minFileSize = "0"
  40. self.maxFileSize = "0"
  41. self.writeSubs = False
  42. self.writeAllSubs = False
  43. self.writeAutoSubs = False
  44. self.embedSubs = False
  45. self.subsLang = "English"
  46. self.openDownloadDir = False
  47. self.ignoreErrors = True
  48. self.writeDescription = False
  49. self.writeInfo = False
  50. self.writeThumbnail = False
  51. self.retries = 10
  52. self.userAgent = ""
  53. self.referer = ""
  54. self.proxy = ""
  55. self.username = ""
  56. self.password = ""
  57. self.videoPass = ""
  58. self.updatePath = self.get_config_path()
  59. self.autoUpdate = False
  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.updatePath = opts[30].decode('utf8')
  127. self.autoUpdate = opts[31] in ['True']
  128. self.cmdArgs = opts[32]
  129. self.enableLog = opts[33] in ['True']
  130. self.writeTimeToLog = opts[34] in ['True']
  131. except:
  132. self.statusBarWrite('Error while loading settings file. Loading default settings.')
  133. self.load_default()
  134. else:
  135. self.statusBarWrite('Settings file version problem. Loading default settings.')
  136. self.load_default()
  137. def save_to_file(self):
  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('UpdatePath='+self.updatePath.encode('utf-8')+'\n')
  172. f.write('AutoUpdate='+str(self.autoUpdate)+'\n')
  173. f.write('CmdArgs='+str(self.cmdArgs)+'\n')
  174. f.write('EnableLog='+str(self.enableLog)+'\n')
  175. f.write('WriteTimeToLog='+str(self.writeTimeToLog)+'\n')
  176. f.close()