Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 137 lines (120 sloc) 4.53 KB
#!/usr/bin/env python
"""
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
If you do find a use for it, please consider submitting patches to
http://github.com/drewcrawford/Phone-Pipe or just shooting me an
e-mail at drew@sealedabstract.com
Usage: %prog [options] [--] [msg]"""
import optparse
import getpass
import os
import sys
import textwrap
import urllib
try:
import simplejson as json
except:
import json
API_URL = "https://%(username)s:%(secret)s@api.notifo.com/v1/send_notification"
SETTINGS = os.path.expanduser("~/.phonepipe")
def get_credentials(username = None, secret = None):
if username is not None and secret is not None:
return {"username": username, "secret": secret}
if not os.path.exists(SETTINGS):
set_credentials()
handle = open(SETTINGS)
try:
return json.load(handle)
finally:
handle.close()
def set_credentials():
username = raw_input("Username: ")
print textwrap.dedent("""
You can find your API secret in your Notify
account settings. Leave the field blank to
launch your browser.
""")
while True:
secret = getpass.getpass("API Secret: ")
if len(secret.strip()):
umask = os.umask(066)
handle = open(SETTINGS, "w")
try:
settings = {"username": username, "secret": secret}
json.dump(settings, handle, indent=2)
except:
handle.close()
os.umask(umask)
return
run_file("http://notifo.com/user/settings")
def run_file(file):
if sys.platform == "darwin":
os.system("open %s" % file)
elif sys.platform == "linux2":
os.system("xdg-open %s" % file)
else:
os.startfile(file)
def alert(msg, url=None, title="phonepipe", label="phonepipe", username=None, secret = None):
msg = msg.strip()
apiurl = API_URL % get_credentials(username, secret)
data = urllib.urlencode({
"title": title,
"msg": msg,
"label": label,
"uri": url if url is not None else ""
})
data = urllib.urlopen(apiurl, data)
try:
try:
result = json.load(data)
except IOError:
print "Failed to send message. Maybe your credentials are wrong?"
print "Error: %s" % sys.exc_info()[1]
set_credentials()
alert(msg, url, title)
return
finally:
data.close()
if result["status"] != "success" or result["response_message"] != "OK":
print "Notifo reported an error."
print "Status: %s" % result["status"]
print "Message: %s" % result["response_message"]
sys.exit(1)
def main():
msg = []
parser = optparse.OptionParser()
parser.usage = __doc__
parser.add_option("-u", "--username",
action="store", dest="username", default=None,
help="Override your ~/.phonepipe if you specify a secret as well.")
parser.add_option("-s", "--secret",
action="store", dest="secret", default=None,
help="Override your ~/.phonepipe when specified with a username.")
parser.add_option("-t", "--title",
action="store", dest="title", type="str", default="phonepipe",
help="The title of the message. (default: 'phonepipe')")
parser.add_option("-l", "--label",
action="store", dest="label", type="str", default="phonepipe",
help="A message label, generally the name of the sending service. (default: 'phonepipe')")
parser.add_option("", "--url",
action="store", dest="url", default=None,
help="If specified, the Notifo app makes a link from the message to it.")
(options, args) = parser.parse_args()
if len(args) > 0:
msg = [ " ".join(args) ]
if not sys.stdin.isatty():
try:
for line in sys.stdin.readlines():
msg.append(line)
except:
print "Failed to read message from stdin."
msg = "".join(msg)
if not msg:
msg = "Phone Pipe Alert"
alert(msg, url=options.url, title=options.title, label=options.label, username=options.username, secret=options.secret)
if __name__ == '__main__':
main()