diff --git a/Universal/ioservice.py b/Universal/ioservice.py new file mode 100644 index 0000000..57e6905 --- /dev/null +++ b/Universal/ioservice.py @@ -0,0 +1,176 @@ +import os +import stat +import time +import shutil +import platform +import configparser + +M_DEFAULTCONFIG = "./Settings.cfg" +M_CONFIGPARSER = configparser.ConfigParser() + +def normalizePath(path): + return os.path.normcase(path) + +def doesFileExist(path): + return os.path.isfile(path) + +def doesFolderExist(path): + path = normalizePath(path) + if os.path.exists(path): + return not doesFileExist(path) + return False + +def getFileSize(path): + path = normalizePath(path) + if doesFileExist(path): + return os.path.getsize(path) + else: + return 0 + +def checkSizeChange(path): + path = normalizePath(path) + if doesFileExist(path): + tmp_size = getFileSize(path) + time.sleep(10) + return (tmp_size == getFileSize(path)) + return False + +def createFolder(path): + path = normalizePath(path) + if doesFolderExist(path): + return True + else: + try: + os.makedirs(path) + return True + except OSError: + return False + +def deleteDirectory(path, attempt=True): + path = normalizePath(path) + if doesFileExist(path): + try: + os.remove(path) + except: + if attempt: + os.chmod(path, stat.S_IWRITE) + os.unlink(path) + deleteDirectory(path, False) + else: + return False + elif doesFolderExist(path): + try: + os.rmdir(path) + except: + if attempt: + os.chmod(path, stat.S_IWRITE) + os.unlink(path) + deleteDirectory(path, False) + else: + return False + return True + +def moveFile(input_path, output_path, copy=False): + input_path = normalizePath(input_path) + output_path = normalizePath(output_path) + folder_created = False + if platform.system() == 'Windows': + folder_created = createFolder(output_path.rsplit("\\", 1)[0]) + else: + folder_created = createFolder(output_path.rsplit("/", 1)[0]) + if folder_created: + try: + if copy: + shutil.copyfile(input_path, output_path) + else: + shutil.move(input_path, output_path) + return True + except shutil.Error: + if copy: + print("Copying failed...") + else: + print("Moving failed...") + return False + except: + if doesFileExist(output_path): + if getFileSize(input_path) == getFileSize(output_path): + if not copy: + try: + deleteDirectory(input_path) + except: + print("Successfully copied file but could not delete input file...") + else: + if copy: + print("Failed to copy file") + else: + print("Failed to move file") + return False + +def listAllFiles(path): + path = normalizePath(path) + try: + tmp_paths = os.listdir(path) + tmp_files = [] + for files in tmp_paths: + tmp_current = os.path.join(path, files) + if doesFolderExist(tmp_current): + tmp_files = tmp_files + listAllFiles(tmp_current) + elif doesFileExist(tmp_current): + tmp_files.append(tmp_current) + return tmp_files + except OSError: + return None + +def deleteEmptyFolders(path, delroot=False): + path = normalizePath(path) + if not os.path.isdir(path): + return + tmp_files = os.listdir(path) + if tmp_files: + for files in tmp_files: + tmp_fullpath = os.path.join(path, files) + if os.path.isdir(tmp_fullpath): + deleteEmptyFolders(tmp_fullpath, True) + tmp_files = os.listdir(path) + if not tmp_files and delroot: + os.rmdir(path) + +def addConfiguration(section, option, key=""): + if not doesFileExist(M_DEFAULTCONFIG): + M_CONFIGPARSER.add_section(section) + M_CONFIGPARSER.set(section, option, key) + else: + M_CONFIGPARSER.read(M_DEFAULTCONFIG) + if not M_CONFIGPARSER.has_section(section): + M_CONFIGPARSER.add_section(section) + if not M_CONFIGPARSER.has_option(section, option): + M_CONFIGPARSER.set(section, option, key) + with open(M_DEFAULTCONFIG, 'w') as tmp: + M_CONFIGPARSER.write(tmp) + +def setConfiguration(section, option, key=""): + if not doesFileExist(M_DEFAULTCONFIG): + addConfiguration(section, option, key) + else: + M_CONFIGPARSER.read(M_CONFIGPARSER) + if not M_CONFIGPARSER.has_section(section): + M_CONFIGPARSER.add_section(section) + M_CONFIGPARSER.set(section, option, key) + with open(M_DEFAULTCONFIG, 'w') as tmp: + M_CONFIGPARSER.write(tmp) + +def getConfigurationStr(section, option): + if doesFileExist(M_DEFAULTCONFIG): + M_CONFIGPARSER.read(M_DEFAULTCONFIG) + if M_CONFIGPARSER.has_section(section) and M_CONFIGPARSER.has_option(section, option): + tmp_key = M_CONFIGPARSER.get(section, option) + if tmp_key != "" or tmp_key.lower().find("none") != -1: + return tmp_key + return None + +def getConfigurationBool(section, option): + if doesFileExist(M_DEFAULTCONFIG): + M_CONFIGPARSER.read(M_DEFAULTCONFIG) + if M_CONFIGPARSER.has_section(section) and M_CONFIGPARSER.has_option(section, option): + return M_CONFIGPARSER.getboolean(section, option) + return None diff --git a/Video/default.py b/Video/default.py new file mode 100644 index 0000000..5e06ec9 --- /dev/null +++ b/Video/default.py @@ -0,0 +1,39 @@ +from Universal import ioservice + +def generateConfigs(): + generateProcessorConfigs() + generateServerConfig() + generateVideoConfig() + +def generateProcessorConfigs(): + ioservice.addConfiguration("Video Configuration", "TMDB API Key") + ioservice.addConfiguration("Video Configuration", "FFProbe Path") + ioservice.addConfiguration("Video Configuration", "Encoder Path") + ioservice.addConfiguration("Video Configuration", "Sleep Timer (Seconds)", "100") + ioservice.addConfiguration("Video Configuration", "Blacklist Words", "1080p, 720p, 480p, 360p, x265, x264") + +def generateServerConfig(): + ioservice.addConfiguration("Server 1", "Type (SFTP/NONE)", "NONE") + ioservice.addConfiguration("Server 1", "Host", "127.0.0.1") + ioservice.addConfiguration("Server 1", "Port", "22") + ioservice.addConfiguration("Server 1", "Username", "NONE") + ioservice.addConfiguration("Server 1", "Password", "NONE") + ioservice.addConfiguration("Server 1", "Key File", "NONE") + ioservice.addConfiguration("Server 1", "Key Type (RSA/DSA)", "RSA") + ioservice.addConfiguration("Server 1", "Media Root", "/Server/Drive/Media/") + +def generateVideoConfig(): + ioservice.addConfiguration("Video Library 1", "Name", "TV Shows") + ioservice.addConfiguration("Video Library 1", "Input", "./Input/TV-Shows/") + ioservice.addConfiguration("Video Library 1", "Output", "./Output/TV-Shows/") + ioservice.addConfiguration("Video Library 1", "Database Service", "TMDB") + ioservice.addConfiguration("Video Library 1", "Media Type", "SHOWS") + ioservice.addConfiguration("Video Library 1", "Compress Video", "False") + ioservice.addConfiguration("Video Library 1", "Output Format", "mkv") + ioservice.addConfiguration("Video Library 1", "Directory Format", "/${NAME}/Season ${SEASON}/${NAME} S${SEASON}E${EPISODE}.${FORMAT}") + ioservice.addConfiguration("Video Library 1", "Command", "${COMPRESSOR} -i ${INPUT} -o ${OUTPUT}") + ioservice.addConfiguration("Video Library 1", "Subtitle Command", "--srt-file ${SRT_FILE} --srt-lang ${SRT_LANGUAGE}") + ioservice.addConfiguration("Video Library 1", "Delete Input", "False") + ioservice.addConfiguration("Video Library 1", "Server", "None") + ioservice.addConfiguration("Video Library 1", "Server Path", "/TV Shows/${NAME}/Season ${SEASON}/${NAME} S${SEASON}E${EPISODE}.${FORMAT}") + ioservice.addConfiguration("Video Library 1", "Server Overwrite", "False")