From 23c51b1d5625c2299b3d43f6d635671d2ae7d72a Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Wed, 26 Mar 2014 18:44:23 +0200 Subject: [PATCH] Fixed old settings file problem, os.makedirs(~) problem --- youtube_dl_gui/OptionsHandler.py | 77 ++++++++++++++++++-------------- youtube_dl_gui/Utils.py | 23 +++++++--- youtube_dl_gui/YoutubeDLGUI.py | 37 +++++++++------ 3 files changed, 84 insertions(+), 53 deletions(-) diff --git a/youtube_dl_gui/OptionsHandler.py b/youtube_dl_gui/OptionsHandler.py index 5438232..66c29b1 100644 --- a/youtube_dl_gui/OptionsHandler.py +++ b/youtube_dl_gui/OptionsHandler.py @@ -14,7 +14,8 @@ class OptionsHandler(): settings_abs_path = '' - def __init__(self): + def __init__(self, statusBarWrite): + self.statusBarWrite = statusBarWrite self.load_default() self.set_settings_path() if file_exist(self.settings_abs_path): @@ -28,7 +29,7 @@ class OptionsHandler(): self.keepVideo = False self.audioQuality = 5 self.proxy = "" - self.savePath = "" + self.savePath = get_HOME() self.autoUpdate = False self.videoFormat = "highest available" self.userAgent = "" @@ -55,7 +56,11 @@ class OptionsHandler(): def set_settings_path(self): self.settings_abs_path = get_HOME() - if get_os_type() != 'nt': + ''' + On Linux save settings file under $HOME/LINUX_SAVEPATH/settings. + On windows save settings file under %UserProfile%/settings + ''' + if get_os_type() != 'nt': self.settings_abs_path += LINUX_SAVEPATH self.settings_abs_path = fix_path(self.settings_abs_path) self.settings_abs_path += SETTINGS_FILENAME @@ -70,37 +75,41 @@ class OptionsHandler(): opts = [] for option in self.read_from_file(): opts.append(option.split('=')[1].rstrip('\n')) - self.ignoreErrors = opts[0] in ['True'] - self.idAsName = opts[1] in ['True'] - self.toAudio = opts[2] in ['True'] - self.audioFormat = opts[3] - self.keepVideo = opts[4] in ['True'] - self.audioQuality = int(opts[5]) - self.proxy = opts[6] - self.savePath = opts[7].decode('utf8') - self.autoUpdate = opts[8] in ['True'] - self.videoFormat = opts[9] - self.userAgent = opts[10] - self.referer = opts[11] - self.username = opts[12] - self.startTrack = opts[13] - self.endTrack = opts[14] - self.maxDownloads = opts[15] - self.rateLimit = opts[16] - self.retries = opts[17] - self.writeDescription = opts[18] in ['True'] - self.writeInfo = opts[19] in ['True'] - self.writeThumbnail = opts[20] in ['True'] - self.minFileSize = opts[21] - self.maxFileSize = opts[22] - self.writeSubs = opts[23] in ['True'] - self.writeAllSubs = opts[24] in ['True'] - self.subsLang = opts[25] - self.writeAutoSubs = opts[26] in ['True'] - self.cmdArgs = opts[27] - self.dashAudioFormat = opts[28] - self.clearDashFiles = opts[29] in ['True'] - self.updatePath = opts[30] + try: + self.ignoreErrors = opts[0] in ['True'] + self.idAsName = opts[1] in ['True'] + self.toAudio = opts[2] in ['True'] + self.audioFormat = opts[3] + self.keepVideo = opts[4] in ['True'] + self.audioQuality = int(opts[5]) + self.proxy = opts[6] + self.savePath = opts[7].decode('utf8') + self.autoUpdate = opts[8] in ['True'] + self.videoFormat = opts[9] + self.userAgent = opts[10] + self.referer = opts[11] + self.username = opts[12] + self.startTrack = opts[13] + self.endTrack = opts[14] + self.maxDownloads = opts[15] + self.rateLimit = opts[16] + self.retries = opts[17] + self.writeDescription = opts[18] in ['True'] + self.writeInfo = opts[19] in ['True'] + self.writeThumbnail = opts[20] in ['True'] + self.minFileSize = opts[21] + self.maxFileSize = opts[22] + self.writeSubs = opts[23] in ['True'] + self.writeAllSubs = opts[24] in ['True'] + self.subsLang = opts[25] + self.writeAutoSubs = opts[26] in ['True'] + self.cmdArgs = opts[27] + self.dashAudioFormat = opts[28] + self.clearDashFiles = opts[29] in ['True'] + self.updatePath = opts[30] + except: + self.statusBarWrite('Error while loading settings file') + self.load_default() def save_to_file(self): f = open(self.settings_abs_path, 'w') diff --git a/youtube_dl_gui/Utils.py b/youtube_dl_gui/Utils.py index d2526f6..2eb09d6 100644 --- a/youtube_dl_gui/Utils.py +++ b/youtube_dl_gui/Utils.py @@ -29,13 +29,17 @@ def have_dash_audio(audio): def remove_file(filename): os.remove(filename) + +def get_path_seperator(): + if os.name == 'nt': + return '\\' + else: + return '/' def fix_path(path): if path != '': - if os.name == 'nt': - path += '\\' - else: - path += '/' + if path[-1:] != get_path_seperator(): + path += get_path_seperator() return path def get_HOME(): @@ -44,6 +48,15 @@ def get_HOME(): def add_PATH(path): os.environ["PATH"] += os.pathsep + path +def get_abs_path(path): + sep = get_path_seperator() + path_list = path.split(sep) + for i in range(len(path_list)): + if path_list[i] == '~': + path_list[i] = get_HOME() + path = sep.join(path_list) + return path + def file_exist(filename): return os.path.exists(filename) @@ -52,4 +65,4 @@ def get_os_type(): def makedir(path): os.makedirs(path) - \ No newline at end of file + diff --git a/youtube_dl_gui/YoutubeDLGUI.py b/youtube_dl_gui/YoutubeDLGUI.py index 3d1f616..d4ba9df 100644 --- a/youtube_dl_gui/YoutubeDLGUI.py +++ b/youtube_dl_gui/YoutubeDLGUI.py @@ -32,7 +32,8 @@ from .Utils import ( get_HOME, get_os_type, file_exist, - fix_path + fix_path, + get_abs_path ) if get_os_type() == 'nt': @@ -41,7 +42,9 @@ else: YOUTUBE_DL_FILENAME = 'youtube-dl' TITLE = 'Youtube-dlG' + AUDIOFORMATS = ["mp3", "wav", "aac", "m4a"] + VIDEOFORMATS = ["highest available", "mp4 [1280x720]", "mp4 [640x360]", @@ -52,9 +55,11 @@ VIDEOFORMATS = ["highest available", "mp4 720p(DASH)", "mp4 480p(DASH)", "mp4 360p(DASH)"] + DASH_AUDIO_FORMATS = ["NO SOUND", "DASH m4a audio 128k", "DASH webm audio 48k"] + LANGUAGES = ["English", "Greek", "Portuguese", @@ -107,7 +112,7 @@ class MainFrame(wx.Frame): Publisher.subscribe(self.download_handler, "download") # init Options and DownloadHandler objects - self.optionsList = OptionsHandler() + self.optionsList = OptionsHandler(self.status_bar_write) self.downloadHandler = None # init some thread variables @@ -117,28 +122,32 @@ class MainFrame(wx.Frame): # init urlList for evt_text on self.trackList self.urlList = [] - # pop user for update path if none has entered - if self.optionsList.updatePath == "": - self.pop_update_dialog() - if self.optionsList.updatePath == "": - self.optionsList.updatePath = get_HOME() + # fix update path + self.check_update_path() - # add youtube-dl.exe to %PATH% for windows - if get_os_type() == 'nt': add_PATH(self.optionsList.updatePath) - # check & update libraries (youtube-dl) - self.check_if_youtube_dl_exist(fix_path(self.optionsList.updatePath)) + self.check_if_youtube_dl_exist() if (self.optionsList.autoUpdate): self.status_bar_write("Auto update enable") self.update_youtube_dl() + def check_update_path(self): + if self.optionsList.updatePath == '': + self.pop_update_dialog() + if self.optionsList.updatePath == '': + self.optionsList.updatePath = get_HOME() + self.optionsList.updatePath = get_abs_path(self.optionsList.updatePath) + if get_os_type() == 'nt': + add_PATH(self.optionsList.updatePath) + def pop_update_dialog(self): upDialog = UpdateDialog(self.optionsList) upDialog.ShowModal() upDialog.Destroy() - def check_if_youtube_dl_exist(self, path): - if not file_exist(path + YOUTUBE_DL_FILENAME): + def check_if_youtube_dl_exist(self): + path = fix_path(self.optionsList.updatePath)+YOUTUBE_DL_FILENAME + if not file_exist(path): self.status_bar_write("Youtube-dl is missing, trying to download it...") self.update_youtube_dl() @@ -328,7 +337,7 @@ class UpdatePanel(wx.Panel): self.optionsList = optionsList wx.Panel.__init__(self, parent) - wx.StaticText(self, -1, 'Update Path', (25, 20)) + wx.StaticText(self, -1, 'Update Path (If you edit this value restart youtube-dlG)', (25, 20)) self.updatePathBox = wx.TextCtrl(self, -1, pos=(20, 40), size=(450, -1)) self.autoUpdateChk = wx.CheckBox(self, -1, 'Auto Update', (25, 80))