Added NZB_CLIENTAGENT and TORRENT_CLIENTAGENT variables to compliment adding in Nzb client API support for SabNZBD and NzbGet.

Partially added in searching for downloads via download client API calls, more work to be done on this.

NZB Client users but set there client in the cfg, either sabnzbd or nzbget.

Cleaned up some more of the code in TorrentToMedia and nzbToMedia.
This commit is contained in:
echel0n 2014-04-17 18:05:01 -07:00
commit 504ea8ac45
7 changed files with 160 additions and 139 deletions

View file

@ -16,7 +16,7 @@ from nzbtomedia.autoProcess.autoProcessMusic import autoProcessMusic
from nzbtomedia.autoProcess.autoProcessTV import autoProcessTV
from nzbtomedia.extractor import extractor
from nzbtomedia.nzbToMediaUtil import category_search, safeName, is_sample, copy_link, parse_args, flatten, get_dirnames, \
remove_read_only, cleanup_directories, create_torrent_class
remove_read_only, cleanup_directories, create_torrent_class, pause_torrent, resume_torrent
from nzbtomedia import logger
def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent):
@ -215,41 +215,6 @@ def processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID,
cleanup_directories(inputCategory, processCategories, result, outputDestination)
return result
def pause_torrent(clientAgent, TorrentClass, inputHash, inputID, inputName):
# if we are using links with Torrents it means we need to pause it in order to access the files
logger.debug("Stoping torrent %s in %s while processing", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.stop(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.stop_torrent(inputID)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.pause_torrent([inputID])
time.sleep(5) # Give Torrent client some time to catch up with the change
def resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName):
# Hardlink solution for uTorrent, need to implent support for deluge, transmission
if clientAgent in ['utorrent', 'transmission', 'deluge'] and inputHash:
# Delete torrent and torrentdata from Torrent client if processing was successful.
if (int(nzbtomedia.CFG["Torrent"]["deleteOriginal"]) is 1 and result != 1) or nzbtomedia.USELINK == 'move': # if we move files, nothing to resume seeding.
logger.debug("Deleting torrent %s from %s", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.removedata(inputHash)
TorrentClass.remove(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.remove_torrent(inputID, True)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.remove_torrent(inputID, True)
# we always want to resume seeding, for now manually find out what is wrong when extraction fails
else:
logger.debug("Starting torrent %s in %s", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.start(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.start_torrent(inputID)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.resume_torrent([inputID])
time.sleep(5)
def external_script(outputDestination, torrentName, torrentLabel):
final_result = int(0) # start at 0.
@ -327,8 +292,11 @@ def main(args):
# Initialize the config
nzbtomedia.initialize()
# clientAgent for Torrents
clientAgent = nzbtomedia.TORRENT_CLIENTAGENT
logger.postprocess("#########################################################")
logger.postprocess("## ..::[%s]::.. :: STARTING", os.path.splitext(os.path.basename(os.path.normpath(os.path.abspath(__file__))))[0])
logger.postprocess("## ..::[%s]::.. CLIENT:%s ## STARTING", args[0], clientAgent)
logger.postprocess("#########################################################")
# debug command line options
@ -337,8 +305,6 @@ def main(args):
# Post-Processing Result
result = 0
# clientAgent for Torrents
clientAgent = nzbtomedia.CLIENTAGENT
try:
inputDirectory, inputName, inputCategory, inputHash, inputID = parse_args(clientAgent, args)
@ -346,10 +312,14 @@ def main(args):
logger.error("There was a problem loading variables")
return -1
# check if this is a manual run
if inputDirectory is None:
clientAgent = 'manual'
if inputDirectory and inputName and inputHash and inputID:
result = processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent)
else:
# Perform Manual Run
logger.warning("Invalid number of arguments received from client, Switching to manual run mode ...")
# Loop and auto-process
clientAgent = 'manual'
for section, subsection in nzbtomedia.SUBSECTIONS.items():
for category in subsection:
if nzbtomedia.CFG[section][category].isenabled():
@ -362,10 +332,12 @@ def main(args):
logger.error("A problem was reported when trying to manually run %s:%s.", section, category)
else:
logger.warning("%s:%s is DISABLED, you can enable this in autoProcessMedia.cfg ...", section, category)
else:
result = processTorrent(inputDirectory, inputName, inputCategory, inputHash, inputID, clientAgent)
logger.postprocess("All done.")
if result == 0:
logger.postprocess("The %s script completed successfully.", args[0])
else:
logger.error("A problem was reported in the %s script.", args[0])
sys.exit(result)
if __name__ == "__main__":

View file

@ -121,6 +121,14 @@
##### Set to path where download client places completed downloads locally for this category
watch_dir =
[Nzb]
###### clientAgent - Supported clients: sabnzbd, nzbget
clientAgent = sabnzbd
###### SabNZBD (You must edit this if your using nzbToMedia.py with SabNZBD)
sabnzbd_host = localhost
sabnzbd_port = 8080
sabnzbd_apikey =
[Torrent]
###### clientAgent - Supported clients: utorrent, transmission, deluge, rtorrent, other
clientAgent = other

View file

@ -318,8 +318,11 @@ def main(args, section=None):
# Initialize the config
nzbtomedia.initialize(section)
# clientAgent for NZBs
clientAgent = nzbtomedia.NZB_CLIENTAGENT
logger.postprocess("#########################################################")
logger.postprocess("## ..::[%s]::.. :: STARTING", os.path.splitext(os.path.basename(os.path.normpath(os.path.abspath(__file__))))[0])
logger.postprocess("## ..::[%s]::.. CLIENT:%s ## STARTING", args[0], clientAgent)
logger.postprocess("#########################################################")
# debug command line options
@ -329,6 +332,7 @@ def main(args, section=None):
result = 0
status = 0
# 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':
@ -376,7 +380,8 @@ def main(args, section=None):
download_id = os.environ['NZBPR_COUCHPOTATO']
# All checks done, now launching the script.
result = process(os.environ['NZBPP_DIRECTORY'], inputName=os.environ['NZBPP_NZBFILENAME'], status=status, clientAgent = "nzbget", download_id=download_id, inputCategory=os.environ['NZBPP_CATEGORY'])
clientAgent = 'nzbget'
result = process(os.environ['NZBPP_DIRECTORY'], inputName=os.environ['NZBPP_NZBFILENAME'], status=status, clientAgent=clientAgent, download_id=download_id, inputCategory=os.environ['NZBPP_CATEGORY'])
# SABnzbd Pre 0.7.17
elif len(args) == nzbtomedia.SABNZB_NO_OF_ARGUMENTS:
# SABnzbd argv:
@ -387,8 +392,9 @@ def main(args, section=None):
# 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
clientAgent = 'sabnzbd'
logger.postprocess("Script triggered from SABnzbd")
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent = "sabnzbd", download_id='')
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent, download_id='')
# SABnzbd 0.7.17+
elif len(args) >= nzbtomedia.SABNZB_0717_NO_OF_ARGUMENTS:
# SABnzbd argv:
@ -400,20 +406,22 @@ def main(args, section=None):
# 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
# 8 Failure URL
clientAgent = 'sabnzbd'
logger.postprocess("Script triggered from SABnzbd 0.7.17+")
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent = "sabnzbd", download_id='')
result = process(args[1], inputName=args[2], status=args[7], inputCategory=args[5], clientAgent=clientAgent, download_id='')
else:
# Perform Manual Run
logger.warning("Invalid number of arguments received from client, Switching to manual run mode ...")
# Loop and auto-process
clientAgent = 'manual'
for section, subsection in nzbtomedia.SUBSECTIONS.items():
for category in subsection:
if nzbtomedia.CFG[section][category].isenabled():
dirNames = get_dirnames(section, category)
for dirName in dirNames:
logger.postprocess("nzbToMedia running %s:%s as a manual run on folder %s ...", section, category, dirName)
results = process(dirName, os.path.basename(dirName), 0, inputCategory=category)
results = process(dirName, os.path.basename(dirName), 0, clientAgent=clientAgent, inputCategory=category)
if results != 0:
logger.error("A problem was reported when trying to manually run %s:%s.", section, category)
result = results
@ -426,7 +434,7 @@ def main(args, section=None):
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
sys.exit(nzbtomedia.NZBGET_POSTPROCESS_SUCCESS)
else:
logger.error("A problem was reported in the nzbToMedia script.", args[0])
logger.error("A problem was reported in the %s script.", args[0])
if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
sys.exit(nzbtomedia.NZBGET_POSTPROCESS_ERROR)

View file

@ -5,7 +5,7 @@ import sys
import platform
from nzbtomedia import logger, versionCheck
from nzbtomedia.nzbToMediaConfig import config
from nzbtomedia.nzbToMediaUtil import WakeUp
from nzbtomedia.nzbToMediaUtil import WakeUp, makeDir
# sabnzbd constants
SABNZB_NO_OF_ARGUMENTS = 8
@ -55,7 +55,12 @@ GIT_USER = None
GIT_BRANCH = None
GIT_REPO = None
CLIENTAGENT = None
NZB_CLIENTAGENT = None
SABNZBDHOST = None
SABNZBDPORT = None
SABNZBDAPIKEY = None
TORRENT_CLIENTAGENT = None
USELINK = None
OUTPUTDIRECTORY = None
CATEGORIES = []
@ -102,12 +107,12 @@ def initialize(section=None):
NZBTOMEDIA_TIMEOUT, FORKS, FORK_DEFAULT, FORK_FAILED_TORRENT, FORK_FAILED, SICKBEARD_TORRENT, SICKBEARD_FAILED, \
PROGRAM_DIR, CFG, CFG_LOGGING, CONFIG_FILE, CONFIG_MOVIE_FILE, CONFIG_SPEC_FILE, LOG_DIR, NZBTOMEDIA_BRANCH, \
CONFIG_TV_FILE, LOG_FILE, NZBTOMEDIA_VERSION, NEWEST_VERSION, NEWEST_VERSION_STRING, VERSION_NOTIFY, SYS_ARGV, \
SABNZB_NO_OF_ARGUMENTS, SABNZB_0717_NO_OF_ARGUMENTS, CATEGORIES, CLIENTAGENT, USELINK, OUTPUTDIRECTORY, NOFLATTEN, \
SABNZB_NO_OF_ARGUMENTS, SABNZB_0717_NO_OF_ARGUMENTS, CATEGORIES, TORRENT_CLIENTAGENT, USELINK, OUTPUTDIRECTORY, NOFLATTEN, \
UTORRENTPWD, UTORRENTUSR, UTORRENTWEBUI, DELUGEHOST, DELUGEPORT, DELUGEUSR, DELUGEPWD, TRANSMISSIONHOST, TRANSMISSIONPORT, \
TRANSMISSIONPWD, TRANSMISSIONUSR, COMPRESSEDCONTAINER, MEDIACONTAINER, METACONTAINER, MINSAMPLESIZE, SAMPLEIDS, \
SECTIONS, SUBSECTIONS, USER_SCRIPT_CATEGORIES, __INITIALIZED__, AUTO_UPDATE, APP_FILENAME, USER_DELAY, USER_SCRIPT_RUNONCE, \
APP_NAME,USER_SCRIPT_MEDIAEXTENSIONS, USER_SCRIPT, USER_SCRIPT_PARAM, USER_SCRIPT_SUCCESSCODES, USER_SCRIPT_CLEAN, \
TRANSCODE, GIT_PATH, GIT_USER, GIT_BRANCH, GIT_REPO, SYS_ENCODING
TRANSCODE, GIT_PATH, GIT_USER, GIT_BRANCH, GIT_REPO, SYS_ENCODING, NZB_CLIENTAGENT, SABNZBDHOST, SABNZBDPORT, SABNZBDAPIKEY
if __INITIALIZED__:
@ -150,7 +155,7 @@ def initialize(section=None):
print 'or find another way to force Python to use ' + SYS_ENCODING + ' for string encoding.'
sys.exit(1)
if not nzbToMediaUtil.makeDir(LOG_DIR):
if not makeDir(LOG_DIR):
print("!!! No log folder, logging to screen only!")
# init logging
@ -196,7 +201,12 @@ def initialize(section=None):
WakeUp()
CLIENTAGENT = CFG["Torrent"]["clientAgent"] # utorrent | deluge | transmission | rtorrent | other
NZB_CLIENTAGENT = CFG["Nzb"]["clientAgent"] # sabnzbd
SABNZBDHOST = CFG["Nzb"]["sabnzbd_host"]
SABNZBDPORT = int(CFG["Nzb"]["sabnzbd_port"])
SABNZBDAPIKEY = CFG["Nzb"]["sabnzbd_apikey"]
TORRENT_CLIENTAGENT = CFG["Torrent"]["clientAgent"] # utorrent | deluge | transmission | rtorrent | other
USELINK = CFG["Torrent"]["useLink"] # no | hard | sym
OUTPUTDIRECTORY = CFG["Torrent"]["outputDirectory"] # /abs/path/to/complete/
CATEGORIES = (CFG["Torrent"]["categories"]) # music,music_videos,pictures,software
@ -207,12 +217,12 @@ def initialize(section=None):
UTORRENTPWD = CFG["Torrent"]["uTorrentPWD"] # mysecretpwr
TRANSMISSIONHOST = CFG["Torrent"]["TransmissionHost"] # localhost
TRANSMISSIONPORT = CFG["Torrent"]["TransmissionPort"] # 8084
TRANSMISSIONPORT = int(CFG["Torrent"]["TransmissionPort"])
TRANSMISSIONUSR = CFG["Torrent"]["TransmissionUSR"] # mysecretusr
TRANSMISSIONPWD = CFG["Torrent"]["TransmissionPWD"] # mysecretpwr
DELUGEHOST = CFG["Torrent"]["DelugeHost"] # localhost
DELUGEPORT = CFG["Torrent"]["DelugePort"] # 8084
DELUGEPORT = int(CFG["Torrent"]["DelugePort"]) # 8084
DELUGEUSR = CFG["Torrent"]["DelugeUSR"] # mysecretusr
DELUGEPWD = CFG["Torrent"]["DelugePWD"] # mysecretpwr
@ -221,6 +231,7 @@ def initialize(section=None):
METACONTAINER = (CFG["Extensions"]["metaExtensions"]) # .nfo,.sub,.srt
MINSAMPLESIZE = int(CFG["Extensions"]["minSampleSize"]) # 200 (in MB)
SAMPLEIDS = (CFG["Extensions"]["SampleIDs"]) # sample,-s.
TRANSCODE = int(CFG["Transcoder"]["transcode"])
# check for script-defied section and if None set to allow sections
SECTIONS = ("CouchPotato", "SickBeard", "NzbDrone", "HeadPhones", "Mylar", "Gamez")
@ -230,8 +241,6 @@ def initialize(section=None):
SUBSECTIONS = CFG[SECTIONS]
CATEGORIES += SUBSECTIONS.sections
TRANSCODE = int(CFG["Transcoder"]["transcode"])
USER_SCRIPT_CATEGORIES = CFG["UserScript"]["user_script_categories"]
if not "NONE" in USER_SCRIPT_CATEGORIES:
USER_SCRIPT_MEDIAEXTENSIONS = (CFG["UserScript"]["user_script_mediaExtensions"])

View file

@ -2,7 +2,7 @@ import os
import sys
import nzbtomedia
from subprocess import call, Popen
from nzbtomedia.nzbToMediaUtil import create_destination
from nzbtomedia.nzbToMediaUtil import makeDir
from nzbtomedia import logger
# which() and os_platform() breaks when running in Transmission (has to do with os.environ)
@ -91,7 +91,7 @@ def extract(filePath, outputDestination):
return False
# Create outputDestination folder
create_destination(outputDestination)
makeDir(outputDestination)
passwordsfile = nzbtomedia.CFG["passwords"]["PassWordFile"]
if passwordsfile != "" and os.path.isfile(os.path.normpath(passwordsfile)):

View file

@ -14,31 +14,10 @@ from nzbtomedia.synchronousdeluge.client import DelugeClient
from nzbtomedia.utorrent.client import UTorrentClient
from nzbtomedia.transmissionrpc.client import Client as TransmissionClient
def getDirectorySize(directory):
dir_size = 0
for (path, dirs, files) in os.walk(directory):
for file in files:
filename = os.path.join(path, file)
dir_size += os.path.getsize(filename)
dir_size = dir_size / (1024.0 * 1024.0 * 1024.0) # convert to GiB
return dir_size
def safeName(name):
safename = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "", name) #make this name safe for use in directories for windows etc.
return safename
def create_destination(outputDestination):
if os.path.exists(outputDestination):
return
try:
logger.info("CREATE DESTINATION: Creating destination folder: %s", outputDestination)
os.makedirs(outputDestination)
except:
logger.error("CREATE DESTINATION: Not possible to create destination folder. Exiting")
sys.exit(-1)
def makeDir(path):
if not os.path.isdir(path):
try:
@ -132,7 +111,7 @@ def copy_link(filePath, targetDirectory, useLink, outputDestination):
logger.info("COPYLINK: target file already exists. Nothing to be done")
return True
create_destination(outputDestination)
makeDir(outputDestination)
if useLink == "hard":
try:
logger.info("COPYLINK: Hard linking %s to %s", filePath, targetDirectory)
@ -167,7 +146,6 @@ def copy_link(filePath, targetDirectory, useLink, outputDestination):
shutil.copy(filePath, targetDirectory)
return True
def flatten(outputDestination):
logger.info("FLATTEN: Flattening directory: %s", outputDestination)
for dirpath, dirnames, filenames in os.walk(outputDestination): # Flatten out the directory to make postprocessing easier
@ -182,7 +160,6 @@ def flatten(outputDestination):
logger.error("FLATTEN: Could not flatten %s", source)
removeEmptyFolders(outputDestination) # Cleanup empty directories
def removeEmptyFolders(path):
logger.info("REMOVER: Removing empty folders in: %s", path)
if not os.path.isdir(path):
@ -221,7 +198,6 @@ def iterate_media_files(dirname):
continue
yield dirpath, os.path.join(dirpath, filename)
#Wake function
def WakeOnLan(ethernet_address):
@ -253,10 +229,6 @@ def TestCon(host, port):
return "Down"
def WakeUp():
if not nzbtomedia.CFG:
logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
return
wake = int(nzbtomedia.CFG["WakeOnLan"]["wake"])
if wake == 0: # just return if we don't need to wake anything.
return
@ -278,10 +250,6 @@ def WakeUp():
logger.info("System with mac: %s has been woken. Continuing with the rest of the script.", mac)
def convert_to_ascii(nzbName, dirName):
if not nzbtomedia.CFG:
logger.error("You need an autoProcessMedia.cfg file - did you rename and edit the .sample?")
return nzbName, dirName
ascii_convert = int(nzbtomedia.CFG["ASCII"]["convert"])
if ascii_convert == 0 or os.name == 'nt': # just return if we don't want to convert or on windows os and "\" is replaced!.
return nzbName, dirName
@ -302,7 +270,6 @@ def convert_to_ascii(nzbName, dirName):
return nzbName, dirName
def parse_other(args):
return os.path.normpath(args[1]), '', '', '', ''
def parse_rtorrent(args):
@ -446,8 +413,7 @@ def cleanup_directories(inputCategory, processCategories, result, directory):
def create_torrent_class(clientAgent):
# Hardlink solution for Torrents
TorrentClass = ""
if clientAgent in ['utorrent', 'transmission', 'deluge']:
TorrentClass = None
if clientAgent == 'utorrent':
try:
logger.debug("Connecting to %s: %s", clientAgent, nzbtomedia.UTORRENTWEBUI)
@ -471,6 +437,62 @@ def create_torrent_class(clientAgent):
TorrentClass = DelugeClient()
TorrentClass.connect(host =nzbtomedia.DELUGEHOST, port =nzbtomedia.DELUGEPORT, username =nzbtomedia.DELUGEUSR, password =nzbtomedia.DELUGEPWD)
except:
logger.error("Failed to connect to deluge")
logger.error("Failed to connect to Deluge")
return TorrentClass
def pause_torrent(clientAgent, TorrentClass, inputHash, inputID, inputName):
# if we are using links with Torrents it means we need to pause it in order to access the files
logger.debug("Stoping torrent %s in %s while processing", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.stop(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.stop_torrent(inputID)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.pause_torrent([inputID])
time.sleep(5) # Give Torrent client some time to catch up with the change
def resume_torrent(clientAgent, TorrentClass, inputHash, inputID, result, inputName):
# Hardlink solution for uTorrent, need to implent support for deluge, transmission
if clientAgent in ['utorrent', 'transmission', 'deluge'] and inputHash:
# Delete torrent and torrentdata from Torrent client if processing was successful.
if (int(nzbtomedia.CFG["Torrent"]["deleteOriginal"]) is 1 and result != 1) or nzbtomedia.USELINK == 'move': # if we move files, nothing to resume seeding.
logger.debug("Deleting torrent %s from %s", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.removedata(inputHash)
TorrentClass.remove(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.remove_torrent(inputID, True)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.remove_torrent(inputID, True)
# we always want to resume seeding, for now manually find out what is wrong when extraction fails
else:
logger.debug("Starting torrent %s in %s", inputName, clientAgent)
if clientAgent == 'utorrent' and TorrentClass != "":
TorrentClass.start(inputHash)
if clientAgent == 'transmission' and TorrentClass !="":
TorrentClass.start_torrent(inputID)
if clientAgent == 'deluge' and TorrentClass != "":
TorrentClass.core.resume_torrent([inputID])
time.sleep(5)
def find_download(clientAgent, nzbName, download_id):
tc = create_torrent_class(clientAgent)
logger.debug("Searching for Download on %s ...", clientAgent)
if clientAgent == 'utorrent':
torrents = tc.list()[1]['torrents']
if torrents:
for torrent in torrents:
if nzbName in torrent and download_id in torrent:
return True
if clientAgent == 'transmission':
torrent = tc.get_torrent(download_id)
if torrent:
name = torrent.name
if name == nzbName:
return True
if clientAgent == 'deluge':
pass
if clientAgent == 'sabnzbd':
pass

File diff suppressed because one or more lines are too long