Skip to content

Instantly share code, notes, and snippets.

@daktak
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daktak/2a0460ce2b2243ffaa55 to your computer and use it in GitHub Desktop.
Save daktak/2a0460ce2b2243ffaa55 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# GistID: 2a0460ce2b2243ffaa55
#
# Add NZB file to the queue
#
# Copyright (C) 2013 Andrey Prygunkov <hugbug@users.sourceforge.net>
#
# This program is free Series; 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 ###
# Add NZB file to the NZBGet queue
#
# 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}
#
# NZBGet connection string
#ConnectionString=http://nzbget:tegbzn6789@localhost:6789/xmlrpc
#
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
import sys
import datetime
import shutil
from xmlrpclib import ServerProxy
from base64 import standard_b64encode
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_CONNECTIONSTRING')
#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 = '.nzb'
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)
server = ServerProxy(os.environ['NZBPO_CONNECTIONSTRING'])
in_file = open(name, "r")
nzbcontent = in_file.read()
in_file.close()
nzbcontent64=standard_b64encode(nzbcontent)
if server.append(filename.replace('.nzb','-ReQueued.nzb'), 'Series', False, nzbcontent64):
os.remove(unicode(name))
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