-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushbullet_max.py
152 lines (126 loc) · 5.44 KB
/
pushbullet_max.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import json
import requests
from lbc_utils import log
# @brief: This class represents a Pushbullet account (i.e : one PB token), with all its attached devices
# Hence the inner class PushbulletDevice
# It's this object that handles all the Pushbullet API requests, including
# sending pushes
class Pushbullet():
# Inner class for PB devices
# --------------------------
class PushbulletDevice():
def __init__(self, JSON=None, Account=None):
if json is not None:
self._nickname = JSON['nickname']
self._iden = JSON['iden']
self._type = JSON['type']
self._active = JSON['active']
self._parentAccount = Account
else:
raise ValueError(
"Cannot init PushbulletDevice without json data")
def __str__(self):
if (self._nickname is not None and self._iden is not None and self._type is not None):
return str(self._nickname) + " / iden: " + str(self._iden) + " / " + str(self._type)
else:
return "None"
def getAccount(self):
return self._parentAccount
def isInSendlist(self):
return self._iden in self._parentAccount.sendlist
def getIden(self):
return self._iden
# Constants
# ---------
JSON_HEADER = {"Content-Type": "application/json"}
PB_URL_ME = "https://api.pushbullet.com/v2/users/me"
PB_URL_DEVICES = "https://api.pushbullet.com/v2/devices"
PB_URL_PUSH = "https://api.pushbullet.com/v2/pushes"
# Methods & functions
# -------------------
def __init__(self, pb_token=None, JSON=None):
self.devices = []
self.sendlist = []
if JSON is not None:
self.pushbulletToken = JSON["pushbulletToken"]
for deviceJSON in JSON["devices"]:
self.devices += [
Pushbullet.PushbulletDevice(JSON=deviceJSON, Account=self)]
log(1, "Added device to new instance: " +
str(self.devices[-1]))
self.sendlist = JSON["sendlist"]
elif pb_token is not None:
self.pushbulletToken = pb_token
else:
raise ValueError("Cannot initialize a Pushbullet object with \
no Token or no JSON data")
# Session object must always be initialized
self.curSession = requests.session()
self.curSession.headers.update(self.JSON_HEADER)
authParam = "Bearer " + self.pushbulletToken
self.curSession.headers.update({"Authorization": authParam})
if not self.isTokenValid():
raise ValueError("Invalid token (\"{}\")".format(self.pushbulletToken))
def __eq__(self, other):
return self.pushbulletToken == other.pushbulletToken and self.getDevices() == other.getDevices()
def __str__(self):
result = "Token = " + self.pushbulletToken + "\n"
self.getDevices()
result += "\n".join([str(device) + "\n" for device in self.devices])
return result
def getToken(self):
return self.pushbulletToken
def isTokenValid(self):
response = self.curSession.get(self.PB_URL_ME)
if response.status_code == 401:
return False
else:
return True
def getDevices(self, forceRefresh=False):
if len(self.devices) == 0 or forceRefresh:
try:
response = self.curSession.get(self.PB_URL_DEVICES)
if response.status_code == 401:
# Token is not a valid Pushbullet token
log(1, "Error: invalid token ({})".format(self.pushbulletToken))
return -3
elif response.status_code != 200:
log(1, "Error during getDevices request - HTTP " +
str(response.status_code))
return -2
else:
for device in response.json()['devices']:
self.devices += [
self.PushbulletDevice(device, Account=self)]
return None
# return response.json()['devices'] # Really necessary ?
except Exception as e:
log(0, e.args)
return -1
def addDeviceToSendlist(self, deviceIden):
if deviceIden not in self.sendlist:
self.sendlist += [deviceIden]
def removeDeviceFromSendlist(self, deviceIden):
if deviceIden in self.sendlist:
self.sendlist.remove(deviceIden)
def sendLinkToDevicesInSendlist(self, title="", body="", url="", lbcItem=None):
if lbcItem is not None:
_title = lbcItem.getTitle()
_body = lbcItem.get_date_string() + " - " + lbcItem.getPrice()
_url = lbcItem.getURL()
else:
_title = title
_body = body
_url = url
for deviceIden in self.sendlist:
data = {
"type": "link",
"title": _title,
"body": _body,
"url": _url,
}
return self.curSession.post(self.PB_URL_PUSH, data=json.dumps(data))
def getJSONCompatibleDict(self):
# Request objects are not JSON-serializable & are not useful to save,
# so we need to omit them when exporting
return {key: value for key, value in self.__dict__.items() if key is not "curSession"}