mirror of
https://github.com/mtan93/Apps-Community.git
synced 2026-03-15 21:02:48 +00:00
fixed directory names
This commit is contained in:
93
apps/templates/nzbget/scripts/DeleteSamples.py
Normal file
93
apps/templates/nzbget/scripts/DeleteSamples.py
Normal file
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Title: PGBlitz (Reference Title File)
|
||||
# Maintainer: Admin9705
|
||||
# URL: https://pgblitz.com - http://github.pgblitz.com
|
||||
# GNU: General Public License v3.0
|
||||
#
|
||||
# Additions: clinton-hall - https://github.com/Prinz23
|
||||
################################################################################
|
||||
import os
|
||||
import sys
|
||||
|
||||
# NZBGet Exit Codes
|
||||
NZBGET_POSTPROCESS_PARCHECK = 92
|
||||
NZBGET_POSTPROCESS_SUCCESS = 93
|
||||
NZBGET_POSTPROCESS_ERROR = 94
|
||||
NZBGET_POSTPROCESS_NONE = 95
|
||||
|
||||
def is_sample(filePath, inputName, maxSampleSize, SampleIDs):
|
||||
# 200 MB in bytes
|
||||
SIZE_CUTOFF = int(maxSampleSize) * 1024 * 1024
|
||||
if os.path.getsize(filePath) < SIZE_CUTOFF:
|
||||
if 'SizeOnly' in SampleIDs:
|
||||
return True
|
||||
# Ignore 'sample' in files unless 'sample' in Torrent Name
|
||||
for ident in SampleIDs:
|
||||
if ident.lower() in filePath.lower() and not ident.lower() in inputName.lower():
|
||||
return True
|
||||
# Return False if none of these were met.
|
||||
return False
|
||||
|
||||
if not os.environ.has_key('NZBOP_SCRIPTDIR'):
|
||||
print "This script can only be called from NZBGet (11.0 or later)."
|
||||
sys.exit(0)
|
||||
|
||||
if os.environ['NZBOP_VERSION'][0:5] < '11.0':
|
||||
print "NZBGet Version %s is not supported. Please update NZBGet." % (str(os.environ['NZBOP_VERSION']))
|
||||
sys.exit(0)
|
||||
|
||||
print "Script triggered from NZBGet Version %s." % (str(os.environ['NZBOP_VERSION']))
|
||||
status = 0
|
||||
if os.environ.has_key('NZBPP_TOTALSTATUS'):
|
||||
if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
|
||||
print "Download failed with status %s." % (os.environ['NZBPP_STATUS'])
|
||||
status = 1
|
||||
|
||||
else:
|
||||
# Check par status
|
||||
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
|
||||
print "Par-repair failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# Check unpack status
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '1':
|
||||
print "Unpack failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
|
||||
# Unpack was skipped due to nzb-file properties or due to errors during par-check
|
||||
|
||||
if os.environ['NZBPP_HEALTH'] < 1000:
|
||||
print "Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"."
|
||||
print "Please check your Par-check/repair settings for future downloads."
|
||||
status = 1
|
||||
|
||||
else:
|
||||
print "Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful."
|
||||
print "Please check your Par-check/repair settings for future downloads."
|
||||
|
||||
# Check if destination directory exists (important for reprocessing of history items)
|
||||
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
|
||||
print "Nothing to post-process: destination directory", os.environ['NZBPP_DIRECTORY'], "doesn't exist. Setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# All checks done, now launching the script.
|
||||
if status == 1:
|
||||
sys.exit(NZBGET_POSTPROCESS_NONE)
|
||||
|
||||
mediaContainer = os.environ['NZBPO_MEDIAEXTENSIONS'].split(',')
|
||||
SampleIDs = os.environ['NZBPO_SAMPLEIDS'].split(',')
|
||||
for dirpath, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
|
||||
for file in filenames:
|
||||
filePath = os.path.join(dirpath, file)
|
||||
fileName, fileExtension = os.path.splitext(file)
|
||||
if fileExtension in mediaContainer or ".*" in mediaContainer : # If the file is a video file
|
||||
if is_sample(filePath, os.environ['NZBPP_NZBNAME'], os.environ['NZBPO_MAXSAMPLESIZE'], SampleIDs): # Ignore samples
|
||||
print "Deleting sample file: ", filePath
|
||||
try:
|
||||
os.unlink(filePath)
|
||||
except:
|
||||
print "Error: unable to delete file", filePath
|
||||
sys.exit(NZBGET_POSTPROCESS_ERROR)
|
||||
sys.exit(NZBGET_POSTPROCESS_SUCCESS)
|
||||
106
apps/templates/nzbget/scripts/flatten.py
Normal file
106
apps/templates/nzbget/scripts/flatten.py
Normal file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Title: PGBlitz (Reference Title File)
|
||||
# Maintainer: Admin9705
|
||||
# URL: https://pgblitz.com - http://github.pgblitz.com
|
||||
# GNU: General Public License v3.0
|
||||
#
|
||||
# Additions: clinton-hall - https://github.com/Prinz23
|
||||
################################################################################
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
# NZBGet Exit Codes
|
||||
NZBGET_POSTPROCESS_PARCHECK = 92
|
||||
NZBGET_POSTPROCESS_SUCCESS = 93
|
||||
NZBGET_POSTPROCESS_ERROR = 94
|
||||
NZBGET_POSTPROCESS_NONE = 95
|
||||
|
||||
if not os.environ.has_key('NZBOP_SCRIPTDIR'):
|
||||
print "This script can only be called from NZBGet (11.0 or later)."
|
||||
sys.exit(0)
|
||||
|
||||
if os.environ['NZBOP_VERSION'][0:5] < '11.0':
|
||||
print "[ERROR] NZBGet Version %s is not supported. Please update NZBGet." % (str(os.environ['NZBOP_VERSION']))
|
||||
sys.exit(0)
|
||||
|
||||
print "Script triggered from NZBGet Version %s." % (str(os.environ['NZBOP_VERSION']))
|
||||
status = 0
|
||||
if os.environ.has_key('NZBPP_TOTALSTATUS'):
|
||||
if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
|
||||
print "[ERROR] Download failed with status %s." % (os.environ['NZBPP_STATUS'])
|
||||
status = 1
|
||||
|
||||
else:
|
||||
# Check par status
|
||||
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
|
||||
print "[ERROR] Par-repair failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# Check unpack status
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '1':
|
||||
print "[ERROR] Unpack failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
|
||||
# Unpack was skipped due to nzb-file properties or due to errors during par-check
|
||||
|
||||
if os.environ['NZBPP_HEALTH'] < 1000:
|
||||
print "[ERROR] Download health is compromised and Par-check/repair disabled or no .par2 files found. Setting status \"failed\"."
|
||||
print "[ERROR] Please check your Par-check/repair settings for future downloads."
|
||||
status = 1
|
||||
|
||||
else:
|
||||
print "[ERROR] Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is ok so handle as though download successful."
|
||||
print "[WARNING] Please check your Par-check/repair settings for future downloads."
|
||||
|
||||
# Check if destination directory exists (important for reprocessing of history items)
|
||||
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
|
||||
print "[ERROR] Nothing to post-process: destination directory", os.environ['NZBPP_DIRECTORY'], "doesn't exist. Setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# All checks done, now launching the script.
|
||||
if status == 1:
|
||||
sys.exit(NZBGET_POSTPROCESS_NONE)
|
||||
|
||||
def removeEmptyFolders(path, removeRoot=True):
|
||||
#Function to remove empty folders
|
||||
if not os.path.isdir(path):
|
||||
return
|
||||
|
||||
# remove empty subfolders
|
||||
print "[INFO] Checking for empty folders in:%s" % path
|
||||
files = os.listdir(path)
|
||||
if len(files):
|
||||
for f in files:
|
||||
fullpath = os.path.join(path, f)
|
||||
if os.path.isdir(fullpath):
|
||||
removeEmptyFolders(fullpath)
|
||||
|
||||
# if folder empty, delete it
|
||||
files = os.listdir(path)
|
||||
if len(files) == 0 and removeRoot:
|
||||
print "[INFO] Removing empty folder:%s" % path
|
||||
os.rmdir(path)
|
||||
|
||||
directory = os.path.normpath(os.environ['NZBPP_DIRECTORY'])
|
||||
if os.environ['NZBPO_DESTINATIONDIRECTORY'] and os.path.isdir(os.environ['NZBPO_DESTINATIONDIRECTORY']):
|
||||
destination = os.environ['NZBPO_DESTINATIONDIRECTORY']
|
||||
if os.environ['NZBPO_APPENDCATEGORIES'] == 'yes':
|
||||
destination = os.path.join(destination, os.environ['NZBPP_CATEGORY'])
|
||||
else:
|
||||
destination = directory
|
||||
print "Flattening directory: %s" % (directory)
|
||||
for dirpath, dirnames, filenames in os.walk(directory):
|
||||
for fileName in filenames:
|
||||
outputFile = os.path.join(dirpath, fileName)
|
||||
if dirpath == directory:
|
||||
continue
|
||||
target = os.path.join(destination, fileName)
|
||||
try:
|
||||
shutil.move(outputFile, target)
|
||||
except:
|
||||
print "[ERROR] Could not flatten %s" % outputFile
|
||||
removeEmptyFolders(directory) # Cleanup empty directories
|
||||
sys.exit(NZBGET_POSTPROCESS_SUCCESS)
|
||||
167
apps/templates/nzbget/scripts/hash.py
Normal file
167
apps/templates/nzbget/scripts/hash.py
Normal file
@@ -0,0 +1,167 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Title: PGBlitz (Reference Title File)
|
||||
# Maintainer: Admin9705
|
||||
# URL: https://pgblitz.com - http://github.pgblitz.com
|
||||
# GNU: General Public License v3.0
|
||||
#
|
||||
# Original Author: clinton-hall
|
||||
# https://github.com/clinton-hall/GetScripts/blob/master/SafeRename.py
|
||||
#
|
||||
# Modified By: desimaniac (No Acknowledgement of Source Above)
|
||||
################################################################################
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
# NZBGet Exit Codes
|
||||
NZBGET_POSTPROCESS_PARCHECK = 92
|
||||
NZBGET_POSTPROCESS_SUCCESS = 93
|
||||
NZBGET_POSTPROCESS_ERROR = 94
|
||||
NZBGET_POSTPROCESS_NONE = 95
|
||||
|
||||
############################################################
|
||||
# EXTENSION STUFF
|
||||
############################################################
|
||||
|
||||
def do_check():
|
||||
if not os.environ.has_key('NZBOP_SCRIPTDIR'):
|
||||
print "This script can only be called from NZBGet (11.0 or later)."
|
||||
sys.exit(0)
|
||||
|
||||
if os.environ['NZBOP_VERSION'][0:5] < '11.0':
|
||||
print "[ERROR] NZBGet Version %s is not supported. Please update NZBGet." % (str(os.environ['NZBOP_VERSION']))
|
||||
sys.exit(0)
|
||||
|
||||
print "Script triggered from NZBGet Version %s." % (str(os.environ['NZBOP_VERSION']))
|
||||
|
||||
status = 0
|
||||
if 'NZBPP_TOTALSTATUS' in os.environ:
|
||||
if not os.environ['NZBPP_TOTALSTATUS'] == 'SUCCESS':
|
||||
print "[ERROR] Download failed with status %s." % (os.environ['NZBPP_STATUS'])
|
||||
status = 1
|
||||
else:
|
||||
# Check par status
|
||||
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4':
|
||||
print "[ERROR] Par-repair failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# Check unpack status
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '1':
|
||||
print "[ERROR] Unpack failed, setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
if os.environ['NZBPP_UNPACKSTATUS'] == '0' and os.environ['NZBPP_PARSTATUS'] == '0':
|
||||
# Unpack was skipped due to nzb-file properties or due to errors during par-check
|
||||
|
||||
if os.environ['NZBPP_HEALTH'] < 1000:
|
||||
print "[ERROR] Download health is compromised and Par-check/repair disabled or no .par2 files found. " \
|
||||
"Setting status \"failed\"."
|
||||
print "[ERROR] Please check your Par-check/repair settings for future downloads."
|
||||
status = 1
|
||||
|
||||
else:
|
||||
print "[ERROR] Par-check/repair disabled or no .par2 files found, and Unpack not required. Health is " \
|
||||
"ok so handle as though download successful."
|
||||
print "[WARNING] Please check your Par-check/repair settings for future downloads."
|
||||
|
||||
# Check if destination directory exists (important for reprocessing of history items)
|
||||
if not os.path.isdir(os.environ['NZBPP_DIRECTORY']):
|
||||
print "[ERROR] Nothing to post-process: destination directory", os.environ[
|
||||
'NZBPP_DIRECTORY'], "doesn't exist. Setting status \"failed\"."
|
||||
status = 1
|
||||
|
||||
# All checks done, now launching the script.
|
||||
if status == 1:
|
||||
sys.exit(NZBGET_POSTPROCESS_NONE)
|
||||
|
||||
|
||||
def get_file_name(path):
|
||||
try:
|
||||
file_name = os.path.basename(path)
|
||||
extensions = re.findall(r'\.([^.]+)', file_name)
|
||||
ext = '.'.join(extensions)
|
||||
name = file_name.replace(".%s" % ext, '')
|
||||
return name, ext
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def is_file_hash(file_name):
|
||||
hash_regexp = [
|
||||
r'^[a-fA-F0-9]{40}$',
|
||||
r'^[a-fA-F0-9]{32}$',
|
||||
r'^[a-f0-9]{128}$',
|
||||
r'^[a-zA-Z0-9]{42}$'
|
||||
]
|
||||
for hash in hash_regexp:
|
||||
if re.match(hash, file_name):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def find_files(folder, extension=None, depth=None):
|
||||
file_list = []
|
||||
start_count = folder.count(os.sep)
|
||||
for path, subdirs, files in os.walk(folder, topdown=True):
|
||||
for name in files:
|
||||
if depth and path.count(os.sep) - start_count >= depth:
|
||||
del subdirs[:]
|
||||
continue
|
||||
file = os.path.join(path, name)
|
||||
if not extension:
|
||||
file_list.append(file)
|
||||
else:
|
||||
if file.lower().endswith(extension.lower()):
|
||||
file_list.append(file)
|
||||
|
||||
return sorted(file_list, key=lambda x: x.count(os.path.sep), reverse=True)
|
||||
|
||||
|
||||
############################################################
|
||||
# MAIN
|
||||
############################################################
|
||||
|
||||
# do checks
|
||||
do_check()
|
||||
|
||||
# retrieve required variables
|
||||
directory = os.path.normpath(os.environ['NZBPP_DIRECTORY'])
|
||||
nzb_name = os.environ['NZBPP_NZBFILENAME']
|
||||
if nzb_name is None:
|
||||
print("[ERROR] Unable to retrieve NZBPP_NZBFILENAME")
|
||||
sys.exit(NZBGET_POSTPROCESS_ERROR)
|
||||
nzb_name = nzb_name.replace('.nzb', '')
|
||||
|
||||
print("[INFO] Using \"%s\" for hashed filenames" % nzb_name)
|
||||
print("[INFO] Scanning \"%s\" for hashed filenames" % directory)
|
||||
|
||||
# scan for files
|
||||
found_files = find_files(directory)
|
||||
if not found_files:
|
||||
print("[INFO] No files were found in \"%s\"" % directory)
|
||||
sys.exit(NZBGET_POSTPROCESS_NONE)
|
||||
else:
|
||||
print("[INFO] Found %d files to check for hashed filenames" % len(found_files))
|
||||
# loop files checking for file hash
|
||||
moved_files = 0
|
||||
for found_file_path in found_files:
|
||||
# set variable
|
||||
dir_name = os.path.dirname(found_file_path)
|
||||
file_name, file_ext = get_file_name(found_file_path)
|
||||
|
||||
# is this a file hash
|
||||
if is_file_hash(file_name):
|
||||
new_file_path = os.path.join(dir_name, "%s.%s" % (nzb_name, file_ext))
|
||||
print("[INFO] Moving \"%s\" to \"%s\"" % (found_file_path, new_file_path))
|
||||
try:
|
||||
shutil.move(found_file_path, new_file_path)
|
||||
moved_files += 1
|
||||
except Exception:
|
||||
print("[ERROR] Failed moving \"%s\" to \"%s\"" % (found_file_path, new_file_path))
|
||||
|
||||
print("[INFO] Finished processing \"%s\", moved %d files" % (directory, moved_files))
|
||||
|
||||
sys.exit(NZBGET_POSTPROCESS_SUCCESS)
|
||||
1
apps/templates/nzbget/scripts/rarfile/__init__.py
Normal file
1
apps/templates/nzbget/scripts/rarfile/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
2932
apps/templates/nzbget/scripts/rarfile/rarfile.py
Normal file
2932
apps/templates/nzbget/scripts/rarfile/rarfile.py
Normal file
File diff suppressed because it is too large
Load Diff
293
apps/templates/nzbget/scripts/reverse_name.py
Normal file
293
apps/templates/nzbget/scripts/reverse_name.py
Normal file
@@ -0,0 +1,293 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Title: PGBlitz (Reference Title File)
|
||||
# Maintainer: Admin9705
|
||||
# URL: https://pgblitz.com - http://github.pgblitz.com
|
||||
# GNU: General Public License v3.0
|
||||
#
|
||||
# Additions: clinton-hall - https://github.com/Prinz23
|
||||
################################################################################
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import locale
|
||||
|
||||
reverse_list = [r"\.\d{2}e\d{2}s\.", r"\.p0612\.", r"\.[pi]0801\.", r"\.p027\.", r"\.[pi]675\.", r"\.[pi]084\.", r"\.p063\.", r"\b[45]62[xh]\.", r"\.yarulb\.", r"\.vtd[hp]\.",
|
||||
r'\.(?:ld[.-]?)?bew\.', r"\.pir.?(shv|dov|bew|dvd|db|rb)\.", r"\brdvd\.", r"\.vts\.", r"\.reneercs\.", r"\.dcv\.", r"\b(pir|mac)dh\b", r"\.reporp\.", r"\.kcaper\.",
|
||||
r"\.lanretni\.", r"\b3ca\b", r"\bcaa\b", r"\b3pm\b", r"\.cstn\.", r"\.5r\.", r"\brcs\b"]
|
||||
reverse_pattern = re.compile('|'.join(reverse_list), flags=re.IGNORECASE)
|
||||
season_pattern = re.compile(r"(.*\.\d{2}e\d{2}s\.)(.*)", flags=re.IGNORECASE)
|
||||
word_pattern = re.compile(r"([^A-Z0-9]*[A-Z0-9]+)")
|
||||
char_replace = [[r"(\w)1\.(\w)",r"\1i\2"]
|
||||
]
|
||||
garbage_name = re.compile(r"^[a-zA-Z0-9]{2,}$")
|
||||
media_list = [r"\.s\d{2}e\d{2}\.", r"\.2160p\.", r"\.1080[pi]\.", r"\.720p\.", r"\.576[pi]\.", r"\.480[pi]\.", r"\.360p\.", r"\.[xh]26[45]\b", r"\.bluray\.", r"\.[hp]dtv\.",
|
||||
r'\.web(?:[.-]?dl)?\.', r"\.(vhs|vod|dvd|web|bd|br).?rip\.", r"\.dvdr\b", r"\.stv\.", r"\.screener\.", r"\.vcd\.", r"\bhd(cam|rip)\b", r"\.proper\.", r"\.repack\.",
|
||||
r"\.internal\.", r"\bac3\b", r"\baac\b", r"\bmp3\b", r"\.ntsc\.", r"\.pal\.", r"\.secam\.", r"\bdivx\b", r"\bxvid\b", r"\.r5\.", r"\.scr\."]
|
||||
media_pattern = re.compile('|'.join(media_list), flags=re.IGNORECASE)
|
||||
media_extentions = [".mkv", ".mp4", ".avi", ".wmv", ".divx", ".xvid"]
|
||||
|
||||
if 'nt' == os.name:
|
||||
import ctypes
|
||||
|
||||
class WinEnv:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_environment_variable(name):
|
||||
name = unicode(name) # ensures string argument is unicode
|
||||
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
|
||||
env_value = None
|
||||
if n:
|
||||
buf = ctypes.create_unicode_buffer(u'\0'*n)
|
||||
ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
|
||||
env_value = buf.value
|
||||
return env_value
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.get_environment_variable(key)
|
||||
|
||||
def get(self, key, default=None):
|
||||
r = self.get_environment_variable(key)
|
||||
return r if r is not None else default
|
||||
|
||||
evn = WinEnv()
|
||||
else:
|
||||
class LinuxEnv(object):
|
||||
def __init__(self, environ):
|
||||
self.environ = environ
|
||||
|
||||
def __getitem__(self, key):
|
||||
v = self.environ.get(key)
|
||||
try:
|
||||
return v.decode(SYS_ENCODING) if isinstance(v, str) else v
|
||||
except (UnicodeDecodeError, UnicodeEncodeError):
|
||||
return v
|
||||
|
||||
def get(self, key, default=None):
|
||||
v = self[key]
|
||||
return v if v is not None else default
|
||||
|
||||
evn = LinuxEnv(os.environ)
|
||||
|
||||
SYS_ENCODING = None
|
||||
|
||||
try:
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
except (locale.Error, IOError):
|
||||
pass
|
||||
try:
|
||||
SYS_ENCODING = locale.getpreferredencoding()
|
||||
except (locale.Error, IOError):
|
||||
pass
|
||||
|
||||
if not SYS_ENCODING or SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
|
||||
SYS_ENCODING = 'UTF-8'
|
||||
|
||||
|
||||
class ek:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def fix_string_encoding(x):
|
||||
if str == type(x):
|
||||
try:
|
||||
return x.decode(SYS_ENCODING)
|
||||
except UnicodeDecodeError:
|
||||
return None
|
||||
elif unicode == type(x):
|
||||
return x
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def fix_out_encoding(x):
|
||||
if isinstance(x, basestring):
|
||||
return ek.fix_string_encoding(x)
|
||||
return x
|
||||
|
||||
@staticmethod
|
||||
def fix_list_encoding(x):
|
||||
if type(x) not in (list, tuple):
|
||||
return x
|
||||
return filter(lambda i: None is not i, map(ek.fix_out_encoding, x))
|
||||
|
||||
@staticmethod
|
||||
def encode_item(x):
|
||||
try:
|
||||
return x.encode(SYS_ENCODING)
|
||||
except UnicodeEncodeError:
|
||||
return x.encode(SYS_ENCODING, 'ignore')
|
||||
|
||||
@staticmethod
|
||||
def win_encode_unicode(x):
|
||||
if isinstance(x, str):
|
||||
try:
|
||||
return x.decode('UTF-8')
|
||||
except UnicodeDecodeError:
|
||||
return x
|
||||
return x
|
||||
|
||||
@staticmethod
|
||||
def ek(func, *args, **kwargs):
|
||||
if 'nt' == os.name:
|
||||
# convert all str parameter values to unicode
|
||||
args = tuple([x if not isinstance(x, str) else ek.win_encode_unicode(x) for x in args])
|
||||
kwargs = {k: x if not isinstance(x, str) else ek.win_encode_unicode(x) for k, x in
|
||||
kwargs.iteritems()}
|
||||
func_result = func(*args, **kwargs)
|
||||
else:
|
||||
func_result = func(*[ek.encode_item(x) if type(x) == str else x for x in args], **kwargs)
|
||||
|
||||
if type(func_result) in (list, tuple):
|
||||
return ek.fix_list_encoding(func_result)
|
||||
elif str == type(func_result):
|
||||
return ek.fix_string_encoding(func_result)
|
||||
return func_result
|
||||
|
||||
|
||||
class logger:
|
||||
INFO = 'INFO'
|
||||
DETAIL = 'DETAIL'
|
||||
ERROR = 'ERROR'
|
||||
WARNING = 'WARNING'
|
||||
|
||||
@staticmethod
|
||||
def log(message, msg_type=INFO):
|
||||
print('[%s] %s' % (msg_type, message))
|
||||
|
||||
|
||||
def tryInt(s, s_default=0):
|
||||
try:
|
||||
return int(s)
|
||||
except:
|
||||
return s_default
|
||||
|
||||
# NZBGet V11+
|
||||
# Check if the script is called from nzbget 11.0 or later
|
||||
nzbget_version = evn.get('NZBOP_VERSION', '0.1')
|
||||
nzbget_version = tryInt(nzbget_version[:nzbget_version.find(".")])
|
||||
if nzbget_version >= 11:
|
||||
logger.log("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 evn['NZBOP_UNPACK'] != 'yes':
|
||||
logger.log("Please enable option \"Unpack\" in nzbget configuration file, exiting")
|
||||
sys.exit(POSTPROCESS_NONE)
|
||||
|
||||
parstatus = evn['NZBPP_PARSTATUS']
|
||||
|
||||
# Check par status
|
||||
if parstatus == '3':
|
||||
logger.log("Par-check successful, but Par-repair disabled, exiting")
|
||||
sys.exit(POSTPROCESS_NONE)
|
||||
|
||||
if parstatus == '1':
|
||||
logger.log("Par-check failed, setting status \"failed\"")
|
||||
status = 1
|
||||
sys.exit(POSTPROCESS_NONE)
|
||||
|
||||
unpackstatus = evn['NZBPP_UNPACKSTATUS']
|
||||
|
||||
# Check unpack status
|
||||
if unpackstatus == '1':
|
||||
logger.log("Unpack failed, setting status \"failed\"")
|
||||
status = 1
|
||||
sys.exit(POSTPROCESS_NONE)
|
||||
|
||||
directory = evn['NZBPP_DIRECTORY']
|
||||
|
||||
if unpackstatus == '0' and 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 ek.ek(os.walk, directory):
|
||||
for file in filenames:
|
||||
fileExtension = ek.ek(os.path.splitext, file)[1]
|
||||
|
||||
if fileExtension in ['.par2']:
|
||||
logger.log("Post-Process: Unpack skipped and par-check skipped (although par2-files exist), setting status \"failed\"g")
|
||||
status = 1
|
||||
break
|
||||
|
||||
if ek.ek(os.path.isfile, ek.ek(os.path.join, directory, "_brokenlog.txt")) and not status == 1:
|
||||
logger.log("Post-Process: _brokenlog.txt exists, download is probably damaged, exiting")
|
||||
status = 1
|
||||
|
||||
if not status == 1:
|
||||
logger.log("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 ek.ek(os.path.isdir, directory):
|
||||
logger.log("Post-Process: Nothing to post-process: destination directory %s doesn't exist" % directory)
|
||||
status = 1
|
||||
|
||||
# All checks done, now launching the script.
|
||||
|
||||
rd = False
|
||||
videos = 0
|
||||
old_name = None
|
||||
new_name = None
|
||||
base_name = ek.ek(os.path.basename, directory)
|
||||
for dirpath, dirnames, filenames in ek.ek(os.walk, directory):
|
||||
for file in filenames:
|
||||
|
||||
filePath = ek.ek(os.path.join, dirpath, file)
|
||||
fileName, fileExtension = ek.ek(os.path.splitext, file)
|
||||
dirname = ek.ek(os.path.dirname, filePath)
|
||||
|
||||
if reverse_pattern.search(fileName) is not None:
|
||||
na_parts = season_pattern.search(fileName)
|
||||
if na_parts is not None:
|
||||
word_p = word_pattern.findall(na_parts.group(2))
|
||||
new_words = ""
|
||||
for wp in word_p:
|
||||
if wp[0] == ".":
|
||||
new_words += "."
|
||||
new_words += re.sub(r"\W","",wp)
|
||||
for cr in char_replace:
|
||||
new_words = re.sub(cr[0],cr[1],new_words)
|
||||
new_filename = new_words[::-1] + na_parts.group(1)[::-1]
|
||||
else:
|
||||
new_filename = fileName[::-1]
|
||||
logger.log("reversing filename from: %s to %s" % (fileName, new_filename))
|
||||
try:
|
||||
ek.ek(os.rename, filePath, ek.ek(os.path.join, dirpath, new_filename + fileExtension))
|
||||
rd = True
|
||||
except Exception,e:
|
||||
logger.log(e, logger.ERROR)
|
||||
logger.log("Error: unable to rename file %s" % file, logger.ERROR)
|
||||
pass
|
||||
elif (fileExtension.lower() in media_extentions) and (garbage_name.search(fileName) is not None) and (media_pattern.search(base_name) is not None):
|
||||
videos += 1
|
||||
old_name = filePath
|
||||
new_name = ek.ek(os.path.join, dirname, '%s%s' % (base_name, fileExtension))
|
||||
|
||||
if not rd and videos == 1 and old_name is not None and new_name is not None:
|
||||
logger.log("renaming the File %s to the Dirname %s" % (ek.ek(os.path.basename, old_name), base_name))
|
||||
try:
|
||||
ek.ek(os.rename, old_name, new_name)
|
||||
rd = True
|
||||
except Exception,e:
|
||||
logger.log(e, logger.ERROR)
|
||||
logger.log("Error unable to rename file %s" % old_name, logger.ERROR)
|
||||
pass
|
||||
|
||||
if rd:
|
||||
sys.exit(POSTPROCESS_SUCCESS)
|
||||
else:
|
||||
sys.exit(POSTPROCESS_NONE)
|
||||
|
||||
else:
|
||||
logger.log("This script can only be called from NZBGet (11.0 or later).", logger.ERROR)
|
||||
sys.exit(0)
|
||||
294
apps/templates/nzbget/scripts/unzip.py
Normal file
294
apps/templates/nzbget/scripts/unzip.py
Normal file
@@ -0,0 +1,294 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
##############################################################################
|
||||
### NZBGET SCAN SCRIPT ###
|
||||
|
||||
# Unzips zipped nzbs.
|
||||
#
|
||||
# NOTE: This script requires Python to be installed on your system.
|
||||
|
||||
##############################################################################
|
||||
### OPTIONS ###
|
||||
### NZBGET SCAN SCRIPT ###
|
||||
##############################################################################
|
||||
|
||||
import os, zipfile, tarfile, gzip, pickle, datetime, re, struct, locale
|
||||
import rarfile.rarfile as rarfile
|
||||
|
||||
from gzip import FEXTRA, FNAME
|
||||
|
||||
if 'nt' == os.name:
|
||||
import ctypes
|
||||
|
||||
class WinEnv:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_environment_variable(name):
|
||||
name = unicode(name) # ensures string argument is unicode
|
||||
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
|
||||
result = None
|
||||
if n:
|
||||
buf = ctypes.create_unicode_buffer(u'\0'*n)
|
||||
ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
|
||||
result = buf.value
|
||||
return result
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.get_environment_variable(key)
|
||||
|
||||
def get(self, key, default=None):
|
||||
r = self.get_environment_variable(key)
|
||||
return r if r is not None else default
|
||||
|
||||
env_var = WinEnv()
|
||||
else:
|
||||
class LinuxEnv(object):
|
||||
def __init__(self, environ):
|
||||
self.environ = environ
|
||||
|
||||
def __getitem__(self, key):
|
||||
v = self.environ.get(key)
|
||||
try:
|
||||
return v.decode(SYS_ENCODING) if isinstance(v, str) else v
|
||||
except (UnicodeDecodeError, UnicodeEncodeError):
|
||||
return v
|
||||
|
||||
def get(self, key, default=None):
|
||||
v = self[key]
|
||||
return v if v is not None else default
|
||||
|
||||
env_var = LinuxEnv(os.environ)
|
||||
|
||||
|
||||
SYS_ENCODING = None
|
||||
try:
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
except (locale.Error, IOError):
|
||||
pass
|
||||
try:
|
||||
SYS_ENCODING = locale.getpreferredencoding()
|
||||
except (locale.Error, IOError):
|
||||
pass
|
||||
if not SYS_ENCODING or SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
|
||||
SYS_ENCODING = 'UTF-8'
|
||||
|
||||
|
||||
filename = env_var.get('NZBNP_FILENAME')
|
||||
if re.search(r"\.tar\.gz$", filename, flags=re.I) is None:
|
||||
ext = os.path.splitext(filename)[1].lower()
|
||||
else:
|
||||
ext = '.tar.gz'
|
||||
cat = env_var.get('NZBNP_CATEGORY')
|
||||
dir = env_var.get('NZBNP_DIRECTORY')
|
||||
prio = env_var.get('NZBNP_PRIORITY')
|
||||
top = env_var.get('NZBNP_TOP')
|
||||
pause = env_var.get('NZBNP_PAUSED')
|
||||
if 'NZBNP_DUPEKEY' in os.environ:
|
||||
dupekey = env_var.get('NZBNP_DUPEKEY')
|
||||
dupescore = env_var.get('NZBNP_DUPESCORE')
|
||||
dupemode = env_var.get('NZBNP_DUPEMODE')
|
||||
else:
|
||||
dupekey = None
|
||||
dupescore = None
|
||||
dupemode = None
|
||||
|
||||
tmp_zipinfo = os.path.join(os.environ.get('NZBOP_TEMPDIR'), r'nzbget\unzip_scan\info')
|
||||
nzb_list = []
|
||||
|
||||
def read_gzip_info(gzipfile):
|
||||
gf = gzipfile.fileobj
|
||||
pos = gf.tell()
|
||||
|
||||
# Read archive size
|
||||
gf.seek(-4, 2)
|
||||
size = struct.unpack('<I', gf.read())[0]
|
||||
|
||||
gf.seek(0)
|
||||
magic = gf.read(2)
|
||||
if magic != '\037\213':
|
||||
raise IOError, 'Not a gzipped file'
|
||||
|
||||
method, flag, mtime = struct.unpack("<BBIxx", gf.read(8))
|
||||
|
||||
if not flag & FNAME:
|
||||
# Not stored in the header, use the filename sans .gz
|
||||
gf.seek(pos)
|
||||
fname = gzipfile.name
|
||||
if fname.endswith('.gz'):
|
||||
fname = fname[:-3]
|
||||
return fname, size
|
||||
|
||||
if flag & FEXTRA:
|
||||
# Read & discard the extra field, if present
|
||||
gf.read(struct.unpack("<H", gf.read(2)))
|
||||
|
||||
# Read a null-terminated string containing the filename
|
||||
fname = []
|
||||
while True:
|
||||
s = gf.read(1)
|
||||
if not s or s=='\000':
|
||||
break
|
||||
fname.append(s)
|
||||
|
||||
gf.seek(pos)
|
||||
return ''.join(fname), size
|
||||
|
||||
def save_obj(obj, name):
|
||||
tp = os.path.dirname(name)
|
||||
if not os.path.exists(tp):
|
||||
try:
|
||||
os.makedirs(tp)
|
||||
except:
|
||||
print "Error creating Dir " + tp
|
||||
return
|
||||
try:
|
||||
with open(name, 'wb') as f:
|
||||
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
|
||||
except:
|
||||
print "Error saving: " + name
|
||||
|
||||
def load_obj(name):
|
||||
if os.path.isfile(name):
|
||||
try:
|
||||
with open(name, 'rb') as f:
|
||||
return pickle.load(f)
|
||||
except:
|
||||
print "Error loading " + name
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
|
||||
def save_nzb_list():
|
||||
if nzb_list:
|
||||
save_obj(nzb_list, tmp_zipinfo)
|
||||
else:
|
||||
if os.path.isfile(tmp_zipinfo):
|
||||
try:
|
||||
os.unlink(tmp_zipinfo)
|
||||
except:
|
||||
print "Error deleting " + tmp_zipinfo
|
||||
|
||||
def load_nzb_list():
|
||||
global nzb_list
|
||||
nzb_list = load_obj(tmp_zipinfo)
|
||||
if nzb_list:
|
||||
now = datetime.datetime.now()
|
||||
o_l = len(nzb_list)
|
||||
nzb_list[:] = [el for el in nzb_list if (now - el[8]).days < 1]
|
||||
if nzb_list is not None and o_l != len(nzb_list):
|
||||
save_nzb_list()
|
||||
|
||||
def get_files(zf):
|
||||
zi = zf.infolist()
|
||||
zi[:] = [el for el in zi if os.path.splitext(el.filename)[1].lower() == '.nzb']
|
||||
return zi
|
||||
|
||||
def get_tar_files(tf):
|
||||
ti = tf.getmembers()
|
||||
ti[:] = [el for el in ti if el.isfile() and os.path.splitext(el.name)[1].lower() == '.nzb']
|
||||
return ti
|
||||
|
||||
def get_rar_files(rf):
|
||||
ri = rf.infolist()
|
||||
ri[:] = [el for el in ri if os.path.splitext(el.filename)[1].lower() == '.nzb']
|
||||
return ri
|
||||
|
||||
def remove_filename():
|
||||
try:
|
||||
os.unlink(filename)
|
||||
except:
|
||||
print "Error deleting " + filename
|
||||
|
||||
if ext == '.zip':
|
||||
load_nzb_list()
|
||||
zipf = zipfile.ZipFile(filename, mode='r')
|
||||
zf = get_files(zipf)
|
||||
if zf:
|
||||
zipf.extractall(path = dir, members = zf)
|
||||
now = datetime.datetime.now()
|
||||
for z in zf:
|
||||
if nzb_list:
|
||||
nzb_list.append([z.filename, cat, prio, top, pause, dupekey, dupescore, dupemode, now])
|
||||
else:
|
||||
nzb_list = [[z.filename, cat, prio, top, pause, dupekey, dupescore, dupemode, now]]
|
||||
save_nzb_list()
|
||||
zipf.close()
|
||||
|
||||
remove_filename()
|
||||
|
||||
elif ext in ['.tar.gz', '.tar', '.tgz']:
|
||||
load_nzb_list()
|
||||
tarf = tarfile.open(filename, mode='r')
|
||||
tf = get_tar_files(tarf)
|
||||
if tf:
|
||||
tarf.extractall(path = dir, members = tf)
|
||||
now = datetime.datetime.now()
|
||||
for z in tf:
|
||||
if nzb_list:
|
||||
nzb_list.append([z.name, cat, prio, top, pause, dupekey, dupescore, dupemode, now])
|
||||
else:
|
||||
nzb_list = [[z.name, cat, prio, top, pause, dupekey, dupescore, dupemode, now]]
|
||||
save_nzb_list()
|
||||
tarf.close()
|
||||
|
||||
remove_filename()
|
||||
|
||||
elif ext == '.gz':
|
||||
load_nzb_list()
|
||||
gzf =gzip.open(filename, mode='rb')
|
||||
out_filename, size = read_gzip_info(gzf)
|
||||
if out_filename and os.path.splitext(out_filename)[1].lower() == '.nzb':
|
||||
with open(os.path.join(os.path.dirname(filename), out_filename), 'wb') as outf:
|
||||
outf.write(gzf.read())
|
||||
outf.close()
|
||||
|
||||
if gzf and out_filename:
|
||||
now = datetime.datetime.now()
|
||||
if nzb_list:
|
||||
nzb_list.append([os.path.basename(out_filename), cat, prio, top, pause, dupekey, dupescore, dupemode, now])
|
||||
else:
|
||||
nzb_list = [[os.path.basename(out_filename), cat, prio, top, pause, dupekey, dupescore, dupemode, now]]
|
||||
save_nzb_list()
|
||||
gzf.close()
|
||||
|
||||
remove_filename()
|
||||
|
||||
elif ext == '.rar':
|
||||
load_nzb_list()
|
||||
rarf = rarfile.RarFile(filename, mode='r')
|
||||
rf = get_files(rarf)
|
||||
if rf:
|
||||
rarf.extractall(path = dir, members = rf)
|
||||
now = datetime.datetime.now()
|
||||
for r in rf:
|
||||
if nzb_list:
|
||||
nzb_list.append([r.filename, cat, prio, top, pause, dupekey, dupescore, dupemode, now])
|
||||
else:
|
||||
nzb_list = [[r.filename, cat, prio, top, pause, dupekey, dupescore, dupemode, now]]
|
||||
save_nzb_list()
|
||||
rarf.close()
|
||||
|
||||
remove_filename()
|
||||
|
||||
elif ext == '.nzb' and os.path.exists(tmp_zipinfo):
|
||||
load_nzb_list()
|
||||
if nzb_list:
|
||||
ni = None
|
||||
f_l = os.path.basename(filename).lower()
|
||||
for i, nf in enumerate(nzb_list):
|
||||
if os.path.basename(nf[0]).lower() == f_l:
|
||||
ni = i
|
||||
break
|
||||
if ni is not None:
|
||||
print "[NZB] CATEGORY=" + str(nzb_list[ni][1])
|
||||
print "[NZB] PRIORITY=" + str(nzb_list[ni][2])
|
||||
print "[NZB] TOP=" + str(nzb_list[ni][3])
|
||||
print "[NZB] PAUSED=" + str(nzb_list[ni][4])
|
||||
if dupekey is not None:
|
||||
print "[NZB] DUPEKEY=" + str(nzb_list[ni][5])
|
||||
print "[NZB] DUPESCORE=" + str(nzb_list[ni][6])
|
||||
print "[NZB] DUPEMODE=" + str(nzb_list[ni][7])
|
||||
del nzb_list[ni]
|
||||
save_nzb_list()
|
||||
Reference in New Issue
Block a user