#!/usr/bin/env python3 import os import sys import re import subprocess import shutil import json video_exts = [ '.mkv', '.mp4', '.avi' ] meta_exts = [ '.nfo', '.srt', # We can get these on our own. '.jpg', '.jpeg', '.png', '.bmp' ] def filebot_rename(src, dest, action, name=None, format=None): if not os.path.isdir(dest): os.makedirs(dest) args = ['filebot', '-rename', src, '-non-strict', '--output', dest, '--action', action] if name is not None: args.append('--filter') args.append('name == \"' + name + '\"') if format is not None: args.append('--format') args.append(format) subprocess.run(args) def unrar(src, dest='extract'): if not os.path.isdir(dest): os.makedirs(dest) subprocess.run(['unrar', 'x', '-y', src, dest + '/']) return dest # Load the config. cfgpath = os.path.join(os.path.expanduser('~'), '.config', 'bootybot.conf') if 'BOOTYCFG' in os.environ: cfgpath = os.path.expanduser(os.environ['BOOTYCFG']) cfg = None with open(cfgpath) as data: cfg = json.load(data) # Now go over all of the input files and figure out what to do with them. sources = [] rarregex = re.compile('\\.r[0-9][0-9]') for f in os.listdir(os.getcwd()): if os.path.isdir(f): continue name, ext = os.path.splitext(f) if ext in video_exts: sources.append({ 'src': f, 'action': 'hardlink' }) elif ext == '.rar': contents = unrar(f) for ff in os.listdir(contents): _, ext2 = os.path.splitext(ff) if ext2 in video_exts: sources.append({ 'src': os.path.join(contents, ff), 'action': 'move' }) else: pass elif ext in meta_exts or rarregex.match(ext) is not None: pass else: print('warning: ignoring file because of extension: ' + f) #raw_tv_episode_regex = re.compile('\\.S[0-9][0-9]E[0-9][0-9]\\.') #raw_tv_season_regex = re.compile('\\.S[0-9]\\.') #tv_name_regex = re.compile('.* -') #movie_regex = re.compile('.* \\([0-9][0-9][0-9][0-9]\\)') outdir = cfg['outdir'] if not os.path.isdir(outdir): os.makedirs(outdir, mode=0o755) print('created destination directory ' + outdir) for e in sources: filebot_rename(e['src'], outdir, e['action'], format='{plex}')