Skip to content

Instantly share code, notes, and snippets.

@iwconfig
Last active April 2, 2019 15:56
Show Gist options
  • Save iwconfig/8c4477def6a48e7769cb22bed4eb3625 to your computer and use it in GitHub Desktop.
Save iwconfig/8c4477def6a48e7769cb22bed4eb3625 to your computer and use it in GitHub Desktop.
script for transmission to pass downloaded file(s) to filebot and make sure the ownership and permissions are correct
#!/usr/bin/env python3
# Note: on synology, make sure your user is added to transmission
# group so you can have read and write access to the destination
# without sudo.
import subprocess, sys, pwd, grp, os
processed_path = '/tmp/filebot-output-dir'
def run_filebot(path):
series_format = "/volumeUSB1/usbshare/TV-serier/{n}/Säsong {s}" + \
"/{n.replaceTrailingBrackets()} - {s+'x'}{e.pad(2)} - {t.replaceAll(/[!?.]+$/).replaceAll(/[`´‘’ʻ]/, \"'\").lowerTrail().replacePart(replacement = ', Part $1')}"
movie_format = "/volumeUSB2/usbshare/{genres.contains('Documentary') ? 'Dokumentärer' : 'Filmer'}" + \
"/{n.replaceAll(/[:|]/, ' - ')} ({y}) [{vf}]/{n.replaceAll(/[:|]/, ' - ')}({y}{', '+director}) [{vf}{' '+source}]"
music_format = "/volumeUSB1/usbshare/Musik/{media.Performer}/{media.Album}{' ('+y+')'}" + \
"/{media.TrackPosition.pad(2)} {media.Title}"
cmd = ["/usr/local/bin/filebot",
"-script", "/usr/local/filebot/scripts/amc.groovy",
"-rename", path,
"--action", "hardlink",
"--conflict", "skip",
"-non-strict",
"--log", "all",
"--log-file", "/usr/local/filebot/data/sc-transmission/logs/amc.log",
"--def", "excludeList=/usr/local/filebot/data/sc-transmission/amc.excludes",
"subtitles=sv,en",
"unsorted=n",
"music=y",
"artwork=y",
"extras=y",
"seriesFormat={}".format(series_format),
"movieFormat={}".format(movie_format),
"musicFormat={}".format(music_format),
"exec=echo {quote f.dir.parent} > %s" % processed_path
]
return subprocess.run(cmd, env={'LC_ALL': 'sv_SE.utf8'})
def fix_owner_and_permissions(path):
uid = pwd.getpwnam("sc-transmission").pw_uid
gid = grp.getgrnam("transmission").gr_gid
for DIR, _, FILES in os.walk(path):
os.chmod(DIR, 0o775)
os.chown(DIR, uid, gid)
for f in FILES:
f = os.path.join(DIR, f)
os.chmod(f , 0o664)
os.chown(f, uid, gid)
if __name__ == '__main__':
TR_DIR = os.getenv('TR_TORRENT_DIR')
TR_NAME = os.getenv('TR_TORRENT_NAME')
if TR_DIR is not None and TR_NAME is not None:
run_filebot(os.path.join(TR_DIR, TR_NAME))
else:
paths = sys.argv[1:]
if len(paths) == 0:
print("Need one or more paths to feed filebot")
sys.exit(1)
for path in paths:
print('\nProcessing {}\n\n'.format(path))
if run_filebot(path).returncode == 0:
print('\nFixing ownership and permissions')
fix_owner_and_permissions(open(processed_path).read())
print('Removing {}\n\n'.format(processed_path))
os.remove(processed_path)
## For updating kodi, but this can be done already from amc script in filebot.
## Besides that, updating library this way does not work if the library is a shared mysql database.
## Leaving this here anyway in case i need it:
# /usr/bin/wget --header='Content-Type:application/json' --post-data='{"jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "osmc", "params": {"directory":"'"$TR_TORRENT_DIR"'/"}}' "http://localhost:8080/jsonrpc"
#!/bin/sh
# Wrapper for stuff i want to run after torrent completion, and because
# transmission is a PITA when dealing with scripts other than shell.
# I can't get environemntal variables directly from my python script so
# this wrapper acts as a proxy since, for some reason, the variables are
# working only in shell scripts so they are then passed on to the child
# processes. Have not tried /bin/sh -c "/PATH/TO/SCRIPT.py" though, maybe
# that works. Who knows. Oh well.
export LC_ALL=sv_SE.utf8
{
./PATH/TO/YOUR/SCRIPTS/run-filebot.py &
} 2>>/tmp/transmission-complete_error.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment