|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 |
| -import urllib |
4 |
| -import urllib2 |
5 |
| -import urlparse |
| 3 | +from http.client import HTTPSConnection |
| 4 | +from urllib.parse import urlencode |
6 | 5 | import json
|
7 | 6 | import os
|
8 | 7 |
|
9 |
| -PUSHOVER_API = "https://api.pushover.net/1/" |
10 |
| - |
11 | 8 | class PushoverError(Exception): pass
|
12 | 9 |
|
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 |
15 | 124 |
|
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'] |
20 | 132 |
|
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) |
27 | 138 |
|
28 |
| - if data['status'] != 1: |
29 |
| - raise PushoverError(output) |
| 139 | + if data['status'] != 1: |
| 140 | + raise PushoverError(output) |
| 141 | + else: |
| 142 | + return True |
0 commit comments