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

@ -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,31 +413,86 @@ def cleanup_directories(inputCategory, processCategories, result, directory):
def create_torrent_class(clientAgent):
# Hardlink solution for Torrents
TorrentClass = ""
if clientAgent in ['utorrent', 'transmission', 'deluge']:
if clientAgent == 'utorrent':
try:
logger.debug("Connecting to %s: %s", clientAgent, nzbtomedia.UTORRENTWEBUI)
TorrentClass = UTorrentClient(nzbtomedia.UTORRENTWEBUI, nzbtomedia.UTORRENTUSR, nzbtomedia.UTORRENTPWD)
except:
logger.error("Failed to connect to uTorrent")
TorrentClass = None
if clientAgent == 'utorrent':
try:
logger.debug("Connecting to %s: %s", clientAgent, nzbtomedia.UTORRENTWEBUI)
TorrentClass = UTorrentClient(nzbtomedia.UTORRENTWEBUI, nzbtomedia.UTORRENTUSR, nzbtomedia.UTORRENTPWD)
except:
logger.error("Failed to connect to uTorrent")
if clientAgent == 'transmission':
try:
logger.debug("Connecting to %s: http://%s:%s", clientAgent, nzbtomedia.TRANSMISSIONHOST,
nzbtomedia.TRANSMISSIONPORT)
TorrentClass = TransmissionClient(nzbtomedia.TRANSMISSIONHOST, nzbtomedia.TRANSMISSIONPORT, nzbtomedia.TRANSMISSIONUSR,
nzbtomedia.TRANSMISSIONPWD)
except:
logger.error("Failed to connect to Transmission")
if clientAgent == 'transmission':
try:
logger.debug("Connecting to %s: http://%s:%s", clientAgent, nzbtomedia.TRANSMISSIONHOST,
nzbtomedia.TRANSMISSIONPORT)
TorrentClass = TransmissionClient(nzbtomedia.TRANSMISSIONHOST, nzbtomedia.TRANSMISSIONPORT, nzbtomedia.TRANSMISSIONUSR,
nzbtomedia.TRANSMISSIONPWD)
except:
logger.error("Failed to connect to Transmission")
if clientAgent == 'deluge':
try:
logger.debug("Connecting to %s: http://%s:%s", clientAgent, nzbtomedia.DELUGEHOST,
nzbtomedia.DELUGEPORT)
TorrentClass = DelugeClient()
TorrentClass.connect(host =nzbtomedia.DELUGEHOST, port =nzbtomedia.DELUGEPORT, username =nzbtomedia.DELUGEUSR, password =nzbtomedia.DELUGEPWD)
except:
logger.error("Failed to connect to deluge")
if clientAgent == 'deluge':
try:
logger.debug("Connecting to %s: http://%s:%s", clientAgent, nzbtomedia.DELUGEHOST,
nzbtomedia.DELUGEPORT)
TorrentClass = DelugeClient()
TorrentClass.connect(host =nzbtomedia.DELUGEHOST, port =nzbtomedia.DELUGEPORT, username =nzbtomedia.DELUGEUSR, password =nzbtomedia.DELUGEPWD)
except:
logger.error("Failed to connect to Deluge")
return TorrentClass
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