diff --git a/Functions/systemFunctions.py b/Functions/systemFunctions.py index 1895315..c0008e5 100644 --- a/Functions/systemFunctions.py +++ b/Functions/systemFunctions.py @@ -1,7 +1,35 @@ -import json, os, shutil +import json, os, shutil, platform from datetime import datetime +from os import listdir +from os.path import isfile, join + +#read JSON and TXT files +def ReadFILE(path): + if(path[-3:] == "txt"): + with open(path, "r") as file: + return file.readlines() + else: + with open(path, 'r') as file: + return json.load(file) + +#Outputs the stored data inside a json file, i.e: +#jsonPath = "dir1/dir2" it will output file[dir1][dir2] +def getDataJSON(filePath, jsonPath): + file = ReadFILE(filePath) + for element in jsonPath.split('/'): + file = file[element] #each element enters a new path + return file + +#Handle the windows path and Linux path +def convertPath(path): + sysos = getDataJSON(setting_path, "System Os") + if(sysos.lower() == 'linux'): + return path + else: + return path.replace('/', '\\') + #Write on JSON files def WriteJSON(filePath, toWrite, mode): with open(filePath, mode) as outfile: @@ -10,8 +38,13 @@ def WriteJSON(filePath, toWrite, mode): #Create "Playlists Informations.json" and "Settings.json" def SettingUp(): currentPath = os.path.abspath(os.getcwd()) + if('/' in currentPath): + currentPath += '/' + else: + currentPath += '\\' + Settings = { - "System Os": "", + "System Os": str(platform.system()), "Settings": { "Quality": "BEST", "Format": "MP3", @@ -22,41 +55,20 @@ def SettingUp(): } } } - WriteJSON(currentPath + "Settings.json", Settings, "w") + onlyfiles = [f for f in listdir(currentPath) if isfile(join(currentPath, f))] + if("Settings.json" not in onlyfiles): + WriteJSON(currentPath + "Settings.json", Settings, 'w') playlistInformations = { "Playlists Informations" : [], "Playlists links": [] } - WriteJSON(currentPath + "Playlists Informations.json", playlistInformations, 'w') + onlyfiles = [f for f in listdir(convertPath(currentPath + "Data/")) if isfile(join(convertPath(currentPath + "Data/"), f))] + if("Playlists Informations.json" not in onlyfiles): + WriteJSON(convertPath(currentPath + "Data/Playlists Informations.json") , playlistInformations, 'w') setting_path = "Settings.json" -#read JSON and TXT files -def ReadFILE(path): - if(path[-3:] == "txt"): - with open(path, "r") as file: - return file.readlines() - else: - with open(path, 'r') as file: - return json.load(file) - -#Outputs the stored data inside a json file, i.e: -#jsonPath = "dir1/dir2" it will output file[dir1][dir2] -def getDataJSON(filePath, jsonPath): - file = ReadFILE(filePath) - for element in jsonPath.split('/'): - file = file[element] #each element enters a new path - return file - -#Handle the windows path and Linux path -def convertPath(path): - sysos = getDataJSON(setting_path, "System Os") - if(sysos.lower() == 'linux'): - return path - else: - return path.replace('/', '\\') - #Set outdated path def SetOutdatedPath(settingFile): settingFile["Settings"]["Paths"]["Outdated Playlists"] = convertPath(settingFile["Settings"]["Paths"]["Playlist"] + 'Outdated Playlists/') diff --git a/main.py b/main.py index 90b57ee..de9a74f 100644 --- a/main.py +++ b/main.py @@ -11,13 +11,13 @@ import logging, json, shutil, os, fnmatch from Functions.systemFunctions import * +#Setting up needed files +SettingUp() + #Importing playlistHandeling from Functions.playlistHandeling import * -#Setting up needed files -SettingUp() - setting_path = "Settings.json" playlist_path = convertPath("Data/Playlists Informations.json")