From 4ffbd25143cc962c2c3bc47aa2b3771df482ef70 Mon Sep 17 00:00:00 2001 From: zybeon Date: Mon, 9 Feb 2015 13:46:41 -0500 Subject: [PATCH] Update and rename nzbToNzbDroneMP4.py to nzbToSonarrMP4.py changed to Sonarr to reflect name change. Completely new code, mostly from mdhiggins and a little from evreichard. This script should be copied to your NZBGet scripts folder. You must change the default setting of the 'sickbeard_mp4_automator' folder to reflect the full path. Editable inside NZBGet web GUI. My only contribute is merging the code, adding the folder option and loading of python modules from that folder. Uses modules JSON and REQUESTS for sending the POST command to Sonarr. My idea for parsing the ini file for sanity checks was from evreichard's code in nzbDroneFactory.py which is obsolete with this script. Still working on adding more debugging output and error escapes --- nzbToNzbDroneMP4.py | 50 ----------------- nzbToSonarrMP4.py | 133 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 50 deletions(-) delete mode 100644 nzbToNzbDroneMP4.py create mode 100644 nzbToSonarrMP4.py diff --git a/nzbToNzbDroneMP4.py b/nzbToNzbDroneMP4.py deleted file mode 100644 index 6b8d3507..00000000 --- a/nzbToNzbDroneMP4.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python - -import os -import sys -import autoProcessMovie -import nzbDroneFactory -from readSettings import ReadSettings -from mkvtomp4 import MkvtoMp4 - -print "nzbToNzbDrone MP4 edition" - -if len(sys.argv) > 3: - settings = ReadSettings(os.path.dirname(sys.argv[0]), "autoProcess.ini") - converter = MkvtoMp4(settings) - path = str(sys.argv[1]) - for r, d, f in os.walk(path): - for files in f: - inputfile = os.path.join(r, files) - if MkvtoMp4(settings).validSource(inputfile): - print "Processing episode " + inputfile - converter.process(inputfile) - - -#NzbDrone -# SABnzbd -if len(sys.argv) ==8: -# SABnzbd argv: -# 1 The final directory of the job (full path) -# 2 The original name of the NZB file -# 3 Clean version of the job name (no path info and ".nzb" removed) -# 4 Indexer's report number (if supported) -# 5 User-defined category -# 6 Group that the NZB was posted in e.g. alt.binaries.x -# 7 Status of post processing. 0 = OK, 1=failed verification, 2=failed unpack, 3=1+2 - print "Script triggered from SABnzbd, starting nzbDroneFactory..." - nzbDroneFactory.scan(sys.argv[1], sys.argv[7]) - -# NZBGet -elif len(sys.argv) == 4: -# NZBGet argv: -# 1 The final directory of the job (full path) -# 2 The original name of the NZB file -# 3 The status of the download: 0 == successful - print "Script triggered from NZBGet, starting nzbDroneFactory..." - nzbDroneFactory.scan(sys.argv[1], sys.argv[3]) - -else: - print "Invalid number of arguments received from client." - print "Running autoProcessMovie as a manual run..." - autoProcessMovie.process('Manual Run', 'Manual Run', 0) diff --git a/nzbToSonarrMP4.py b/nzbToSonarrMP4.py new file mode 100644 index 00000000..632f3679 --- /dev/null +++ b/nzbToSonarrMP4.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python +# +############################################################################## +### NZBGET POST-PROCESSING SCRIPT ### + +# Converts files and passes them to Sonarr for further processing. +# +# NOTE: This script requires Python to be installed on your system. + +############################################################################## +### OPTIONS ### + +# Change to MP4 Automator folder, no quotes and a trailing / +#MP4_FOLDER=~/sickbeard_mp4_automator/ + +### NZBGET POST-PROCESSING SCRIPT ### +############################################################################## + +import os, sys, re, requests, json +sys.path.append(os.environ['NZBPO_MP4_FOLDER']) +from readSettings import ReadSettings +from mkvtomp4 import MkvtoMp4 + +# NZBGet V11+ +# Check if the script is called from nzbget 11.0 or later +if os.environ.has_key('NZBOP_SCRIPTDIR') and not os.environ['NZBOP_VERSION'][0:5] < '11.0': + print "[INFO] Script triggered from NZBGet (11.0 or later)." + + # NZBGet argv: all passed as environment variables. + clientAgent = "nzbget" + # Exit codes used by NZBGet + POSTPROCESS_PARCHECK=92 + POSTPROCESS_SUCCESS=93 + POSTPROCESS_ERROR=94 + POSTPROCESS_NONE=95 + + # Check nzbget.conf options + status = 0 + + if os.environ['NZBOP_UNPACK'] != 'yes': + print "[INFO] Please enable option \"Unpack\" in nzbget configuration file, exiting" + sys.exit(POSTPROCESS_NONE) + + # Check par status + if os.environ['NZBPP_PARSTATUS'] == '3': + print "[INFO] Par-check successful, but Par-repair disabled, exiting" + sys.exit(POSTPROCESS_NONE) + + if os.environ['NZBPP_PARSTATUS'] == '1': + print "[INFO] Par-check failed, setting status \"failed\"" + status = 1 + sys.exit(POSTPROCESS_NONE) + + # Check unpack status + if os.environ['NZBPP_UNPACKSTATUS'] == '1': + print "[INFO] Unpack failed, setting status \"failed\"" + status = 1 + sys.exit(POSTPROCESS_NONE) + + if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] != '2': + # Unpack is disabled or was skipped due to nzb-file properties or due to errors during par-check + + for dirpath, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']): + for file in filenames: + fileExtension = os.path.splitext(file)[1] + + if fileExtension in ['.par2']: + print "[INFO] Post-Process: Unpack skipped and par-check skipped (although par2-files exist), setting status \"failed\"g" + status = 1 + break + + if os.path.isfile(os.path.join(os.environ['NZBPP_DIRECTORY'], "_brokenlog.txt")) and not status == 1: + print "[INFO] Post-Process: _brokenlog.txt exists, download is probably damaged, exiting" + status = 1 + + if not status == 1: + print "[INFO] Neither par2-files found, _brokenlog.txt doesn't exist, considering download successful" + + # Check if destination directory exists (important for reprocessing of history items) + if not os.path.isdir(os.environ['NZBPP_DIRECTORY']): + print "[INFO] Post-Process: Nothing to post-process: destination directory ", os.environ['NZBPP_DIRECTORY'], "doesn't exist" + status = 1 + sys.exit(POSTPROCESS_NONE) + + # All checks done, now launching the script. + settings = ReadSettings(os.path.dirname(sys.argv[0]), os.environ['NZBPO_MP4_FOLDER']+"autoProcess.ini") + + path = os.environ['NZBPP_DIRECTORY'] + converter = MkvtoMp4(settings) + converter.output_dir = None + for r, d, f in os.walk(path): + for files in f: + inputfile = os.path.join(r, files) + #Ignores files under 50MB + if os.path.getsize(inputfile) > 50000000: + if MkvtoMp4(settings).validSource(inputfile): + try: + print "[INFO] Valid file detected: " + inputfile + except: + print "[INFO] Valid file detected" + try: + converter.process(inputfile) + print "[INFO] Successfully converted!" + except: + print "[WARNING] File conversion failed" + sys.exit(POSTPROCESS_ERROR) + #else: + # print "Possible sample file detected: " + inputfile + " skipping file" + + #Send Folder Scan command to Sonarr + #Example:curl http://localhost:8989/api/command -X POST -d '{"name": "downloadedepisodesscan"}' --header "X-Api-Key:XXXXXXXXXXX" + host=settings.Sonarr['host'] + port=settings.Sonarr['port'] + apikey = settings.Sonarr['apikey'] + try: + ssl=int(settings.Sonarr['ssl']) + except (ConfigParser.NoOptionError,ValueError): + ssl=0 + if ssl: + protocol="https://" + else: + protocol="http://" + url = protocol+host+":"+port+"/api/command" + payload = {'name': 'downloadedepisodesscan'} + headers = {'X-Api-Key': apikey} + r = requests.post(url, data=json.dumps(payload), headers=headers) + print "[INFO] Sonarr folder scan is "+r.headers['state'] + + sys.exit(POSTPROCESS_SUCCESS) + +else: + print "[ERROR] This script can only be called from NZBGet (11.0 or later)." + sys.exit(0)