Skip to content

Instantly share code, notes, and snippets.

@nzoschke
Created December 17, 2009 23:52
Show Gist options
  • Save nzoschke/259139 to your computer and use it in GitHub Desktop.
Save nzoschke/259139 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2.4
import sys
import email
import urllib
import httplib
from optparse import OptionParser, OptionGroup
from datetime import datetime
PROWLURL='prowl.weks.net'
APIKEY='YOUR_PROWL_API_KEY'
def add(application, event, description=None, priority=0):
"""Send a message using the Prowl API"""
data = urllib.urlencode({
'apikey': APIKEY,
'event': event,
'application': application,
'priority': priority,
'description': description or ''
})
headers = {
"Content-type": "application/x-www-form-urlencoded",
'User-Agent': "urllib"
}
conn = httplib.HTTPSConnection(PROWLURL)
conn.request("POST", "/publicapi/add", data, headers)
response = conn.getresponse()
data = response.read()
return ("success" in data)
if __name__ == '__main__':
parser = OptionParser()
static_group = OptionGroup(parser, "Static Prowl Notifications")
static_group.add_option("-a", "--application", default="ProwlY",
help="The application generating the notification.")
static_group.add_option("-e", "--event",
help="The subject of the notification.")
static_group.add_option("-d", "--description",
help="The body of the notification. Generally terse.")
email_group = OptionGroup(parser, "Email-based Prowl Notifications")
email_group.add_option("--stdin", action="store_true",
help="Read email from STDIN, and set the notificaltion event and \
description to subject and body respectively.")
email_group.add_option("--google-voice-sms", action="store_true",
help="Parse notification event and description from Google Voice \
SMS email")
email_group.add_option("--google-voice-voicemail", action="store_true",
help="Parse notification event and description from Google Voice \
Voicemail email")
parser.add_option_group(static_group)
parser.add_option_group(email_group)
(options, args) = parser.parse_args()
event = description = None
# parse email from stdin
if options.stdin:
data = sys.stdin.read()
message = email.message_from_string(data)
event = message['Subject']
description = message.get_payload()
if options.google_voice_sms:
# take out the SMS-to-email footer
# FIXME: more robust
description = "\n".join(description.split('\n')[:-6])
elif options.google_voice_voicemail:
# FIXME: extract only the transcription
pass
# staic options override email if specified
event = options.event or event
description = options.description or description
if not event:
parser.print_help()
sys.exit()
add(options.application, event, description)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment