Skip to content

Instantly share code, notes, and snippets.

@daktak
Last active February 3, 2018 01:06
Show Gist options
  • Save daktak/581f5a91899f5662a4a8 to your computer and use it in GitHub Desktop.
Save daktak/581f5a91899f5662a4a8 to your computer and use it in GitHub Desktop.
NZBGet script to move nzb files into watch dir
#!/usr/bin/env python2
# GistID: 581f5a91899f5662a4a8
#
# Move files post-processing script for NZBGet
#
# Copyright (C) 2013 Andrey Prygunkov <hugbug@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Revision: 660 $
# $Date: 2013-05-02 22:40:36 +0200 (Do, 02 Mai 2013) $
#
##############################################################################
### NZBGET POST-PROCESSING SCRIPT ###
# Move files into specified directory
#
# This script moves a file type to a specified directoiry
# Can be used to move nzb files to NZBGets watch dir ${NzbDir}
#
# NOTE: This script requires Python to be installed on your system.
##############################################################################
### OPTIONS ###
# Directory to move to
#Directory=${NzbDir}
# Extensions of files to be send.
#
# Only files with these extensions are processed. Extensions must
# be separated with commas.
# Example=.nzb,.txt
#Extensions=.nzb
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
import sys
import datetime
import shutil
try:
from xmlrpclib import ServerProxy # python 2
except ImportError:
from xmlrpc.client import ServerProxy # python 3
# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
POSTPROCESS_NONE=95
# Check if the script is called from nzbget 11.0 or later
if not 'NZBOP_SCRIPTDIR' in os.environ:
print('*** NZBGet post-processing script ***')
print('This script is supposed to be called from nzbget (11.0 or later).')
sys.exit(POSTPROCESS_ERROR)
if not os.path.exists(os.environ['NZBPP_DIRECTORY']):
print('Destination directory doesn\'t exist, exiting')
sys.exit(POSTPROCESS_NONE)
# Check par and unpack status for errors
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_PARSTATUS'] == '4' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
print('[WARNING] Download of "%s" has failed, exiting' % (os.environ['NZBPP_NZBNAME']))
sys.exit(POSTPROCESS_NONE)
required_options = ('NZBPO_DIRECTORY', 'NZBPO_EXTENSIONS')
for optname in required_options:
if (not optname in os.environ):
print('[ERROR] Option %s is missing in configuration file. Please check script settings' % optname[6:])
sys.exit(POSTPROCESS_ERROR)
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)
def movefile(filename, newname):
# Send message
print('[INFO] Moving %s' % os.path.basename(filename))
sys.stdout.flush()
try:
os.rename(os.path.basename(filename), os.path.basename(newname))
except Exception as err:
print('[ERROR] %s' % err)
sys.exit(POSTPROCESS_ERROR)
# search files
Extensions = os.environ['NZBPO_EXTENSIONS']
Extensions = Extensions.split(',')
cnt = 0
for dirname, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
for filename in filenames:
extension = os.path.splitext(filename)[1]
if extension.lower() in Extensions:
name = os.path.join(dirname, filename)
newname = os.path.join(os.environ['NZBPO_DIRECTORY'], filename.replace('.nzb','_1.nzb'))
shutil.move(unicode(name), unicode(newname))
cnt += 1
if cnt > 0:
directory = os.path.normpath(os.environ['NZBPP_DIRECTORY'])
removeEmptyFolders(directory)
print('Moved %i file(s)' % cnt)
sys.exit(POSTPROCESS_SUCCESS)
else:
print('No suitable files found')
sys.exit(POSTPROCESS_NONE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment