Created
February 25, 2018 16:45
-
-
Save devster31/309f294b1948f3ccd227ccc30ba2d8cb to your computer and use it in GitHub Desktop.
messy post-processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -u | |
MEDUSA_API_KEY=***** | |
file=$1 # {f} | |
location=$2 # {f.dir.dir} | |
database=${3} # {info.DataBase} | |
id=${4:-x} # {info.id} | |
name="${5}" # {info.Name} | |
if [[ ! -f "$1" ]]; then | |
echo "No ouput file" | |
exit 0 | |
fi | |
chmod 664 "${file}" | |
export JAVA_OPTS="-Xmx128M" | |
filebot -script fn:suball --def maxAgeDaysLimit=false maxAgeDays=3000d "${file}" | |
echo "${location/mnt/downloads}" | |
[ ${id} == "x" ] && exit 0 | |
if [[ "${database}" = "AniDB" ]] | |
then | |
id=$(http --body :8081/api/v1/"${MEDUSA_API_KEY}"/ cmd=='shows' sort==name | jq --arg n "${name}" '.data | .[$n].indexerid') | |
fi | |
http --check-status --ignore-stdin --body --pretty=format \ | |
:8081/api/v1/${MEDUSA_API_KEY}/ \ | |
cmd=="show.refresh" \ | |
indexerid=="${id}" | |
printf "\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import csv | |
import logging | |
import json | |
import os.path | |
import requests | |
import sys | |
from collections import defaultdict | |
logging.basicConfig(format='[%(levelname)s] %(asctime)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO) | |
parser = argparse.ArgumentParser(description='Update Medusa') | |
parser.add_argument('csv_file', type=str) | |
args = parser.parse_args() | |
logging.info(args) | |
MEDUSA_URL="http://127.0.0.1" | |
MEDUSA_PORT="8081" | |
MEDUSA_API_KEY="9a6a79116fe1fb6a8c2de90c945daad3" | |
def read_csv(f): | |
ucolumns = defaultdict(set) | |
if os.path.isfile(f): | |
with open(f, 'r') as c: | |
csv_r = csv.DictReader(c) | |
for row in csv_r: | |
for (k,v) in row.items(): | |
ucolumns[k].add(v) | |
return dict([ (k, list(v)) for k, v in ucolumns.items() ]) | |
def show_update(tid): | |
try: | |
r = requests.get(MEDUSA_URL + MEDUSA_PORT + '/api/v1/' + MEDUSA_API_KEY, | |
params={'cmd': 'show.refresh', 'indexerid': tid}) | |
return r.json() | |
except requests.exceptions.ConnectionError as e: | |
logging.error(e) | |
sys.exit(1) | |
def main(): | |
ucol = read_csv(args.csv_file) | |
if len(ucol['ID']) > 1: | |
logging.error('More than one show post-processed, human input required') | |
sys.exit(1) | |
for f in ucol['File']: | |
if os.path.isfile(f): | |
logging.info('Changing permissions to 664 for {}'.format(f)) | |
os.chmod(f, 664) | |
else: | |
logging.error('No output file') | |
# sys.exit(1) | |
for d in ucol['Directory']: | |
logging.info(str.replace(d, 'downloads', 'mnt')) | |
if ucol['DB'][0] == 'AniDB': | |
try: | |
r = requests.get(MEDUSA_URL + MEDUSA_PORT + '/api/v1/' + MEDUSA_API_KEY, | |
params={'cmd': 'shows', 'sort': 'name'}) | |
tvdbid = r.json()['data'].get(ucol['Show'][0]).get('indexerid') | |
except requests.exceptions.ConnectionError as e: | |
logging.error(e) | |
sys.exit(1) | |
else: | |
tvdbid = int(ucol['ID'][0]) | |
ret = show_update(tvdbid) | |
logging.info(json.dumps(ret, indent=4)) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import csv | |
import logging | |
import os.path | |
logging.basicConfig(format='[%(levelname)s] %(asctime)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO) | |
parser = argparse.ArgumentParser() | |
# '/mnt/antares/Media/TV Shows/New Girl (2011)/Season 5/New Girl - S05E21 - Wedding Eve [480p x264 - 2.0 AAC-LC - HDTV]-KILLERS.mkv' \ | |
# '/mnt/antares/Media/TV Shows/New Girl (2011)' \ | |
# TheTVDB \ | |
# 248682 \ | |
# 'New Girl' | |
parser.add_argument('csv_file', type=str) | |
parser.add_argument('video_file', type=str) | |
parser.add_argument('video_folder', type=str) | |
parser.add_argument('db', type=str) | |
parser.add_argument('tvdbid', type=int) | |
parser.add_argument('show', type=str) | |
args = parser.parse_args() | |
logging.info(args) | |
def check_header(csv_f): | |
try: | |
with open(csv_f, 'r') as f: | |
return csv.Sniffer().has_header(f.read()) | |
except csv.Error as e: | |
logging.warn(e) | |
def write_header(csv_f): | |
csv_w = csv.DictWriter(csv_f, fieldnames=['File', 'Directory', 'Show', 'DB', 'ID']) | |
logging.info('Writing csv headers...') | |
csv_w.writeheader() | |
def write_line(csv_f): | |
csv_w = csv.DictWriter(csv_f, fieldnames=['File', 'Directory', 'Show', 'DB', 'ID']) | |
logging.info('Writing new line to csv...') | |
csv_w.writerow({'File': args.video_file, 'Directory': args.video_folder, | |
'Show': args.show, 'DB': args.db, 'ID': args.tvdbid}) | |
def main(): | |
if os.path.isfile(args.csv_file): | |
if not check_header(args.csv_file): | |
with open(args.csv_file, 'w') as c: | |
write_header(c) | |
write_line(c) | |
else: | |
logging.info('Headers present, skipping...') | |
with open(args.csv_file, 'a') as c: | |
write_line(c) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import os | |
import re | |
import subprocess | |
import sys | |
import time | |
def main(): | |
logging.basicConfig(format='[%(levelname)s] %(asctime)s - %(message)s', | |
datefmt='%m/%d/%Y %H:%M:%S', | |
level=logging.INFO) | |
TR_APP_VERSION = os.getenv('TR_APP_VERSION') | |
# Sat Feb 24 23:05:30 2018 | |
TR_TIME_LOCALTIME = time.strptime(os.getenv('TR_TIME_LOCALTIME'), | |
'%a %b %d %H:%M:%S %Y') | |
TR_TORRENT_HASH = os.getenv('TR_TORRENT_HASH') | |
TR_TORRENT_ID = os.getenv('TR_TORRENT_ID') | |
DIR = os.getenv('TR_TORRENT_DIR') | |
NAME = os.getenv('TR_TORRENT_NAME') | |
LABEL = os.getenv('TR_TORRENT_LABEL') | |
pp_path = os.path.join(DIR, NAME) | |
# Configuration | |
CONFIG_OUTPUT="/mnt/antares/Media" | |
FILEBOT="/usr/local/bin/filebot" | |
if ( DIR.startswith('/mnt/usbhdd') or | |
DIR.startswith('/mnt/bellatrix/downloads/books') ): | |
sys.exit(0) | |
links = set() | |
for dirP, dirN, fNL in os.walk(pp_path): | |
for fN in fNL: | |
abs_fN = os.path.abspath(os.path.join(dirP, fN)) | |
if os.path.islink(abs_fN): | |
links.add(abs_fN) | |
if len(links) > 0: | |
logging.info('Folder contains symlinks, ' | |
'has this item already been post-processed?') | |
sys.exit(0) | |
logging.info('Transmission {} finished downloading {} at {} of {}'.format( | |
TR_APP_VERSION, NAME, | |
time.strftime(TR_TIME_LOCALTIME, '%H:%M'), | |
time.strftime(TR_TIME_LOCALTIME, '%Y-%m-%d'))) | |
logging.info('Starting post-processing of {}'.format(pp_path)) | |
subprocess.run(['transmission-remote', '-t', TR_TORRENT_ID, '-S']) | |
fb_cmd = ['sudo', '-H', '-u', 'devster', '-g', 'devster', '--', FILEBOT, | |
'-script', 'fn:amc', '--action', 'keeplink', '--output', CONFIG_OUTPUT, | |
'--conflict', 'skip', '-non-strict', '--log-file', 'amc.log', | |
'--def', | |
'artwork=y', | |
'excludeList=".excludes"', | |
'ut_kind=multi', | |
'ut_dir={}'.format(pp_path), | |
'ut_title={}'.format(NAME), | |
'ut_label={}'.format(LABEL), | |
'@/mnt/antares/scripts/pushover.txt', | |
'movieFormat=@/mnt/antares/scripts/movieFormat.groovy', | |
'seriesFormat=@/mnt/antares/scripts/seriesFormat.groovy', | |
'animeFormat=@/mnt/antares/scripts/animeFormat.groovy'] | |
if re.match(r'^/mnt/bellatrix/downloads/(tv_shows|anime).*', DIR): | |
fb_cmd.extend(['--filter', '!readLines("/mnt/antares/scripts/tv_excludes.txt").contains(n)', | |
'--def', 'exec="/mnt/antares/scripts/post-script.sh {quote file} {quote f.dir.dir} {info.database} {info.id} {quote info.name}"']) | |
else: | |
fb_cmd.extend(['--filter', '!readLines("/mnt/antares/scripts/movie_excludes.txt").contains(n)', | |
'--def', 'subtitles=en', 'exec="chmod 664 {quote file}"']) | |
subprocess.run(fb_cmd, env={'JAVA_OPTS': '-Xmx256M'}) | |
subprocess.run(['transmission-remote', '-t', TR_TORRENT_ID, '-s']) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -xu | |
# Input Parameters from Transmission | |
TR_APP_VERSION="$TR_APP_VERSION" | |
TR_TIME_LOCALTIME="$TR_TIME_LOCALTIME" | |
TR_TORRENT_HASH="$TR_TORRENT_HASH" | |
TR_TORRENT_ID="$TR_TORRENT_ID" | |
ARG_PATH="$TR_TORRENT_DIR/$TR_TORRENT_NAME" | |
ARG_NAME="$TR_TORRENT_NAME" | |
ARG_LABEL="N/A" | |
# Configuration | |
CONFIG_OUTPUT="/mnt/antares/Media" | |
FILEBOT="/usr/local/bin/filebot" | |
if [[ "$TR_TORRENT_DIR" =~ ^/mnt/usbhdd.* ]] | |
then | |
exit 0 | |
fi | |
if [[ "$TR_TORRENT_DIR" =~ ^/mnt/bellatrix/downloads/books.* ]] | |
then | |
exit 0 | |
fi | |
LINKS=($(find "$ARG_PATH" -type l)) | |
if [[ ${#LINKS[@]} -gt 0 ]] | |
then | |
exit 0 | |
fi | |
transmission-remote -t ${TR_TORRENT_ID} -S | |
export JAVA_OPTS="-Xmx256M" | |
if [[ "$TR_TORRENT_DIR" =~ ^/mnt/bellatrix/downloads/(tv_shows|anime).* ]] | |
then | |
# exec="chmod 664 {quote file} ; docker exec rpi3_medusa curl -s -w '\n' 'http://localhost:8081/api/v1/9a6a79116fe1fb6a8c2de90c945daad3?cmd=show.refresh&tvdbid={info.id}'" \ | |
# tmpfile="$(mktemp -p /dev/shm/)" | |
# exec="echo {quote file},{quote f.dir.dir},{info.database},{info.id},{quote info.name} > $tmpfile" | |
sudo -H -u devster -g devster -- $FILEBOT -script fn:amc --action keeplink --output "$CONFIG_OUTPUT" --conflict skip \ | |
--filter '!readLines("/mnt/antares/scripts/tv_excludes.txt").contains(n)' \ | |
-non-strict --log-file amc.log --def artwork=y excludeList=".excludes" \ | |
--def ut_dir="$ARG_PATH" ut_kind="multi" ut_title="$ARG_NAME" ut_label="$ARG_LABEL" \ | |
--def exec="/mnt/antares/scripts/post-script.sh {quote file} {quote f.dir.dir} {info.database} {info.id} {quote info.name}" \ | |
--def @/mnt/antares/scripts/pushover.txt \ | |
--def movieFormat=@/mnt/antares/scripts/movieFormat.groovy \ | |
--def seriesFormat=@/mnt/antares/scripts/seriesFormat.groovy \ | |
--def animeFormat=@/mnt/antares/scripts/animeFormat.groovy | |
else | |
sudo -H -u devster -g devster -- $FILEBOT -script fn:amc --action keeplink --output "$CONFIG_OUTPUT" --conflict skip \ | |
--filter '!readLines("/mnt/antares/scripts/movie_excludes.txt").contains(n)' \ | |
--log-file amc.log --def subtitles=en artwork=y excludeList=".excludes" \ | |
ut_dir="$ARG_PATH" ut_kind="multi" ut_title="$ARG_NAME" ut_label="$ARG_LABEL" \ | |
exec="chmod 664 {quote file}" \ | |
--def @/mnt/antares/scripts/pushover.txt \ | |
--def movieFormat=@/mnt/antares/scripts/movieFormat.groovy \ | |
--def seriesFormat=@/mnt/antares/scripts/seriesFormat.groovy | |
fi | |
transmission-remote -t ${TR_TORRENT_ID} -s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment