This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphone
executable file
·136 lines (120 loc) · 4.53 KB
/
phone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/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 [email protected]
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)[email protected]/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()