From 08c955130220896488bc8088b12e633d5268b88e Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Sun, 19 Feb 2017 15:47:54 -0500 Subject: [PATCH] documentation updated and --remote switch fully working now; refs #3 --- DirWatch.py | 91 +++++++++++++++++++++++++---------------------- README.md | 100 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 131 insertions(+), 60 deletions(-) diff --git a/DirWatch.py b/DirWatch.py index ee87cd0..8240534 100755 --- a/DirWatch.py +++ b/DirWatch.py @@ -196,11 +196,7 @@ def _handle(self, source_path, target_dir): A Simple wrapper to handle content in addition to logging it. """ - if not isfile(source_path): - self.logger.warning( - "The source file '%s' was not found (for handling)." % \ - source_path, - ) + if not target_dir: return False if not isdir(target_dir): @@ -210,6 +206,13 @@ def _handle(self, source_path, target_dir): ) return False + if not isfile(source_path): + self.logger.warning( + "The source file '%s' was not found (for handling)." % \ + source_path, + ) + return False + self.logger.info('Scanning Source: %s' % source_path) # Generate the new filename @@ -259,15 +262,16 @@ def watch_library(self, sources, target_dir, *args, **kwargs): and move found entries to the target directory """ - # Target Directory - target_dir = abspath(expanduser(target_dir)) - if not isdir(target_dir): - # We're done if the target path isn't a directory - self.logger.error( - 'Target directory %s was not found.' % target_dir) - return False + if target_dir is not None: + # Target Directory exists (we're not doing remote pushes) + target_dir = abspath(expanduser(target_dir)) + if not isdir(target_dir): + # We're done if the target path isn't a directory + self.logger.error( + 'Target directory %s was not found.' % target_dir) + return False - self.logger.info('Target directory set to: %s' % target_dir) + self.logger.info('Target directory set to: %s' % target_dir) # Create a reference time ref_time = datetime.now() - timedelta(seconds=self.min_age) @@ -489,15 +493,20 @@ def watch(self): # Store our source paths source_paths = self.parse_path_list(self.get('WatchPaths')) - # Store target directory - target_path = tidy_path(self.get('NzbDir')) + # Get our Mode self.mode = self.get('Mode', DIRWATCH_MODE_DEFAULT) - if not isdir(target_path): - self.logger.error( - "The target directory '%s' was not found." % \ - target_path, - ) - return False + + if self.mode != DIRWATCH_MODE.REMOTE: + # Store target directory + target_path = tidy_path(self.get('NzbDir')) + if not isdir(target_path): + self.logger.error( + "The target directory '%s' was not found." % \ + target_path, + ) + return False + else: + target_path = None return self.watch_library( source_paths, @@ -564,14 +573,15 @@ def main(self, *args, **kwargs): from optparse import OptionParser # Support running from the command line - usage = "Usage: %prog [options] -t TargetDir [SrcDir1 [SrcDir2 [...]]]" + usage = "Usage: %prog [options] [SrcDir1 [SrcDir2 [...]]]" parser = OptionParser(usage=usage) parser.add_option( "-t", "--target-dir", dest="target_dir", help="The directory you want to move found NZB-Files from the " +\ - "identified source directories to.", + "identified source directories to. This option is required " +\ + "if not using the --remote (-r) switch.", metavar="DIR", ) parser.add_option( @@ -638,8 +648,7 @@ def main(self, *args, **kwargs): dest="remote", help="Perform a remote push to NZBGet. This allows you to scan " "directories for NZB-Files on different machines and still remotely " - "push them to your central NZBGet server. This option requires that " - "you additionally specify the --api-url (-u) switch to work.", + "push them to your central NZBGet server.", ) parser.add_option( "-D", @@ -744,12 +753,19 @@ def main(self, *args, **kwargs): # Toggle Remote Mode script.set('Mode', DIRWATCH_MODE.REMOTE) - if not _api_url: - script.logger.error( - 'You must specify an API URL (--api-url / -u) when using ' - 'the --remote (-r) switch.' - ) - exit(EXIT_CODE.FAILURE) + # Ensure NzbDir is set + script.set('NzbDir', '') + + if not _remote and not script.get('NzbDir') and _target_dir: + if not (_preview or _watch_paths): + script.set('Mode', DIRWATCH_MODE_DEFAULT) + + if script.get('WatchPaths') is None: + # Allow this flag to exist + script.set('WatchPaths', '') + + # Finally set the directory the user specified for scanning + script.set('NzbDir', _target_dir) if _max_archive_size: try: @@ -773,18 +789,7 @@ def main(self, *args, **kwargs): ) exit(EXIT_CODE.FAILURE) - if not script.get('NzbDir') and _target_dir: - if not (_preview or _watch_paths): - script.set('Mode', DIRWATCH_MODE_DEFAULT) - - if script.get('WatchPaths') is None: - # Allow this flag to exist - script.set('WatchPaths', '') - - # Finally set the directory the user specified for scanning - script.set('NzbDir', _target_dir) - - if not script.script_mode and not script.get('NzbDir'): + if not script.script_mode and not script.get('WatchPaths'): # Provide some CLI help when NzbDir has been # detected as not being identified parser.print_help() diff --git a/README.md b/README.md index 1a24367..ef13b87 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,32 @@ scan a folder in their home directory (or on a network path) instead. In short: This script allows you to process NZB-Get files that appear in multiple directories instead of just the one. +Remote NZB-File Loading +======================= +Where this script gets a bit more powerful is that you can additionally scan NZB-Files +from another server remotely on your network. All found NZB-Files will be automatically +pushed directly to your NZB-Get server. + +Directory to Category Assignments +================================= +You can additionally tell this script to associate NZB-File filled directories with +a specific category. This can allow you to manage mulitple directories and assign +the NZB-Files found within a specific category when being loaded into NZBGet. + +This is done by simply adding the ?c=category.name to each directory you specify. +For example, in NZBGet you may want to have a structure as follows: +''' +/ + /nzbroot/Movies/ + /nzbroot/TVShows/ + /nzbroot/MyEBooks/ +''' + +You you just add this the NZBGet (DirWatch) Script's configuration: +''' +/nzbroot/Movies?c=movie, /nzbroot/TVShows?c=tv, /nzbroot/MyEBooks?c=ebooks +''' + How It Works ============ Whatever additional path you specify, the script will just move the detected NZB-Files @@ -70,12 +96,14 @@ the fetching of subtitles. Here are the switches available to you: ``` -Usage: DirWatch.py [options] -t TargetDir [scandir1 [scandir2 [...]]] +Usage: DirWatch.py [options] [SrcDir1 [SrcDir2 [...]]] +Options: -h, --help show this help message and exit -t DIR, --target-dir=DIR The directory you want to move found NZB-Files from - the identified source directories to. + the identified source directories to. This option is + required if not using the --remote (-r) switch. -a AGE_IN_SEC, --min-age=AGE_IN_SEC Specify the minimum age a NZB-File must be before considering it for processing. This value is @@ -84,30 +112,41 @@ Usage: DirWatch.py [options] -t TargetDir [scandir1 [scandir2 [...]]] racing condition where an NZB-File is still being written to disk at the same time as we're trying to process it. + -s SIZE_IN_KB, --max-archive-size=SIZE_IN_KB + Specify the maximum size a detected compressed file + can be before ignoring it. If the found compressed + file is within this specified value, it's contents + will be scanned to see if it (only) contains NZB- + Files. These types of files would qualify to be moved + as well. Set this value to Zero (0) to not process + compressed files. The value is interpreted in + Kilobytes and has a default value of 150if not + otherwise specified. -p, --preview This is like a test switch; the actions the script would have otherwise performed are instead just printed to the screen. -L FILE, --logfile=FILE Send output to the specified logfile instead of stdout. + -u API_URL, --api-url=API_URL + Specify the URL of the NZB-Get API server such as: + nzbget://user:pass@control.nzbget.host (to access + insecure port 6789), + nzbgets://user:pass@control.nzbget.host (to access + secure port 6791), + nzbget://user:pass@control.nzbget.host:port (to + specify your own insecure port), and + nzbgets://user:pass@control.nzbget.host:port (to + specify your own secure port). By default + nzbget://127.0.0.1 is used. + -r, --remote-push Perform a remote push to NZBGet. This allows you to + scan directories for NZB-Files on different machines + and still remotely push them to your central NZBGet + server. -D, --debug Debug Mode ``` -Here is a simple example: -```bash -# Scan your library for NZB-Files and print to the sreen what -# you plan on doing (observe the -p switch): -python DirWatch.py -p -t /path/to/NZBGet/NzbDir - /path/to/location/with/NZB-Files - -# Happy with the plan of action? Just drop the -p switch and the -# matched NZB-Files will be moved: -python DirWatch.py -t /path/to/NZBGet/NzbDir - /path/to/location/with/NZB-Files - -``` - You can scan as many directories as you want to type inline on the shell: ```bash # Scan your libraries for NZB-Files and move them @@ -118,7 +157,7 @@ python DirWatch.py -t /path/to/NZBGet/NzbDir \ /home/trevor/Downloads \ /home/joe/Dropbox/NZBFiles \ /home/jason/Dropbox/NZBFiles \ - /home/trevor/Dropbox/NZBFiles \ + /home/trevor/Dropbox/NZBFiles ``` Don't forget that if you're using the CLI, you can take advantage of wildcards @@ -136,3 +175,30 @@ to frequently move matched NZB-Files found in the specified Source Directories # Scan every 2 minutes: */2 * * * * /path/to/DirWatch.py -t /path/to/NZBGet/NzbDir ~/DropBox ``` + +Consider different servers on your network each obtaining NZB-Files in different +locations, but you only have 1 NZBGet instance. Perhaps the NZBGet instance +is even on a different network. No problem; you can scan multiple locations for +NZB-Files and then push them remotely to your NZBGet server: +```bash +# Scan your libraries for NZB-Files and move them +# into our remote location done by the --remote (-r) switch +python DirWatch.py -r -u nzbget://my.nzbget.host \ + /path/to/nzb-files \ + /another/path/to/nzb-files +``` + +You can also use the category switches with the command line to force category +assignments per directory: +```bash +# Assign categories per directory (optionally) if you wish: +python DirWatch.py -t /path/to/NZBGet/NzbDir \ + /home/joe/Downloads/NZBFiles/Movies?c=movie \ + /home/joe/Downloads/NZBFiles/Shows?c=tv + /home/joe/Downloads/NZBFiles/General + +# You can do this using remote calls too: +python DirWatch.py -r -u nzbget://my.nzbget.host \ + /home/joe/Downloads/NZBFiles/Movies?c=movie \ + /home/joe/Downloads/NZBFiles/Shows?c=tv +```