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.

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