From 3996147fb454a276fc0a4896d92bcd411d340312 Mon Sep 17 00:00:00 2001 From: clinton-hall Date: Sat, 9 Dec 2017 08:40:26 +1300 Subject: [PATCH] add par2 rename/repair (linux only). Fixes #1306 --- core/__init__.py | 13 +++++++++-- core/nzbToMediaSceneExceptions.py | 38 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/core/__init__.py b/core/__init__.py index a0cc04e8..11ce4e21 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -195,6 +195,7 @@ OUTPUTFASTSTART = None OUTPUTQUALITYPERCENT = None FFMPEG = None SEVENZIP = None +PAR2CMD = None FFPROBE = None CHECK_MEDIA = None NICENESS = [] @@ -233,7 +234,7 @@ def initialize(section=None): DELETE_ORIGINAL, TORRENT_CHMOD_DIRECTORY, PASSWORDSFILE, USER_DELAY, USER_SCRIPT, USER_SCRIPT_CLEAN, USER_SCRIPT_MEDIAEXTENSIONS, \ USER_SCRIPT_PARAM, USER_SCRIPT_RUNONCE, USER_SCRIPT_SUCCESSCODES, DOWNLOADINFO, CHECK_MEDIA, SAFE_MODE, \ TORRENT_DEFAULTDIR, TORRENT_RESUME_ON_FAILURE, NZB_DEFAULTDIR, REMOTEPATHS, LOG_ENV, PID_FILE, MYAPP, ACHANNELS, ACHANNELS2, ACHANNELS3, \ - PLEXSSL, PLEXHOST, PLEXPORT, PLEXTOKEN, PLEXSEC, TORRENT_RESUME + PLEXSSL, PLEXHOST, PLEXPORT, PLEXTOKEN, PLEXSEC, TORRENT_RESUME, PAR2CMD if __INITIALIZED__: return False @@ -760,7 +761,15 @@ def initialize(section=None): if not SEVENZIP: SEVENZIP = None logger.warning( - "Failed to locate 7zip. Transcosing of disk images and extraction of .7z files will not be possible!") + "Failed to locate 7zip. Transcoding of disk images and extraction of .7z files will not be possible!") + try: + PAR2CMD = subprocess.Popen(['which', 'par2'], stdout=subprocess.PIPE).communicate()[0].strip() + except: + pass + if not PAR2CMD: + PAR2CMD = None + logger.warning( + "Failed to locate par2. Repair and rename using par files will not be possible!") if os.path.isfile(os.path.join(FFMPEG_PATH, 'ffmpeg')) or os.access(os.path.join(FFMPEG_PATH, 'ffmpeg'), os.X_OK): FFMPEG = os.path.join(FFMPEG_PATH, 'ffmpeg') diff --git a/core/nzbToMediaSceneExceptions.py b/core/nzbToMediaSceneExceptions.py index 3607797b..8b88b59f 100644 --- a/core/nzbToMediaSceneExceptions.py +++ b/core/nzbToMediaSceneExceptions.py @@ -3,6 +3,8 @@ import os import re import core import shlex +import platform +import subprocess from core import logger from core.nzbToMediaUtil import listMediaFiles @@ -26,6 +28,7 @@ char_replace = [[r"(\w)1\.(\w)", r"\1i\2"] def process_all_exceptions(name, dirname): + par2(dirname) rename_script(dirname) for filename in listMediaFiles(dirname): newfilename = None @@ -141,6 +144,41 @@ def rename_script(dirname): except Exception as error: logger.error("Unable to rename file due to: {error}".format(error=error), "EXCEPTION") +def par2(dirname): + newlist = [] + sofar = 0 + parfile = "" + objects = os.listdir(dirname) + for item in objects: + if item.endswith(".par2"): + size = os.path.getsize(item) + if size > sofar: + sofar = size + parfile = item + if core.PAR2CMD and parfile: + pwd = os.getcwd() # Get our Present Working Directory + os.chdir(dirname) # set directory to run par on. + if platform.system() == 'Windows': + bitbucket = open('NUL') + else: + bitbucket = open('/dev/null') + logger.info("Running par2 on file {0}.".format(parfile), "PAR2") + command = [core.PAR2CMD, 'r', parfile, "*"] + cmd = "" + for item in command: + cmd = "{cmd} {item}".format(cmd=cmd, item=item) + logger.debug("calling command:{0}".format(cmd), "PAR2") + try: + proc = subprocess.Popen(command, stdout=bitbucket, stderr=bitbucket) + proc.communicate() + result = proc.returncode + except: + logger.error("par2 file processing for {0} has failed".format(parfile), "PAR2") + if result == 0: + logger.info("par2 file processing succeeded", "PAR2") + os.chdir(pwd) + bitbucket.close() + # dict for custom groups # we can add more to this list # _customgroups = {'Q o Q': process_qoq, '-ECI': process_eci}