-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPushbullet.py
210 lines (191 loc) · 6.05 KB
/
Pushbullet.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sublime, sublime_plugin
import urllib.request
import xml.etree.ElementTree as etree
import base64
import threading
import json
import os
import array
import webbrowser
import requests
# Disable HTTPS verification warnings.
from requests.packages import urllib3
urllib3.disable_warnings()
class PushbulletSendNoteCommand(sublime_plugin.TextCommand):
def run(self, edit):
keyEntered = self.check_api_key(self.view.window())
if keyEntered == 1:
for item in Pushbullet().get_push_text(self.view):
Pushbullet().send_note(None, "note", self.view.name(), item, self.view)
pass
def check_api_key(self, window):
settings = sublime.load_settings('Pushbullet.sublime-settings')
if settings.get("token") == None:
sublime.message_dialog("Please Enter your Pushbullet API key")
webbrowser.open("https://www.pushbullet.com/#settings")
window.show_input_panel("API Key:", "", self.on_Api_key_entered, None, None)
return 0
pass
return 1
def on_Api_key_entered(self, result):
settings = sublime.load_settings('Pushbullet.sublime-settings')
settings.set("token", result)
sublime.save_settings('Pushbullet.sublime-settings')
self.run(None)
class PushbulletSendNoteToDeviceCommand(sublime_plugin.TextCommand):
def run(self, edit):
PushbulletSendNoteCommand(self).check_api_key(self.view.window())
thread = ApiCall(None, 'https://api.pushbullet.com/v2/devices');
thread.sublime = self
thread.start()
self.handle_threads(edit, thread)
def handle_threads(self, edit, thread, offset=0, i=0, dir=1):
if thread.is_alive():
# This animates a little activity indicator in the status area
before = i % 8
after = (7) - before
if not after:
dir = -1
if not before:
dir = 1
i += dir
self.view.set_status('pushbullet', 'Getting devices [%s=%s]' % \
(' ' * before, ' ' * after))
sublime.set_timeout(lambda: self.handle_threads(edit, thread,
offset, i, dir), 100)
return
else:
self.view.erase_status('pushbullet')
device_list = []
pushable_devices = []
for device in thread.result["devices"]:
if device["pushable"]:
pushable_devices.append(device)
device_list.append(device["nickname"])
pass
pass
self.devices = pushable_devices
self.view.window().show_quick_panel(device_list, self.on_device_selected)
def on_device_selected(self, result):
if result != -1:
selected_device = self.devices[result]
for item in Pushbullet().get_push_text(self.view):
Pushbullet().send_note(selected_device["iden"], "note", self.view.name(), item, self.view)
pass
class PushbulletSendNoteToContactCommand(sublime_plugin.TextCommand):
def run(self, edit):
PushbulletSendNoteCommand(self).check_api_key(self.view.window())
thread = ApiCall(None, 'https://api.pushbullet.com/v2/contacts');
thread.sublime = self
thread.start()
self.handle_threads(edit, thread)
def handle_threads(self, edit, thread, offset=0, i=0, dir=1):
if thread.is_alive():
# This animates a little activity indicator in the status area
before = i % 8
after = (7) - before
if not after:
dir = -1
if not before:
dir = 1
i += dir
self.view.set_status('pushbullet', 'Getting contacts [%s=%s]' % \
(' ' * before, ' ' * after))
sublime.set_timeout(lambda: self.handle_threads(edit, thread,
offset, i, dir), 100)
return
else:
self.view.erase_status('pushbullet')
device_list = []
pushable_devices = []
for device in thread.result["contacts"]:
if device["active"]:
pushable_devices.append(device)
device_list.append(device["name"])
pass
pass
self.devices = pushable_devices
self.view.window().show_quick_panel(device_list, self.on_device_selected)
def on_device_selected(self, result):
if result != -1:
selected_device = self.devices[result]
for item in Pushbullet().get_push_text(self.view):
Pushbullet().send_note(None, "note", self.view.name(), item, self.view, selected_device["email"])
pass
class Pushbullet:
def send_note(self, device, note_type, title, body, view, email=None):
if device != None:
data = {
"device_iden": device,
"type" : note_type,
"title" : title,
"body" : body
}
pass
elif email != None:
data = {
"type" : note_type,
"title" : title,
"body" : body,
"email" : email
}
else:
data = {
"type" : note_type,
"title" : title,
"body" : body
}
thread = ApiCall(data, 'https://api.pushbullet.com/v2/pushes');
thread.start()
self.handle_threads(view, "Pushing", None, None, thread)
def handle_threads(self, view, waiting_message, callback, edit, thread, offset=0, i=0, dir=1):
if thread.is_alive():
# This animates a little activity indicator in the status area
before = i % 8
after = (7) - before
if not after:
dir = -1
if not before:
dir = 1
i += dir
view.set_status('pushbullet', waiting_message + ' [%s=%s]' % \
(' ' * before, ' ' * after))
sublime.set_timeout(lambda: self.handle_threads(view, waiting_message, callback, edit, thread,
offset, i, dir), 100)
return
else:
view.erase_status('pushbullet')
if callback != None:
callback()
pass
def get_push_text(self, view):
selections = view.sel()
selection_texts = []
for selection in selections:
text = view.substr(selection)
if text:
selection_texts.append(text)
pass
if not selection_texts:
selection_texts.append(view.substr(sublime.Region(0, view.size())))
pass
return selection_texts
class ApiCall(threading.Thread):
def __init__(self, data, url):
threading.Thread.__init__(self)
self.data = data
self.url = url
def run(self):
settings = sublime.load_settings('Pushbullet.sublime-settings')
authheader = "Bearer " + settings.get("token")
headers = {"Authorization":authheader}
session = requests.session()
response = None
if self.data != None:
headers["Content-Type"] = 'application/json'
response = session.post(self.url, data=json.dumps(self.data).encode("utf8"), headers=headers)
else:
response = session.get(self.url, headers=headers)
data = response.json()
response.close()
self.result = data