With the help of my sister (that knows more python than me) here is the script and it works great so far:
Code: Select all
import shutil
import subprocess
import os
import math
from os.path import getsize
def get_plexscan(section):
return '"\"C:\\Program Files (x86)\\Plex\\Plex Media Server\\Plex Media Scanner.exe\" --scan --refresh --section %s --directory \"{folder}\""' % section
def get_child(path):
return [item for item in os.listdir(path) if item not in ('.stignore', '.stfolder')]
def get_command(command, extra):
cmd = list(command)
cmd.extend(extra)
return cmd
#convert file size to human redable (http://stackoverflow.com/questions/5194057/better-way-to-convert-file-sizes-in-python)
def convert_size(size):
if (size == 0):
return '0B'
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size,1024)))
p = math.pow(1024,i)
s = round(size/p,2)
return '%s' % s
#Declare variables
source = "P:\\Processing"
subfolders = os.listdir(source)
logfile = "P:\\Scripts\\Filebot.log"
#Check if log is more than 2Mb and delete it it if needed
logsize = float(convert_size(getsize(logfile)))
#Delete Logfile if it is equal or greater then 2MB
if logsize >= 2:
os.remove(logfile)
filebot_cmd = [
"C:\\Program Files\\Filebot\\filebot.exe", #ie 'C:\Program Files\Filebot\filebot.exe'
# I recommend replacing 'fn:amc' with the path to a locally saved copy of
# amc.groovy. 'fn:amc' downloads the script every time you run filebot.exe
# from filebot.net. If filebot.net is down, your file processing won't be
# able to run. You can download the script from here:
# https://github.com/filebot/scripts/blob/devel/amc.groovy
'-script', 'P:/Scripts/AMC/amc.groovy',#'fn:amc',
# Save log files to same directory where this script file is located
'--log-file', os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Filebot.log'),
'--action', 'move',
'--def', 'clean=y',
'-no-xattr',
'--log=Fine'
]
for folder in subfolders:
folder_path = os.path.join(source, folder)
child = get_child(folder_path)
childcount = len(child)
if not childcount:
#print folder_path + " empty"
continue
if folder == "cps":
#print "Looking for English Movies"
new_filebot = get_command(filebot_cmd, [
'--def', 'movieFormat=D:/Videos/Movies/{plex[1..-1]}',
'--def', 'ut_label=Movies',
'--def', 'exec=' + get_plexscan(1)
])
elif folder == "cpsfr":
#print "Looking for New French Movies and/or English Movies"
dualmovies = os.path.join(source, "dualmovies")
for path, subdirs, files in os.walk(folder_path):
for name in files:
if any(name.endswith(ext) for ext in ('mkv', 'mp4', 'avi')):
current_file = os.path.join(path, name)
audio = subprocess.check_output([
"P:\\apps\\Mediainfo_x64\\MediaInfo.exe",
'--Inform=General;%AudioCount%', current_file
])
intaudio = int(audio)
if intaudio > 1: # Dual audio
if os.path.join(folder_path, name) == current_file:
# File exist at the root
print "Moving file: %s to dualmovies" % name
dst = os.path.join(dualmovies, name)
os.rename(current_file, dst)
else:
# File is in subfolder, move subfolder
print "Moving directory: %s to dualmovies" % path
shutil.move(path, dualmovies)
if len(get_child(folder_path)): # Verify cpsfr
new_filebot = get_command(filebot_cmd, [
'--def', 'movieFormat=D:/Videos/French movies/{plex[1..-1]}',
'--def', 'ut_label=Movies',
'--def', 'exec=' + get_plexscan(17)
])
else:
continue
elif folder == "dualmovies":
# In case the movies were added, but never processed by filebot
#print "Looking for dual audio Movies"
new_filebot = get_command(filebot_cmd, [
'--def', 'movieFormat=D:/Videos/Dual Audio Movies/{plex[1..-1]}',
'--def', 'ut_label=Movies',
'--def', 'exec=' + get_plexscan(18)
])
elif folder == "sickrage_tv":
#print "Looking for New French Series"
new_filebot = get_command(filebot_cmd, [
'--def', 'seriesFormat=D:/Videos/FrenchTV/{n}/{"Season "+s}/{n} - {sxe} - {t}',
'--lang', 'fr',
'--def', 'ut_label=Series',
'--def', 'exec=' + get_plexscan(6)
])
elif folder == "sonarr_tv":
#print "Looking for New English Series"
new_filebot = get_command(filebot_cmd, [
'--def', 'seriesFormat=D:/Videos/TV/{n}/{"Season "+s}/{n} - {sxe} - {t}',
'--filter', 'age < 7',
'--def', 'ut_label=Series',
'--def', 'exec=' + get_plexscan(2)
])
elif folder == "UFC":
#print "Looking for New UFC PPV/FN"
new_filebot = get_command(filebot_cmd, [
'--def', 'movieFormat=D:/Videos/UFC/{plex[1..-1]}',
'--def', 'ut_label=Movies',
'--def', 'exec=' + get_plexscan(3)
])
else: # Move to the next folder in subfolders
continue
new_filebot.extend(['-non-strict', folder_path])
subprocess.Popen(new_filebot).wait()
I had one question, would it be possible to change the drive letter in the {folder} before or while i pass it to