|
|
|
@ -1,18 +1,23 @@
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import time
|
|
|
|
|
import default
|
|
|
|
|
import subprocess
|
|
|
|
|
from Video import default
|
|
|
|
|
from Universal import tools
|
|
|
|
|
from Universal import ioservice
|
|
|
|
|
from Universal import loggingservice
|
|
|
|
|
from Video.Services import tmdbservice
|
|
|
|
|
from Universal.network import sftpservice
|
|
|
|
|
|
|
|
|
|
class VideoService:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.logger = loggingservice.Output(True, False)
|
|
|
|
|
self.video_settings = default.VideoSettings()
|
|
|
|
|
self.tmdbservice = tmdbservice(self.logger)
|
|
|
|
|
# Movie/TV Show Database
|
|
|
|
|
self.tmdbservice = tmdbservice.TMDBService(self.logger)
|
|
|
|
|
# Network Service
|
|
|
|
|
self.sftpservice = sftpservice.SFTPService(self.logger)
|
|
|
|
|
self.failedFiles = []
|
|
|
|
|
self.video_settings.updateSettings()
|
|
|
|
|
|
|
|
|
|
def startNewProcess(self, num=1):
|
|
|
|
@ -25,14 +30,16 @@ class VideoService:
|
|
|
|
|
self.video_settings.updateServer(self.video_settings.library["Server"])
|
|
|
|
|
while self.is_processing:
|
|
|
|
|
self.processLibraries()
|
|
|
|
|
self.startNewProcess(self.current_library)
|
|
|
|
|
|
|
|
|
|
def processLibraries(self, refreshLib = True):
|
|
|
|
|
def processLibraries(self, refreshLib=True):
|
|
|
|
|
self.logger.debugReport("[Video] processLibraries")
|
|
|
|
|
tmp_library_time = time.time()
|
|
|
|
|
if self.video_settings.library is not None:
|
|
|
|
|
if self.video_settings.library["Input"] is not None:
|
|
|
|
|
if refreshLib:
|
|
|
|
|
self.total_processed = 0
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
self.logger.infoReport("[Video] Current Library: " + self.video_settings.library["Name"])
|
|
|
|
|
self.current_library_list = ioservice.listAllFiles(self.video_settings.library["Input"])
|
|
|
|
|
if self.current_library_list is not None:
|
|
|
|
@ -46,7 +53,10 @@ class VideoService:
|
|
|
|
|
if self.srt_list is not None:
|
|
|
|
|
for item_search in self.srt_list:
|
|
|
|
|
self.current_library_list.remove(item_search)
|
|
|
|
|
self.logger.infoReport("[Video] Found a total of " + str(len(self.current_library_list)) + " files")
|
|
|
|
|
if len(self.current_library_list) == 0:
|
|
|
|
|
self.is_processing = False
|
|
|
|
|
else:
|
|
|
|
|
self.logger.infoReport("[Video] Found a total of " + str(len(self.current_library_list)) + " files")
|
|
|
|
|
for self.current_file in self.current_library_list:
|
|
|
|
|
if self.total_processed != 0:
|
|
|
|
|
self.logger.infoReport("[Video] Total file remaining: " + str(len(self.current_library_list) - self.total_processed))
|
|
|
|
@ -65,6 +75,8 @@ class VideoService:
|
|
|
|
|
self.logger.debugReport("[Video] processVideo")
|
|
|
|
|
self.logger.infoReport("[Video] Current File: " + self.current_file)
|
|
|
|
|
tmp_isDeleted = False
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
tmp_serverPath = None
|
|
|
|
|
if not ioservice.checkSizeChange(self.current_file):
|
|
|
|
|
if ioservice.getFileSize(self.current_file) == 0:
|
|
|
|
|
self.logger.infoReport("[Video] Assuming file has been deleted or moved... Skipping...")
|
|
|
|
@ -73,15 +85,94 @@ class VideoService:
|
|
|
|
|
self.logger.infoReport("[Video] File size has changed")
|
|
|
|
|
self.processVideo()
|
|
|
|
|
else:
|
|
|
|
|
tmp_success = True
|
|
|
|
|
if not tmp_isDeleted:
|
|
|
|
|
tmp_current_file_info = self.extractVideoInformation(self.current_file)
|
|
|
|
|
# self.constructOutputPath(tmp_current_file_info)
|
|
|
|
|
if tmp_current_file_info["Episode"] is None and tmp_current_file_info["Season"] is None:
|
|
|
|
|
self.logger.infoReport("[Video] " + tmp_current_file_info["Name"] + " " + str(tmp_current_file_info["Year"]))
|
|
|
|
|
tmp_currentFileDetails = self.extractVideoInformation(self.current_file)
|
|
|
|
|
self.constructOutputPath(tmp_currentFileDetails)
|
|
|
|
|
if tmp_currentFileDetails["Episode"] is None and tmp_currentFileDetails["Season"] is None:
|
|
|
|
|
self.logger.infoReport("[Video] " + tmp_currentFileDetails["Name"] + " " + str(tmp_currentFileDetails["Year"]))
|
|
|
|
|
else:
|
|
|
|
|
self.logger.infoReport("[Video] " + tmp_current_file_info["Name"] + " Season: " + str(tmp_current_file_info["Season"]) + " Episode: " + str(tmp_current_file_info["Episode"]))
|
|
|
|
|
self.find_srts()
|
|
|
|
|
self.logger.infoReport("[Video] " + tmp_currentFileDetails["Name"] + " Season: " + str(tmp_currentFileDetails["Season"]) + " Episode: " + str(tmp_currentFileDetails["Episode"]))
|
|
|
|
|
self.findSRTs()
|
|
|
|
|
tmp_compress = False
|
|
|
|
|
if not self.video_settings.library["Server"].lower().find("none") != -1:
|
|
|
|
|
self.video_settings.updateServer(self.video_settings.library["Server"])
|
|
|
|
|
tmp_serverPath = self.constructServerPath()
|
|
|
|
|
if self.video_settings.server["Type"].lower().find("sftp") != -1:
|
|
|
|
|
if self.sftpservice.isEnabled():
|
|
|
|
|
if not self.sftpservice.isConnected():
|
|
|
|
|
self.sftpservice.connectSFTPServer(self.video_settings.server["Host"], self.video_settings.server["Port"], self.video_settings.server["Username"],
|
|
|
|
|
self.video_settings.server["Password"], self.video_settings.server["Key File"], self.video_settings.server["Key Type"])
|
|
|
|
|
if self.sftpservice.isConnected():
|
|
|
|
|
if not ioservice.doesFileExist(self.currentFileOutputPath):
|
|
|
|
|
if self.video_settings.library["Server Overwrite"]:
|
|
|
|
|
tmp_compress = True
|
|
|
|
|
else:
|
|
|
|
|
if not self.sftpservice.doesFileExists(tmp_serverPath):
|
|
|
|
|
tmp_compress = True
|
|
|
|
|
else:
|
|
|
|
|
if not ioservice.doesFileExist(self.currentFileOutputPath):
|
|
|
|
|
tmp_compress = True
|
|
|
|
|
if tmp_compress and not self.video_settings.library["Compress"]:
|
|
|
|
|
if self.video_settings.library["Delete Input"]:
|
|
|
|
|
self.logger.infoReport("[Video] Moving file")
|
|
|
|
|
self.is_processing = True
|
|
|
|
|
ioservice.moveFile(self.current_file, self.currentFileOutputPath)
|
|
|
|
|
i = 0
|
|
|
|
|
while i < len(self.srt_to_include):
|
|
|
|
|
ioservice.moveFile(self.srt_to_include[i], self.constructOutputPath(tmp_currentFileDetails, True, i))
|
|
|
|
|
i += 1
|
|
|
|
|
self.is_processing = False
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
self.logger.infoReport("[Video] Move complete")
|
|
|
|
|
else:
|
|
|
|
|
self.logger.infoReport("[Video] Copying file")
|
|
|
|
|
self.is_processing = True
|
|
|
|
|
ioservice.moveFile(self.current_file, self.currentFileOutputPath, True)
|
|
|
|
|
i = 0
|
|
|
|
|
while i < len(self.srt_to_include):
|
|
|
|
|
ioservice.moveFile(self.srt_to_include[i], self.constructOutputPath(tmp_currentFileDetails, True, i), True)
|
|
|
|
|
i += 1
|
|
|
|
|
self.is_processing = False
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
self.logger.infoReport("[Video] Copying complete")
|
|
|
|
|
elif tmp_compress and self.video_settings.library["Compress"]:
|
|
|
|
|
self.logger.infoReport("[Video] Compressing video")
|
|
|
|
|
self.startCompressing(self.current_file, self.currentFileOutputPath, self.srt_to_include)
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
self.logger.infoReport("[Video] Compressing completed")
|
|
|
|
|
if not self.video_settings.library["Server"].lower().find("none") != -1:
|
|
|
|
|
self.video_settings.updateServer(self.video_settings.library["Server"])
|
|
|
|
|
if self.video_settings.server["Type"].lower().find("sftp") != -1:
|
|
|
|
|
if self.sftpservice.isEnabled():
|
|
|
|
|
if not self.sftpservice.isConnected():
|
|
|
|
|
self.sftpservice.connectSFTPServer(self.video_settings.server["Host"], self.video_settings.server["Port"], self.video_settings.server["Username"],
|
|
|
|
|
self.video_settings.server["Password"], self.video_settings.server["Key File"], self.video_settings.server["Key Type"])
|
|
|
|
|
if self.sftpservice.isConnected():
|
|
|
|
|
if self.sftpservice.doesFileExists(tmp_serverPath):
|
|
|
|
|
if self.video_settings.library["Server Overwrite"]:
|
|
|
|
|
if self.sftpservice.getFileSize(tmp_serverPath) != ioservice.getFileSize(self.currentFileOutputPath):
|
|
|
|
|
if self.sftpservice.doesFileExist(tmp_serverPath):
|
|
|
|
|
self.sftpservice.deleteFile(tmp_serverPath)
|
|
|
|
|
if self.sftpservice.uploadFile(self.currentFileOutputPath, tmp_serverPath):
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
self.logger.infoReport("[Video] Upload Complete")
|
|
|
|
|
else:
|
|
|
|
|
tmp_isSuccess = False
|
|
|
|
|
else:
|
|
|
|
|
if self.sftpservice.uploadFile(self.currentFileOutputPath, tmp_serverPath):
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
self.logger.infoReport("[Video] Upload Complete")
|
|
|
|
|
else:
|
|
|
|
|
tmp_isSuccess = False
|
|
|
|
|
else:
|
|
|
|
|
if self.sftpservice.uploadFile(self.currentFileOutputPath, tmp_serverPath):
|
|
|
|
|
tmp_isSuccess = True
|
|
|
|
|
else:
|
|
|
|
|
tmp_isSuccess = False
|
|
|
|
|
if not tmp_isSuccess:
|
|
|
|
|
self.failedFiles = self.failedFiles + [self.current_file]
|
|
|
|
|
else:
|
|
|
|
|
self.total_processed += 1
|
|
|
|
|
|
|
|
|
|
def extractVideoInformation(self, file):
|
|
|
|
|
self.logger.debugReport("[Video] extractVideoInformation")
|
|
|
|
@ -158,21 +249,22 @@ class VideoService:
|
|
|
|
|
tmp_name = tmp_data["name"]
|
|
|
|
|
tmp_year = tmp_data["first_air_date"][:4]
|
|
|
|
|
case "tvdb":
|
|
|
|
|
#NotImplemented
|
|
|
|
|
#TODO: Implement
|
|
|
|
|
pass
|
|
|
|
|
case _:
|
|
|
|
|
#TODO: Implement
|
|
|
|
|
pass
|
|
|
|
|
tmp_name = tmp_name.replace(":", "")
|
|
|
|
|
if tmp_episode_title:
|
|
|
|
|
tmp_episode_title = tmp_episode_title.replace(":", "")
|
|
|
|
|
return {"Name": tmp_name, "Season": tmp_season, "Episode": tmp_episode, "Episode Title": tmp_episode_title, "Episode Year": tmp_episode_year, "Input Format": tmp_input_format}
|
|
|
|
|
return {"Name": tmp_name, "Year": tmp_year, "Season": tmp_season, "Episode": tmp_episode, "Episode Title": tmp_episode_title, "Episode Year": tmp_episode_year, "Format": tmp_input_format}
|
|
|
|
|
|
|
|
|
|
def constructOutputPath(self, file_info, subtitles=False, num=0):
|
|
|
|
|
self.logger.debugReport("[Video] constructOutputPath")
|
|
|
|
|
self.current_output_path = self.video_settings.library["Output"] + self.video_settings.library["Directory"]
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${NAME}", file_info["Name"])
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${YEAR}", file_info["Year"])
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${SEASON}", file_info["Season"])
|
|
|
|
|
self.currentFileOutputPath = self.video_settings.library["Output"] + self.video_settings.library["Directory"]
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${NAME}", file_info["Name"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${YEAR}", file_info["Year"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${SEASON}", file_info["Season"])
|
|
|
|
|
if isinstance(file_info["Episode"], list):
|
|
|
|
|
tmp_episodes = None
|
|
|
|
|
for eps in file_info["Episode"]:
|
|
|
|
@ -186,29 +278,37 @@ class VideoService:
|
|
|
|
|
tmp_episodes = str(eps)
|
|
|
|
|
else:
|
|
|
|
|
tmp_episodes = tmp_episodes + str(eps)
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${EPISODE}", tmp_episodes)
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${EPISODE}", tmp_episodes)
|
|
|
|
|
else:
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${EPISODE}", file_info["Episode"])
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${EPISODE_YEAR}", file_info["Episode Year"])
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${EPISODE_NAME}", file_info["Episode Name"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${EPISODE}", file_info["Episode"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${EPISODE_YEAR}", file_info["Episode Year"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${EPISODE_NAME}", file_info["Episode Title"])
|
|
|
|
|
if not subtitles:
|
|
|
|
|
if self.current_output_path[self.current_output_path.index("${FORMAT}") - 1] != ".":
|
|
|
|
|
if self.currentFileOutputPath[self.currentFileOutputPath.index("${FORMAT}") - 1] != ".":
|
|
|
|
|
if file_info["Format"].startswith("."):
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${FORMAT}", file_info["Format"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${FORMAT}", file_info["Format"])
|
|
|
|
|
else:
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${FORMAT}", "." + file_info["Format"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${FORMAT}", "." + file_info["Format"])
|
|
|
|
|
else:
|
|
|
|
|
if file_info["Format"].startswith("."):
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, ".${FORMAT}", file_info["Format"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, ".${FORMAT}", file_info["Format"])
|
|
|
|
|
else:
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, ".${FORMAT}", "." + file_info["Format"])
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, ".${FORMAT}", "." + file_info["Format"])
|
|
|
|
|
else:
|
|
|
|
|
if self.current_output_path[self.current_output_path.index("${FORMAT}") - 1] != ".":
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, "${FORMAT}", " - " + self.srt_language[num] + ".srt")
|
|
|
|
|
elif self.current_output_path[self.current_output_path.index("${FORMAT}" - 1)] == ".":
|
|
|
|
|
self.current_output_path = tools.replace(self.current_output_path, ".${FORMAT}", " - " + self.srt_language[num] + ".srt")
|
|
|
|
|
self.logger.debugReport("[Video] Output Path: " + self.current_output_path)
|
|
|
|
|
return self.current_output_path
|
|
|
|
|
if self.currentFileOutputPath[self.currentFileOutputPath.index("${FORMAT}") - 1] != ".":
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, "${FORMAT}", " - " + self.srt_language[num] + ".srt")
|
|
|
|
|
elif self.currentFileOutputPath[self.currentFileOutputPath.index("${FORMAT}" - 1)] == ".":
|
|
|
|
|
self.currentFileOutputPath = tools.replace(self.currentFileOutputPath, ".${FORMAT}", " - " + self.srt_language[num] + ".srt")
|
|
|
|
|
self.logger.debugReport("[Video] Output Path: " + self.currentFileOutputPath)
|
|
|
|
|
return self.currentFileOutputPath
|
|
|
|
|
|
|
|
|
|
def startCompressing(input_path, output_path, srt_list):
|
|
|
|
|
#TODO: Implement
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def constructServerPath(self):
|
|
|
|
|
#TODO: Implement
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def findSRTs(self):
|
|
|
|
|
self.logger.debugReport("[Video] findSRTs")
|
|
|
|
@ -223,7 +323,19 @@ class VideoService:
|
|
|
|
|
"sag", "san", "sin", "slo", "slv", "sme", "smo", "sna", "snd", "som", "sot", "spa", "srd", "srp", "ssw", "sun", "swa",
|
|
|
|
|
"swe", "tah", "tam", "tat", "tel", "tgk", "tgl", "tha", "tib", "tir", "ton", "tsn", "tso", "tuk", "tur", "twi", "uig",
|
|
|
|
|
"ukr", "urd", "uzb", "ven", "vie", "vol", "wel", "wln", "wol", "xho", "yid", "yor", "zha", "zul"]
|
|
|
|
|
|
|
|
|
|
self.srt_to_include = []
|
|
|
|
|
self.srt_language = []
|
|
|
|
|
tmp_srt = self.current_file.replace(self.current_file.rsplit(".")[-1], "srt").lower()
|
|
|
|
|
if (ioservice.doesFileExist(tmp_srt)):
|
|
|
|
|
self.srt_to_include.append(tmp_srt)
|
|
|
|
|
self.srt_language.append("eng")
|
|
|
|
|
else:
|
|
|
|
|
for possible_srt in tmp_srt_lang:
|
|
|
|
|
tmp_srt = self.current_file.replace(self.current_file.rsplit(".")[-1], "")[:-1].lower()
|
|
|
|
|
tmp_srt = tmp_srt + "-" + possible_srt + ".srt"
|
|
|
|
|
if (ioservice.doesFileExist(tmp_srt)):
|
|
|
|
|
self.srt_to_include.append(tmp_srt)
|
|
|
|
|
self.srt_language.append(possible_srt)
|
|
|
|
|
|
|
|
|
|
def getVideoLength(self):
|
|
|
|
|
self.logger.debugReport("[Video] getVideoLength")
|
|
|
|
|