Browse Source

Made output configurable and fixed renaming of extracted files.

master
treyzania 6 years ago
parent
commit
d217e7496b
1 changed files with 51 additions and 27 deletions
  1. 51
    27
      booty.py

+ 51
- 27
booty.py View File

@@ -3,8 +3,10 @@
import os
import sys

import re
import subprocess
import shutil
import json

video_exts = [
'.mkv',
@@ -14,53 +16,75 @@ video_exts = [

meta_exts = [
'.nfo',
'.srt' # We can get these on our own.
'.srt', # We can get these on our own.
'.jpg',
'.jpeg',
'.png',
'.bmp'
]

def filebot_rename(src, dest, name, action):
filter = 'n == \"' + name + '\"'
subprocess.run([
'filebot',
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',
'--filter', filter,
'--action', action,
'--output', dest])
'--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'):
subprocess.run(['unrar', 'x', dest + '/'])
if not os.path.isdir(dest):
os.makedirs(dest)
subprocess.run(['unrar', 'x', '-y', src, dest + '/'])
return dest

def process_rename(ofile, destdir):
shutil.copy(ofile, destdir)

dest = sys.argv[1]
if not os.path.isdir(dest):
os.makedirs(dest, mode=0o755)
print('created destination directory ' + dest)

medianame = sys.argv[2]
to_rename = []
# 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:
to_rename.append({ 'srcname': f, 'action': 'hardlink' })
elif ext == 'rar':
sources.append({ 'src': f, 'action': 'hardlink' })
elif ext == '.rar':
contents = unrar(f)
for ff in os.listdir(contents):
if ext in video_exts:
to_rename.append({ 'srcname': ff, 'action': 'copy' })
_, 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:
# just an NFO file, ignore it
elif ext in meta_exts or rarregex.match(ext) is not None:
pass
else:
print('warning: ignoring file because of extension: ' + f)

for e in to_rename:
filebot_rename(e['srcname'], dest, medianame, e['action'])
#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}')

Loading…
Cancel
Save