Heres the code for the sorting, specify a folder and it will go through each folder and each file on the 'fileformats' variable, run 'sort.py -h' for more info
Code: Select all
import os
import sys
import argparse
from datetime import datetime
set_type = ''
fileformats = ['.mp4', '.m4a', '.m4v', '.f4v', '.f4a', '.mkv', '.mk4', '.m4b', '.m4r', '.f4b', '.mov', '.3gp', '.3gp2', '.3g2', '.3gpp', '.3gpp2', '.ogg', '.oga', '.ogv', '.ogx', '.wmv', '.wma', '.asf', '.webm', '.flv']
set_dir = ''
set_log_fldr = 'Logs'
prog = r'C:\path\to\utorrent-postprocess.py'
log = None
def main():
args = args_interp()
arg_fn = args.root
arg_test = args.test
script_path = os.path.dirname(os.path.realpath(__file__))
if args.root:
dir = args.root
elif set_dir:
dir = set_dir;
else:
dir = script_path;
if args.type:
type = args.type
elif set_type:
type = set_type;
else:
type = 'TV';
init_log(script_path,type)
for filename in os.listdir(dir):
error_msg = ''
file_path = dir + '\\' + filename
if os.path.isfile(file_path):
if any(substring in filename for substring in fileformats):
cmd = prog + ' "' + type + '" "5" "' + filename + '" "single" "" "' + file_path + '" "move"'
print('File: ' + filename)
print('cmd: ' + cmd)
write_log('cmd: ' + cmd)
if not (arg_test):
os.system(cmd)
else:
error_msg = 'err type: ' + filename
print(error_msg)
write_log(error_msg)
elif os.path.isdir(file_path):
cmd = prog + ' "' + type + '" "5" "' + filename + '" "multi" "" "' + file_path + '" "move"'
print('Folder: ' + filename)
print('cmd: ' + cmd)
write_log('cmd: ' + cmd)
if not (arg_test):
os.system(cmd)
else:
error_msg = 'err excptn: ' + file_path
print(error_msg)
write_log(error_msg)
close_log(log)
def args_interp():
parser = argparse.ArgumentParser(description='Process files for python script by directory')
parser.add_argument('-r -root -dir', dest='root', metavar='DIR', type=str, default='', help='Set root directory for itteration')
parser.add_argument('-t -type', dest='type', metavar='TYPE', type=str, default='', help='Set type of file')
parser.add_argument('--t --test', dest='test', action='store_true', help='Run test without calling program')
return parser.parse_args()
def init_log(dir,type):
now = datetime.now()
log_fn = type + ' ' + now.strftime('%y-%m-%d') + '.log'
log_fldr = dir + '\\' + set_log_fldr
if not os.path.isdir(log_fldr):
os.mkdir(log_fldr)
log_path = log_fldr + '\\' + log_fn;
global log
log = open(log_path, 'a+')
def write_log(txt):
log.write(txt + '\n')
def close_log(log):
log.close();
main()
Here is my modified utorrent-postprocess script, it varies somewhat from the original, extra argument for process type, dynamic label processing (so it can accept sublabels like 'TV/RSS/Test' and custom folders for TV and Movies, as for movies, I'm british and call them films, with TV, I like to have the year with the name, and the name on the file itself.
Code: Select all
# Run Program:
# "C:\path\to\utorrent-postprocess.py" "%L" "" "%N" "multi" "" "%F" "duplicate"
#
# Test with Console:
# python "C:\path\to\utorrent-postprocess.py" "Movie" "" "Avatar" "multi" "" "X:\Files\Avatar" "duplicate"
from datetime import datetime
import sys
import subprocess
# configuration
output = 'D:/Media'
sudo_movie = ['movie','film']
sudo_tv = ['show','tv','series']
sudo_anime = ['anime']
sudo_music = ['music']
# custom formats (use / instead of \ as directory separator)
#movieFormat = '''{plex}'''
#seriesFormat = '''{plex}'''
movieFormat = '''Films/{n} ({y})/{n} ({y}){' CD'+pi}{'.'+lang}'''
seriesFormat = '''TV/{ny}/{episode.special ? 'Special' : 'Season '+s.pad(2)}/{ny} - {episode.special ? 'S00E'+special.pad(2) : s00e00} - {t.replaceAll(/[`´‘’ʻ]/, /'/).replaceAll(/[!?.]+$/).replacePart(', Part $1')}{'.'+lang}'''
animeFormat = '''{plex}'''
musicFormat = '''{plex}'''
# required args
labelin, state, title, kind, file, directory, cmd = sys.argv[1:8]
label=labelin
if any(substr.lower() in labelin.lower() for substr in sudo_movie):
label="movie"
if any(substr.lower() in labelin.lower() for substr in sudo_tv):
label="tv"
if any(substr.lower() in labelin.lower() for substr in sudo_anime):
label="anime"
if any(substr.lower() in labelin.lower() for substr in sudo_music):
label="music"
ignore="([^\s]+(\.(?i)(bat))$)"
command = [
'filebot', '-script', 'fn:amc',
'--output', output,
'--action', cmd,
'--conflict', 'auto',
'-non-strict',
'--log-file', output + '/AMC/' + label + '-' + datetime.today().strftime('%Y-%m-%d') + '.log',
'--def',
'unsorted=y',
'music=y',
'artwork=y',
'subtitles=en',
'clean=y',
'extras=y',
'minFileSize=0',
'minLengthMS=0',
'pushbullet=o.sdfsdfsfefsdfsefsdfsdfsdf',
'plex=localhost:sdfsdfsfefsdfsefsdfsdfsdf',
'movieFormat=' + movieFormat,
'seriesFormat=' + seriesFormat,
'animeFormat=' + animeFormat,
'musicFormat=' + musicFormat,
'ut_label=' + label,
'ut_state=' + state,
'ut_title=' + title,
'ut_kind=' + kind,
'ut_file=' + file,
'ut_dir=' + directory,
'ignore=' + ignore
]
# execute command only for certain conditions (disabled by default)
'''
if state not in ['5', '11']:
print('Illegal state: %s' % state)
sys.exit(0)
'''
# execute command (and hide cmd window)
subprocess.run(command, creationflags=0x08000000)