@ -2,7 +2,9 @@
# coding: utf-8
from configparser import RawConfigParser , NoOptionError , NoSectionError
from os.path import dirname , splitext , basename , isfile , getmtime
from os.path import dirname , splitext , basename , isfile , getmtime , exists
from yapsy.PluginManager import PluginManagerSingleton
import pluginInterfaces as pi
import re
import unidecode
import logging
@ -24,12 +26,28 @@ VALID_LANGUAGES = ("arabic", "english", "french",
" portuguese " , " punjabi " , " russian " , " spanish " )
VALID_PROGRESS = ( " percentage " , " bigfile " , " accurate " )
def helperFunctionalities ( options ) :
pluginManager = PluginManagerSingleton . get ( )
optionName = " --heartbeat "
if options . get ( optionName ) :
for plugin in pluginManager . getPluginsOfCategory ( pi . PluginTypes . PLATFORM ) :
plugin . plugin_object . heartbeat ( )
return False
else :
options . pop ( optionName )
return True
def get_exception_string ( e ) :
if hasattr ( e , " message " ) :
return str ( e . message )
else :
return str ( e )
def validateVideo ( path ) :
supported_types = [ " video/mp4 " ]
detected_type = magic . from_file ( path , mime = True )
@ -73,6 +91,7 @@ def validateLanguage(language):
else :
return False
def validateDate ( date ) :
return datetime . datetime . strptime ( date , " % Y- % m- %d T % H: % M: % S " )
@ -103,7 +122,7 @@ def validateOriginalDate(originalDate):
def validateThumbnail ( thumbnail ) :
supported_types = [ ' image/jpg ' , ' image/jpeg ' ]
if os . path . exists ( thumbnail ) and \
if exists ( thumbnail ) and \
magic . from_file ( thumbnail , mime = True ) in supported_types :
return thumbnail
else :
@ -148,81 +167,81 @@ def searchOriginalDate(options):
return datetime . datetime . fromtimestamp ( int ( fileModificationDate [ 0 ] ) ) . isoformat ( )
# return the nfo as a RawConfigParser object
def loadNFO ( filename ) :
try :
logger . info ( " Loading " + filename + " as NFO " )
nfo = RawConfigParser ( )
nfo . read ( filename , encoding = ' utf-8 ' )
return nfo
except Exception as e :
logger . critical ( " Problem loading NFO file " + filename + " : " + str ( e ) )
exit ( 1 )
return False
def parseNFO ( options ) :
video_directory = dirname ( options . get ( ' --file ' ) )
directory_name = basename ( video_directory )
nfo_txt = False
nfo_directory = False
nfo_videoname = False
nfo_file = False
nfo_cli = False
if isfile ( video_directory + " / " + " nfo.txt " ) :
nfo_txt = loadNFO ( video_directory + " / " + " nfo.txt " )
elif isfile ( video_directory + " / " + " NFO.txt " ) :
nfo_txt = loadNFO ( video_directory + " / " + " NFO.txt " )
if isfile ( video_directory + " / " + directory_name + " .txt " ) :
nfo_directory = loadNFO ( video_directory + " / " + directory_name + " .txt " )
if options . get ( ' --name ' ) :
if isfile ( video_directory + " / " + options . get ( ' --name ' ) ) :
nfo_videoname = loadNFO ( video_directory + " / " + options . get ( ' --name ' ) + " .txt " )
video_file = splitext ( basename ( options . get ( ' --file ' ) ) ) [ 0 ]
if isfile ( video_directory + " / " + video_file + " .txt " ) :
nfo_file = loadNFO ( video_directory + " / " + video_file + " .txt " )
if options . get ( ' --nfo ' ) :
if isfile ( options . get ( ' --nfo ' ) ) :
nfo_cli = loadNFO ( options . get ( ' --nfo ' ) )
else :
logger . critical ( " Given NFO file does not exist, please check your path. ")
exit ( 1 )
# If there is no NFO and strict option is enabled, then stop there
if options . get ( ' --withNFO ' ) :
if not isinstance ( nfo_cli , RawConfigParser ) and \
not isinstance ( nfo_file , RawConfigParser ) and \
not isinstance ( nfo_videoname , RawConfigParser ) and \
not isinstance ( nfo_directory , RawConfigParser ) and \
not isinstance ( nfo_txt , RawConfigParser ) :
logger . critical ( " You have required the strict presence of NFO but none is found, please use a NFO. ")
exit ( 1 )
# We need to load NFO in this exact order to keep the priorities
# options in cli > nfo_cli > nfo_file > nfo_videoname > nfo_directory > nfo_txt
for nfo in [ nfo_cli , nfo_file , nfo_videoname , nfo_directory , nfo_txt ] :
if nfo :
# We need to check all options and replace it with the nfo value if not defined (None or False)
for key , value in options . items ( ) :
key = key . replace ( " -- " , " " )
try :
# get string options
if value is None and nfo . get ( ' video ', key ) :
options [ ' -- ' + key ] = nfo . get ( ' video ', key )
# get boolean options
elif value is False and nfo . getboolean ( ' video ', key ) :
options [ ' -- ' + key ] = nfo . getboolean ( ' video ', key )
except NoOptionError :
continue
except NoSectionError :
logger . critical ( nfo + " misses section [video], please check syntax of your NFO. ")
exit ( 1 )
return options
# # return the nfo as a RawConfigParser object
# def loadNFO(filename) :
# try :
# logger.info("Loading " + filename + " as NFO" )
# nfo = RawConfigParser( )
# nfo.read(filename, encoding='utf-8' )
# return nfo
# except Exception as e :
# logger.critical("Problem loading NFO file " + filename + ": " + str(e) )
# exit(1 )
# return False
#
#
# def parseNFO(options) :
# video_directory = dirname(options.get('--file') )
# directory_name = basename(video_directory )
# nfo_txt = False
# nfo_directory = False
# nfo_videoname = False
# nfo_file = False
# nfo_cli = False
#
# if isfile(video_directory + "/" + "nfo.txt") :
# nfo_txt = loadNFO(video_directory + "/" + "nfo.txt" )
# elif isfile(video_directory + "/" + "NFO.txt") :
# nfo_txt = loadNFO(video_directory + "/" + "NFO.txt" )
#
# if isfile(video_directory + "/" + directory_name + ".txt") :
# nfo_directory = loadNFO(video_directory + "/" + directory_name + ".txt" )
#
# if options.get('--name') :
# if isfile(video_directory + "/" + options.get('--name')) :
# nfo_videoname = loadNFO(video_directory + "/" + options.get('--name') + ".txt" )
#
# video_file = splitext(basename(options.get('--file')))[0 ]
# if isfile(video_directory + "/" + video_file + ".txt") :
# nfo_file = loadNFO(video_directory + "/" + video_file + ".txt" )
#
# if options.get('--nfo') :
# if isfile(options.get('--nfo')) :
# nfo_cli = loadNFO(options.get('--nfo') )
# else :
# logger.critical(" Given NFO file does not exist, please check your path.")
# exit(1 )
#
# # If there is no NFO and strict option is enabled, then stop there
# if options.get('--withNFO') :
# if not isinstance(nfo_cli, RawConfigParser) and \
# not isinstance(nfo_file, RawConfigParser) and \
# not isinstance(nfo_videoname, RawConfigParser) and \
# not isinstance(nfo_directory, RawConfigParser) and \
# not isinstance(nfo_txt, RawConfigParser) :
# logger.critical(" You have required the strict presence of NFO but none is found, please use a NFO.")
# exit(1 )
#
# # We need to load NFO in this exact order to keep the priorities
# # options in cli > nfo_cli > nfo_file > nfo_videoname > nfo_directory > nfo_txt
# for nfo in [nfo_cli, nfo_file, nfo_videoname, nfo_directory, nfo_txt] :
# if nfo :
# # We need to check all options and replace it with the nfo value if not defined (None or False)
# for key, value in options.items() :
# key = key.replace("--", "" )
# try :
# # get string options
# if value is None and nfo.get('video', key):
# options['--' + key] = nfo.get('video', key)
# # get boolean options
# elif value is False and nfo.getboolean('video', key):
# options['--' + key] = nfo.getboolean('video', key)
# except NoOptionError :
# continue
# except NoSectionError :
# logger.critical(nfo + " misses section [video], please check syntax of your NFO.")
# exit(1 )
# return options
def cleanString ( toclean ) :
@ -231,7 +250,8 @@ def cleanString(toclean):
return cleaned
def getOption ( options , optionName , defaultValue = None ) :
def getOption ( options , optionName , defaultValue = None ) :
value = options . get ( optionName )
options . pop ( optionName )