Skip to content

Commit 8cfe3c9

Browse files
committed
Python3 overhaul with class based system.
1 parent 8e44993 commit 8cfe3c9

File tree

4 files changed

+247
-68
lines changed

4 files changed

+247
-68
lines changed

README.md

+81-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,90 @@
1-
A simpe Python client for http://pushover.net/ API
1+
A simpe Python3.3 client for http://pushover.net/ API based off of https://github.com/pix0r/pushover by pix0r.
22

33
Install:
44

5-
pip install https://github.com/pix0r/pushover.git
5+
pip install https://github.com/Wyattjoh/pushover
66

7-
Usage:
7+
Pushover Class:
88

9-
from pushover import pushover
9+
class Pushover(builtins.object)
10+
| Creates a Pushover handler.
11+
|
12+
| Usage:
13+
|
14+
| po = Pushover("My App Token")
15+
| po.user("My User Token", "My User Device Name")
16+
|
17+
| msgid, msg = po.msg("Hello, World!")
18+
|
19+
| po.send(msgid)
20+
|
21+
| Methods defined here:
22+
|
23+
| __init__(self, token=None)
24+
| Creates a Pushover object.
25+
|
26+
| msg(self, message)
27+
| Creates a PushoverMessage object. Takes one "message" parameter (the message to be sent).
28+
| Returns with message id (mid) and PushoverMessage object (msg).
29+
|
30+
| send(self, mid)
31+
| Sends a specified message with id "mid".
32+
|
33+
| sendall(self)
34+
| Sends all PushoverMessage's owned by the Pushover object.
35+
|
36+
| user(self, user_token, user_device=None)
37+
| Sets a single user to be the recipient of all messages created with this Pushover object.
38+
39+
PushoverMessage Class:
40+
41+
class PushoverMessage(builtins.object)
42+
| Used for storing message specific data.
43+
|
44+
| Methods defined here:
45+
|
46+
| __init__(self, message)
47+
| Creates a PushoverMessage object.
48+
|
49+
| __str__(self)
50+
|
51+
| get(self)
52+
| Returns a dictionary with the values for the specified message.
53+
|
54+
| set(self, key, value)
55+
| Sets the value of a field "key" to the value of "value".
56+
|
57+
| user(self, user_token, user_device=None)
58+
| Sets a single user to be the recipient of this message with token "user_token" and device "user_device".
59+
60+
Sample Usage:
61+
62+
```python
63+
from pushover import Pushover
64+
65+
po = Pushover("My App Token")
66+
po.user("My User Token")
67+
68+
msg = po.msg("Hello, World!")
69+
70+
msg.set("title", "Best title ever!!!")
71+
72+
po.send(msg)
73+
```
1074

11-
pushover(message="Hello, world",
12-
token="My App Token",
13-
user="My User Key",
14-
)
15-
1675
Or using command line utility:
1776

18-
pushover "Hello, world" --token="My App Token" --user="My User Key"
77+
Usage: pushover <message> --token=<TOKEN> --user=<USER> [options]
78+
79+
Options:
80+
-h, --help show this help message and exit
81+
--token=<TOKEN> Pushover app token (overrides environment
82+
PUSHOVER_TOKEN)
83+
--user=<USER> Pushover user key
84+
85+
Optional:
86+
--device DEVICE Pushover device name
87+
--title TITLE Message title
88+
--timestamp TIMESTAMP Optional UNIX timestamp
89+
--priority PRIORITY Optional priority setting (0=normal, 1=high)
1990

pushover

+31-36
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

3-
import argparse
3+
"""
4+
Usage: pushover <message> --token=<TOKEN> --user=<USER> [options]
45
5-
from pushover import pushover
6+
Options:
7+
-h, --help show this help message and exit
8+
--token=<TOKEN> Pushover app token (overrides environment
9+
PUSHOVER_TOKEN)
10+
--user=<USER> Pushover user key
11+
12+
Optional:
13+
--device DEVICE Pushover device name
14+
--title TITLE Message title
15+
--timestamp TIMESTAMP Optional UNIX timestamp
16+
--priority PRIORITY Optional priority setting (0=normal, 1=high)
17+
"""
18+
19+
import docopt
20+
import pushover
621

722
if __name__ == '__main__':
8-
parser = argparse.ArgumentParser(description="Send Pushover.net message")
9-
parser.add_argument('message',
10-
type=str,
11-
help="Message to send",
12-
)
13-
parser.add_argument('--token',
14-
type=str,
15-
help="Pushover app token (overrides environment PUSHOVER_TOKEN)",
16-
)
17-
parser.add_argument('--user',
18-
type=str,
19-
help="Pushover user key (overrides ",
20-
)
21-
parser.add_argument('--title',
22-
type=str,
23-
help="Message title",
24-
)
25-
parser.add_argument('--timestamp',
26-
type=int,
27-
help="Optional UNIX timestamp",
28-
)
29-
parser.add_argument('--priority',
30-
type=int,
31-
help="Optional priority setting (0=normal, 1=high)",
32-
default=0,
33-
)
34-
args = parser.parse_args()
35-
36-
d = {}
37-
for k, v in vars(args).iteritems():
38-
if v: d[k] = v
39-
40-
pushover(**d)
23+
arguments = docopt.docopt(__doc__)
24+
25+
print(arguments)
26+
27+
po = pushover.Pushover(arguments['--token'])
28+
po.user(arguments['--user'], arguments['--device'])
29+
msgid, msg = po.msg(arguments['<message>'])
30+
31+
msg.set('title', arguments['--title'])
32+
msg.set('timestamp', arguments['--timestamp'])
33+
msg.set('priority', arguments['--priority'])
34+
35+
po.send(msgid)

pushover.py

+132-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,142 @@
11
#!/usr/bin/env python
22

3-
import urllib
4-
import urllib2
5-
import urlparse
3+
from http.client import HTTPSConnection
4+
from urllib.parse import urlencode
65
import json
76
import os
87

9-
PUSHOVER_API = "https://api.pushover.net/1/"
10-
118
class PushoverError(Exception): pass
129

13-
def pushover(**kwargs):
14-
assert 'message' in kwargs
10+
class PushoverMessage:
11+
"""
12+
Used for storing message specific data.
13+
"""
14+
15+
def __init__(self, message):
16+
"""
17+
Creates a PushoverMessage object.
18+
"""
19+
self.vars = {}
20+
self.vars['message'] = message
21+
22+
def set(self, key, value):
23+
"""
24+
Sets the value of a field "key" to the value of "value".
25+
"""
26+
if value is not None:
27+
self.vars[key] = value
28+
29+
def get(self):
30+
"""
31+
Returns a dictionary with the values for the specified message.
32+
"""
33+
return self.vars
34+
35+
def user(self, user_token, user_device=None):
36+
"""
37+
Sets a single user to be the recipient of this message with token "user_token" and device "user_device".
38+
"""
39+
self.set('user', user_token)
40+
self.set('device', user_device)
41+
42+
def __str__(self):
43+
return "PushoverMessage: " + str(self.vars)
44+
45+
class Pushover:
46+
"""
47+
Creates a Pushover handler.
48+
49+
Usage:
50+
51+
po = Pushover("My App Token")
52+
po.user("My User Token", "My User Device Name")
53+
54+
msg = po.msg("Hello, World!")
55+
56+
po.send(msg)
57+
58+
"""
59+
60+
PUSHOVER_SERVER = "api.pushover.net:443"
61+
PUSHOVER_ENDPOINT = "/1/messages.json"
62+
PUSHOVER_CONTENT_TYPE = { "Content-type": "application/x-www-form-urlencoded"}
63+
64+
def __init__(self, token=None):
65+
"""
66+
Creates a Pushover object.
67+
"""
68+
69+
if token is None:
70+
raise PushoverError("No token supplied.")
71+
else:
72+
self.token = token
73+
self.user_token = None
74+
self.user_device = None
75+
self.messages = []
76+
77+
def msg(self, message):
78+
"""
79+
Creates a PushoverMessage object. Takes one "message" parameter (the message to be sent).
80+
Returns with PushoverMessage object (msg).
81+
"""
82+
83+
message = PushoverMessage(message)
84+
self.messages.append(message)
85+
return message
86+
87+
def send(self, message):
88+
"""
89+
Sends a specified message with id "message" or as object.
90+
"""
91+
if type(message) is PushoverMessage:
92+
return self._send(message)
93+
else:
94+
raise PushoverError("Wrong type passed to Pushover.send()!")
95+
96+
def sendall(self):
97+
"""
98+
Sends all PushoverMessage's owned by the Pushover object.
99+
"""
100+
101+
response = []
102+
for message in self.messages:
103+
response.append(self._send(message))
104+
return response
105+
106+
def user(self, user_token, user_device=None):
107+
"""
108+
Sets a single user to be the recipient of all messages created with this Pushover object.
109+
"""
110+
111+
self.user_token = user_token
112+
self.user_device = user_device
113+
114+
def _send(self, message):
115+
"""
116+
Sends the specified PushoverMessage object via the Pushover API.
117+
"""
118+
119+
kwargs = message.get()
120+
kwargs['token'] = self.token
121+
122+
assert 'message' in kwargs
123+
assert self.token is not None
15124

16-
if not 'token' in kwargs:
17-
kwargs['token'] = os.environ['PUSHOVER_TOKEN']
18-
if not 'user' in kwargs:
19-
kwargs['user'] = os.environ['PUSHOVER_USER']
125+
if not 'user' in kwargs:
126+
if self.user is not None:
127+
kwargs['user'] = self.user_token
128+
if self.user_device is not None:
129+
kwargs['device'] = self.user_device
130+
else:
131+
kwargs['user'] = os.environ['PUSHOVER_USER']
20132

21-
url = urlparse.urljoin(PUSHOVER_API, "messages.json")
22-
data = urllib.urlencode(kwargs)
23-
req = urllib2.Request(url, data)
24-
response = urllib2.urlopen(req)
25-
output = response.read()
26-
data = json.loads(output)
133+
data = urlencode(kwargs)
134+
conn = HTTPSConnection(Pushover.PUSHOVER_SERVER)
135+
conn.request("POST", Pushover.PUSHOVER_ENDPOINT, data, Pushover.PUSHOVER_CONTENT_TYPE)
136+
output = conn.getresponse().read().decode('utf-8')
137+
data = json.loads(output)
27138

28-
if data['status'] != 1:
29-
raise PushoverError(output)
139+
if data['status'] != 1:
140+
raise PushoverError(output)
141+
else:
142+
return True

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
setup(
66
name='pushover',
77
version='0.1',
8-
url='https://github.com/pix0r/pushover',
8+
url='https://github.com/Wyattjoh/pushover',
99
py_modules=['pushover'],
1010
scripts=['pushover'],
11-
author='Mike Matz',
12-
author_email='[email protected]',
11+
author='Wyatt Johnson',
12+
author_email='[email protected]',
1313
)

0 commit comments

Comments
 (0)