From f0bf5fb0cbedf042ad6e64f7b1614bebabb48559 Mon Sep 17 00:00:00 2001 From: Berkona Date: Mon, 25 Feb 2013 15:43:17 -0500 Subject: [PATCH] Added better parsing strategy --- TorrentToMedia.py | 34 ++++----------------------- autoProcessMedia.cfg.sample | 3 +++ nzbToMediaUtil.py | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/TorrentToMedia.py b/TorrentToMedia.py index 989d8695..379e6e7f 100755 --- a/TorrentToMedia.py +++ b/TorrentToMedia.py @@ -269,37 +269,11 @@ if not os.path.isfile(configFilename): Logger.info("MAIN: Loading config from %s", configFilename) config.read(configFilename) -if len(sys.argv) == 2: #for other clients we assume we must at least get the directory. - # We will assume this to be the passin from deluge. torrent id, torrent name, torrent save path. - inputDirectory = os.path.normpath(sys.argv[1]) - inputName = '' # We dont have a name yet - inputCategory = '' # We dont have a category yet +clientAgent = config.get("Torrent", "clientAgent") -elif len(sys.argv) > 4 and sys.argv[1] == 'utorrent': #distinguish utorrent from others like deluge. - # We will pass in 'utorrent' '%D', '%N', and '%L' (if it exists), from uTorrent - # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< - inputDirectory = os.path.normpath(sys.argv[2]) - inputName = sys.argv[3] - try: #assume we have a label. - inputCategory = sys.argv[4] # We dont have a category yet - except: - inputCategory = '' # We dont have a category yet - inputHash = sys.argv[5] - -elif len(sys.argv) == 4: - # We will assume this to be the passin from deluge. torrent id, torrent name, torrent save path. - inputDirectory = os.path.normpath(sys.argv[3]) - inputName = sys.argv[2] - inputCategory = '' # We dont have a category yet - -elif os.getenv('TR_TORRENT_DIR'): - # We will pass in %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission - # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< - inputDirectory = os.path.normpath(os.getenv('TR_TORRENT_DIR')) - inputName = os.getenv('TR_TORRENT_NAME') - inputCategory = '' # We dont have a category yet - -else: +try: + parse_args(clientAgent) +except: Logger.error("MAIN: There was a problem loading variables: Exiting") sys.exit(-1) diff --git a/autoProcessMedia.cfg.sample b/autoProcessMedia.cfg.sample index af847ca3..de03a158 100644 --- a/autoProcessMedia.cfg.sample +++ b/autoProcessMedia.cfg.sample @@ -28,6 +28,9 @@ watch_dir= failed_fork=0 [Torrent] +# Set to whatever torrent client you use. +# Supported values: utorrent, transmission, deluge, other +clientAgent = other useLink = 0 extractionTool = C:\Program Files\7-Zip\7z.exe categories = music,music_videos,pictures,software diff --git a/nzbToMediaUtil.py b/nzbToMediaUtil.py index 7a651284..fe5d1bc0 100644 --- a/nzbToMediaUtil.py +++ b/nzbToMediaUtil.py @@ -1,6 +1,7 @@ import logging import logging.config import os.path +import sys def nzbtomedia_configure_logging(dirname): @@ -10,3 +11,48 @@ def nzbtomedia_configure_logging(dirname): fileHandler.formatter = logging.Formatter('%(asctime)s|%(levelname)-7.7s %(message)s', '%H:%M:%S') fileHandler.level = logging.DEBUG logging.getLogger().addHandler(fileHandler) + + +def parse_other(args): + return os.path.normpath(sys.argv[1]), '', '' + + +def parse_utorrent(args): + # We will pass in 'utorrent' '%D', '%N', and '%L' (if it exists), from uTorrent + # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< + inputDirectory = os.path.normpath(sys.argv[2]) + inputName = sys.argv[3] + try: #assume we have a label. + inputCategory = sys.argv[4] # We dont have a category yet + except: + inputCategory = '' # We dont have a category yet + + +def parse_deluge(args): + # We will assume this to be the passin from deluge. torrent id, torrent name, torrent save path. + inputDirectory = os.path.normpath(sys.argv[3]) + inputName = sys.argv[2] + inputCategory = '' # We dont have a category yet + + +def parse_transmission(args): + # We will pass in %TR_TORRENT_DIR% %TR_TORRENT_NAME% from Transmission + # In short pass "/path/to/downloaded/torrent/ name" to TorrentToMedia.py, eg >>>> TorrentToMedia.py /Downloaded/MovieName.2013.BluRay.1080p.x264-10bit.DTS MovieName.2013.BluRay.1080p.x264-10bit.DTS <<<< + inputDirectory = os.path.normpath(os.getenv('TR_TORRENT_DIR')) + inputName = os.getenv('TR_TORRENT_NAME') + inputCategory = '' # We dont have a category yet + + +__ARG_PARSERS__ = { + 'other': parse_other, + 'utorrent': parse_utorrent, + 'deluge': parse_deluge, + 'transmission': parse_transmission, +} + + +def parse_args(clientAgent): + parseFunc = __ARG_PARSERS__.get(clientAgent, None) + if not parseFunc: + raise RuntimeError("Could not find client-agent") + parseFunc(sys.argv)