Page 1 of 1

postprocess - NotifyMyAndroid

Posted: 06 May 2013, 03:29
by theparnic
This script sends a notification to your NotifyMyAndroid account.

Code: Select all

#!/usr/bin/env python

##############################################################################
### NZBGET POST-PROCESSING SCRIPT                                          ###

# Notify a user's NotifyMyAndroid account of NZBGet events
#
# NOTE: This script requires Python to be installed on your system.

##############################################################################
### OPTIONS                                                       ###

# Your NotifyMyAndroid API key.
#
# Use commas to separate multiple keys if you want to send multiple notifications. E.g.: 12345,67890
#APIKey=

# The name of the application we should report ourselves as.
#ApplicationName=NZBGet NotifyMyAndroid PPScript

# What event is being reported.
#EventName=Download Complete

# Further description of what was downloaded.
#
# Available variables:
# %directory% - The path to the downloaded files.
# %nzbname% - The friendly name of the release that was downloaded (NZB filename with path and extension removed).
# %nzbfilename% - The filename of the process NZB file.
# %category% - The category of this download.
# %parstatus% - Par check result.
# %unpackstatus% - Unpack result.
#Description=%nzbname% download is complete.

### NZBGET POST-PROCESSING SCRIPT                                          ###
##############################################################################

import sys
try:
        import urllib.request as urllibRequest
        import urllib.parse as urllibParse
except:
        import urllib
        urllibRequest = urllib
        urllibParse = urllib
import os

# 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 '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)

required_options = ('NZBPO_APIKEY', 'NZBPO_APPLICATIONNAME', 'NZBPO_EVENTNAME', 'NZBPO_DESCRIPTION')
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)

print('[DETAIL] Script successfully started')
sys.stdout.flush()

class AuthURLOpener(urllib.FancyURLopener):
        def __init__(self, user, pw):
                self.username = user
                self.password = pw
                self.numTries = 0
                urllib.FancyURLopener.__init__(self)

        def prompt_user_passwd(self, host, realm):
                if self.numTries == 0:
                        self.numTries = 1
                        return (self.username, self.password)
                else:
                        return ('', '')

        def openit(self, url, params):
                self.numTries = 0
                return urllib.FancyURLopener.open(self, url, params)

url='https://www.notifymyandroid.com/publicapi/notify'

apikey=os.environ['NZBPO_APIKEY']
applicationName=os.environ['NZBPO_APPLICATIONNAME']
eventName=os.environ['NZBPO_EVENTNAME']
description=os.environ['NZBPO_DESCRIPTION'] \
        .replace("%directory%", os.environ['NZBPP_DIRECTORY']) \
        .replace("%nzbname%", os.environ['NZBPP_NZBNAME']) \
        .replace("%nzbfilename%", os.environ['NZBPP_NZBFILENAME']) \
        .replace("%category%", os.environ['NZBPP_CATEGORY']) \
        .replace("%parstatus%", os.environ['NZBPP_PARSTATUS']) \
        .replace("%unpackstatus%", os.environ['NZBPP_UNPACKSTATUS'])

bSuccess = True
for key in apikey.split(','):
        params = urllib.urlencode({'apikey': key, 'application': applicationName, 'event': eventName, 'description': description})

        myOpener = AuthURLOpener('', '')

        try:
                urlObj = myOpener.openit(url, params)
        except IOError as e:
                print('[ERROR] Unable to open URL: ' + str(e))
                sys.exit(POSTPROCESS_ERROR)

        if urlObj.getcode() != 200:
                print('[ERROR] Unexpected error code returned for key ' + key + ': ' + str(urlObj.getcode()))
                print(urlObj.readlines())
                bSuccess = False

if not bSuccess:
        sys.exit(POSTPROCESS_ERROR)
else:
#       print('SUCCESS!')
        sys.exit(POSTPROCESS_SUCCESS)
edit: now supports notifications to multiple accounts by separating API keys with commas. Also some tweaks to work on newer versions of Python.

Re: postprocess - NotifyMyAndroid

Posted: 17 Feb 2014, 10:40
by kantjer
I have been trying to get this postprocess to work on NZBGet 12.0 installed on my Synology.
But I'm getting these kind of errors:

Code: Select all

Post-process-script NotifyMyAndroid.py for Castle.2009.S06E15.720p.HDTV.X264-DIMENSION failed (terminated with unknown status)
Al build in postprocessing script run fine, So I guess Python can't be the issue. any tips on how to solve this would be greatly appreciated.

Re: postprocess - NotifyMyAndroid

Posted: 17 Feb 2014, 11:03
by hugbug
Is this the only message in the log? It's usually more than one message line.

Re: postprocess - NotifyMyAndroid

Posted: 17 Feb 2014, 12:45
by kantjer
This is th ecomplete log:

Code: Select all

INFO	Mon Feb 17 2014 13:43:36	Collection Castle.2009.S06E15.720p.HDTV.X264-DIMENSION added to history
ERROR	Mon Feb 17 2014 13:43:35	Post-process-script NotifyMyAndroid.py for Castle.2009.S06E15.720p.HDTV.X264-DIMENSION failed (terminated with unknown status)
INFO	Mon Feb 17 2014 13:43:35	NotifyMyAndroid: env: python : No such file or directory
INFO	Mon Feb 17 2014 13:43:35	Executing post-process-script NotifyMyAndroid.py for Castle.2009.S06E15.720p.HDTV.X264-DIMENSION
INFO	Mon Feb 17 2014 13:43:35	Unpausing queue after cleanup
INFO	Mon Feb 17 2014 13:43:35	Nothing to cleanup for Castle.2009.S06E15.720p.HDTV.X264-DIMENSION
INFO	Mon Feb 17 2014 13:43:35	Cleaning up Castle.2009.S06E15.720p.HDTV.X264-DIMENSION
INFO	Mon Feb 17 2014 13:43:35	Pausing queue before cleanup
INFO	Mon Feb 17 2014 13:43:35	Queueing Castle.2009.S06E15.720p.HDTV.X264-DIMENSION for post-processing
INFO	Mon Feb 17 2014 13:43:35	Castle.2009.S06E15.720p.HDTV.X264-DIMENSION returned from history back to download queue
INFO	Mon Feb 17 2014 13:43:35	Executing post-process-script NotifyMyAndroid.py for Castle.2009.S06E15.720p.HDTV.X264-DIMENSION

Re: postprocess - NotifyMyAndroid

Posted: 17 Feb 2014, 14:05
by hugbug
It's the line endings.
Use dos2unix command or convert line endings in unix format using a text editor.

Re: postprocess - NotifyMyAndroid

Posted: 17 Feb 2014, 18:45
by kantjer
Thanks that was indeed the issue. It's working now.

Re: postprocess - NotifyMyAndroid

Posted: 30 May 2014, 03:37
by darxide
Got any tips on modifying this so it notifies me on error only? I have got SB and CP notifying me on success, but would like NZBGet to notify on error.

My python is a little rusty, but it looks like I'd just tweak the Else of this excerpt from the end of the CP script.

Code: Select all

if result == 0:
    Logger.info("MAIN: The autoProcessMovie script completed successfully.")
    if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
        sys.exit(POSTPROCESS_SUCCESS)
else:
    Logger.info("MAIN: A problem was reported in the autoProcessMovie script.")
    if os.environ.has_key('NZBOP_SCRIPTDIR'): # return code for nzbget v11
        sys.exit(POSTPROCESS_ERROR)
Sound like I am on the right path?

Re: postprocess - NotifyMyAndroid

Posted: 22 Mar 2016, 21:24
by zerbart
Anybody know how to send a nma notification with a different priority using this script?