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.

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