Skip to content

Instantly share code, notes, and snippets.

@outlyer
Created May 28, 2016 17:16
Show Gist options
  • Save outlyer/3547a5ffd404cc8204f7b6de47da0fe5 to your computer and use it in GitHub Desktop.
Save outlyer/3547a5ffd404cc8204f7b6de47da0fe5 to your computer and use it in GitHub Desktop.
Pushover Post Script for NZBGet
#!/usr/bin/env python
##############################################################################
### NZBGET POST-PROCESSING SCRIPT ###
# Send Pushover notification.
#
# This script sends a Pushover notification when the job is finished.
#
# NOTE: This script requires Python to be installed on your system.
##############################################################################
### OPTIONS ###
# Pushover user key
#UserKey=
# Application token/key (register application here: http://pushover.net/apps)
#AppToken=
# Device to send notification to (optional)
#Device=
# Supplementary URL to show with your message (optional)
#Url=
# Success Sound (optional)
#
# Sound to play on success. Lists of sounds here: http://pushover.net/api#sounds.
#SuccessSound=
# Failure Sound (optional)
#
# Sound to play on failure. Lists of sounds here: http://pushover.net/api#sounds.
#FailureSound=
# Priority of success notification (low, normal, high).
#
# Low priority (quiet notification)
#
# Normal priority
#
# High priority (bypasses users quiet times)
#SuccessPriority=normal
# Priority of failure notification (low, normal, high).
#
# Low priority (quiet notification)
#
# Normal priority
#
# High priority (bypasses users quiet times)
#FailurePriority=normal
# Append Par-Status and Unpack-Status to the message (yes, no).
#
# Add the Par and Unpack status.
#AppendParUnpack=no
# Append list of files to the message (yes, no).
#
# Add the list of downloaded files (the content of destination directory).
#FileList=no
### NZBGET POST-PROCESSING SCRIPT ###
##############################################################################
import os
import sys
import httplib
import urllib
# Exit codes used by NZBGet
POSTPROCESS_SUCCESS=93
POSTPROCESS_ERROR=94
# Check if the script is called from nzbget 11.0 or later
if not os.environ.has_key('NZBOP_SCRIPTDIR'):
print('*** NZBGet post-processing script ***')
print('This script is supposed to be called from nzbget (11.0 or later).')
sys.exit(POSTPROCESS_ERROR)
required_options = ('NZBPO_USERKEY', 'NZBPO_APPTOKEN')
for optname in required_options:
if (not os.environ.has_key(optname)):
print('[ERROR] Option %s is missing in configuration file. Please check script settings' % optname[6:])
sys.exit(POSTPROCESS_ERROR)
print('[DETAIL] Script successfully started')
sys.stdout.flush()
userkey = os.environ['NZBPO_USERKEY']
apptoken = os.environ['NZBPO_APPTOKEN']
device = os.environ['NZBPO_DEVICE']
url = os.environ['NZBPO_URL']
# Check par and unpack status for errors and set message
# NZBPP_PARSTATUS - result of par-check:
# 0 = not checked: par-check is disabled or nzb-file does
# not contain any par-files;
# 1 = checked and failed to repair;
# 2 = checked and successfully repaired;
# 3 = checked and can be repaired but repair is disabled.
# 4 = par-check needed but skipped (option ParCheck=manual);
# NZBPP_UNPACKSTATUS - result of unpack:
# 0 = unpack is disabled or was skipped due to nzb-file
# properties or due to errors during par-check;
# 1 = unpack failed;
# 2 = unpack successful.
success=False
if os.environ['NZBPP_PARSTATUS'] == '1' or os.environ['NZBPP_UNPACKSTATUS'] == '1':
message = 'Download of "%s" has failed.' % (os.environ['NZBPP_NZBNAME'])
elif os.environ['NZBPP_PARSTATUS'] == '4':
message = 'Download of "%s" requires par-repair.' % (os.environ['NZBPP_NZBNAME'])
else:
message = 'Download of "%s" has successfully completed.' % (os.environ['NZBPP_NZBNAME'])
success=True
#Set requested success or failure sound
if not success and 'NZBPO_FAILURESOUND' in os.environ:
sound = os.environ['NZBPO_FAILURESOUND']
elif success and 'NZBPO_SUCCESSSOUND' in os.environ:
sound = os.environ['NZBPO_SUCCESSSOUND']
else:
sound=""
#Set success priority
if os.environ['NZBPO_SUCCESSPRIORITY'] == 'low':
successpriority = "-1"
elif os.environ['NZBPO_SUCCESSPRIORITY'] == 'normal':
successpriority = "0"
elif os.environ['NZBPO_SUCCESSPRIORITY'] == 'high':
successpriority = "1"
#set failure priority
if os.environ['NZBPO_FAILUREPRIORITY'] == 'low':
failurepriority = "-1"
elif os.environ['NZBPO_FAILUREPRIORITY'] == 'normal':
failurepriority = "0"
elif os.environ['NZBPO_FAILUREPRIORITY'] == 'high':
failurepriority = "1"
#set priority to success or failure priority
if success:
priority = successpriority
else:
priority = failurepriority
# add par and unpack status to the message
if os.environ['NZBPO_APPENDPARUNPACK'] == 'yes':
parStatus = { '0': 'skipped', '1': 'failed', '2': 'repaired', '3': 'repairable', '4': 'manual' }
message += '\nPar-Status: %s' % parStatus[os.environ['NZBPP_PARSTATUS']]
unpackStatus = { '0': 'skipped', '1': 'failed', '2': 'success' }
message += '\nUnpack-Status: %s' % unpackStatus[os.environ['NZBPP_UNPACKSTATUS']]
# add list of downloaded files to the message
if os.environ['NZBPO_FILELIST'] == 'yes':
message += '\n\nFiles:'
for dirname, dirnames, filenames in os.walk(os.environ['NZBPP_DIRECTORY']):
for filename in filenames:
message += '\n' + os.path.join(dirname, filename)[len(os.environ['NZBPP_DIRECTORY']) + 1:]
# Send message
print('[DETAIL] Sending Pushover notification')
sys.stdout.flush()
try:
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": apptoken,
"user": userkey,
"device": device,
"url": url,
"sound": sound,
"priority": priority,
"message": message,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
except Exception, err:
print('[ERROR] %s' % err)
sys.exit(POSTPROCESS_ERROR)
# All OK, returning exit status 'POSTPROCESS_SUCCESS' (int <93>) to let NZBGet know
# that our script has successfully completed.
sys.exit(POSTPROCESS_SUCCESS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment