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.

128 lines
4.2 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #! /usr/bin/env python
  2. import os
  3. OS_TYPE = os.name
  4. SETTINGS_FILENAME = 'settings'
  5. class OptionsHandler():
  6. settings_abs_path = ''
  7. def __init__(self):
  8. self.load_default()
  9. self.set_settings_path()
  10. if self.settings_file_exist():
  11. self.load_from_file()
  12. def load_default(self):
  13. self.ignoreErrors = True
  14. self.idAsName = False
  15. self.toAudio = False
  16. self.audioFormat = "mp3"
  17. self.keepVideo = False
  18. self.audioQuality = 5
  19. self.proxy = ""
  20. self.savePath = ""
  21. self.autoUpdate = False
  22. self.videoFormat = "highest available"
  23. self.userAgent = ""
  24. self.referer = ""
  25. self.username = ""
  26. self.startTrack = "1"
  27. self.endTrack = "0"
  28. self.maxDownloads = "0"
  29. self.rateLimit = "0"
  30. self.retries = "10"
  31. self.writeDescription = False
  32. self.writeInfo = False
  33. self.writeThumbnail = False
  34. self.minFileSize = "0"
  35. self.maxFileSize = "0"
  36. self.writeSubs = False
  37. self.writeAllSubs = False
  38. self.subsLang = "English"
  39. self.writeAutoSubs = False
  40. self.cmdArgs = ""
  41. def set_settings_path(self):
  42. self.settings_abs_path = os.path.expanduser('~')
  43. if OS_TYPE == 'nt':
  44. self.settings_abs_path += '\\'
  45. else:
  46. self.settings_abs_path += '/.config/'
  47. self.settings_abs_path += SETTINGS_FILENAME
  48. def settings_file_exist(self):
  49. return os.path.exists(self.settings_abs_path)
  50. def read_from_file(self):
  51. f = open(self.settings_abs_path, 'r')
  52. options = f.readlines()
  53. f.close()
  54. return options
  55. def load_from_file(self):
  56. opts = []
  57. for option in self.read_from_file():
  58. opts.append(option.split('=')[1].rstrip('\n'))
  59. self.ignoreErrors = opts[0] in ['True']
  60. self.idAsName = opts[1] in ['True']
  61. self.toAudio = opts[2] in ['True']
  62. self.audioFormat = opts[3]
  63. self.keepVideo = opts[4] in ['True']
  64. self.audioQuality = int(opts[5])
  65. self.proxy = opts[6]
  66. self.savePath = opts[7].decode('utf8')
  67. self.autoUpdate = opts[8] in ['True']
  68. self.videoFormat = opts[9]
  69. self.userAgent = opts[10]
  70. self.referer = opts[11]
  71. self.username = opts[12]
  72. self.startTrack = opts[13]
  73. self.endTrack = opts[14]
  74. self.maxDownloads = opts[15]
  75. self.rateLimit = opts[16]
  76. self.retries = opts[17]
  77. self.writeDescription = opts[18] in ['True']
  78. self.writeInfo = opts[19] in ['True']
  79. self.writeThumbnail = opts[20] in ['True']
  80. self.minFileSize = opts[21]
  81. self.maxFileSize = opts[22]
  82. self.writeSubs = opts[23] in ['True']
  83. self.writeAllSubs = opts[24] in ['True']
  84. self.subsLang = opts[25]
  85. self.writeAutoSubs = opts[26] in ['True']
  86. self.cmdArgs = opts[27]
  87. def save_to_file(self):
  88. f = open(self.settings_abs_path, 'w')
  89. f.write('IgnoreErrors='+str(self.ignoreErrors)+'\n')
  90. f.write('IdAsName='+str(self.idAsName)+'\n')
  91. f.write('ToAudio='+str(self.toAudio)+'\n')
  92. f.write('AudioFormat='+str(self.audioFormat)+'\n')
  93. f.write('KeepVideo='+str(self.keepVideo)+'\n')
  94. f.write('AudioQuality='+str(self.audioQuality)+'\n')
  95. f.write('Proxy='+str(self.proxy)+'\n')
  96. f.write('SavePath='+self.savePath.encode('utf-8')+'\n')
  97. f.write('AutoUpdate='+str(self.autoUpdate)+'\n')
  98. f.write('VideoFormat='+str(self.videoFormat)+'\n')
  99. f.write('UserAgent='+str(self.userAgent)+'\n')
  100. f.write('Referer='+str(self.referer)+'\n')
  101. f.write('Username='+str(self.username)+'\n')
  102. f.write('StartTrack='+str(self.startTrack)+'\n')
  103. f.write('EndTrack='+str(self.endTrack)+'\n')
  104. f.write('MaxDownloads='+str(self.maxDownloads)+'\n')
  105. f.write('RateLimit='+str(self.rateLimit)+'\n')
  106. f.write('Retries='+str(self.retries)+'\n')
  107. f.write('WriteDescription='+str(self.writeDescription)+'\n')
  108. f.write('WriteInfo='+str(self.writeInfo)+'\n')
  109. f.write('WriteThumbnail='+str(self.writeThumbnail)+'\n')
  110. f.write('MinFileSize='+str(self.minFileSize)+'\n')
  111. f.write('MaxFileSize='+str(self.maxFileSize)+'\n')
  112. f.write('WriteSubtitles='+str(self.writeSubs)+'\n')
  113. f.write('WriteAllSubtitles='+str(self.writeAllSubs)+'\n')
  114. f.write('SubtitlesLanguage='+str(self.subsLang)+'\n')
  115. f.write('WriteAutoSubtitles='+str(self.writeAutoSubs)+'\n')
  116. f.write('CmdArgs='+str(self.cmdArgs)+'\n')
  117. f.close()