Skip to content

Instantly share code, notes, and snippets.

@leoluk
Created August 8, 2012 17:17
Show Gist options
  • Save leoluk/3296748 to your computer and use it in GitHub Desktop.
Save leoluk/3296748 to your computer and use it in GitHub Desktop.
NotifyMyAndroid shell command result notification
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: http://github.com/leoluk
# License: GPLv3
#
"""
This quick-and-dirty script notifies you once a long-running shell command finishes,
using NotifyMyAndroid.com.
You should add the following alias to your .bashrc:
alias nma='/usr/bin/env python /path/to/script/nma.py "$?" "$_"'
You can use the script like this:
some_long_running_command; nma
some_long_running_command; nma "Long running command"
Once the command finishes, you will get a notification whether it was succesfull or not.
You can specify a friendly name as parameter, if you don't, the last word from the input
history is used.
Requires the requests package from PyPi (can be easily installed using easy_install requests,
pip install requests or by installing the python-requests package).
"""
import sys, os
import requests
API_URL = "http://www.notifymyandroid.com/publicapi/notify"
APP_KEY = "ce420bc287920d2e253ae3f862a22e81c739a74acef4ea7d"
def notify(event, description, application="CMDstate", priority=0, html=False, url=None):
params = {
'apikey': APP_KEY,
'description': description,
'event': event,
'application': application,
'priority': priority,
}
if html:
params['content-type'] = 'text/html'
if url:
params['url'] = url
req = requests.get(API_URL, params=params)
if req.status_code != 200:
raise RuntimeError("NMA call failed")
def main():
# quick and dirty argument parsing, -> argparse
if len(sys.argv) < 3:
print 'Usage: <arbitrary command>; nma "$?" "$_" "cmd_name"'
sys.exit(1)
error_code, last_input = sys.argv[1:3]
if len(sys.argv) == 4:
task_description = sys.argv[3]
else:
task_description = last_input
try:
if int(error_code) == 0:
notify(task_description, "successful")
else:
notify(task_description, "failed (Fehler %s)" % error_code, priority=1)
except RuntimeError:
print "Error: NMA API invocation failed"
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment