platypush/platypush/backend/pushbullet/__init__.py

116 lines
3.1 KiB
Python
Raw Normal View History

import logging
import json
import requests
import time
import websocket
from .. import Backend
class PushbulletBackend(Backend):
def _init(self, token, device):
self.token = token
self.device_name = device
self.pb_device_id = self.get_device_id()
@staticmethod
def _on_msg(ws, msg):
ws.backend._on_push(msg)
@staticmethod
def _on_error(ws, e):
logging.exception(e)
2017-12-14 01:58:42 +01:00
backend = ws.backend
ws.close()
backend._init_socket()
def _get_latest_push(self):
t = int(time.time()) - 2
try:
response = requests.get(
u'https://api.pushbullet.com/v2/pushes',
headers = { 'Access-Token': self.token },
params = {
'modified_after': str(t),
'active' : 'true',
'limit' : 1,
}
)
response = response.json()
except Exception as e:
logging.exception(e)
raise e
if 'pushes' in response and response['pushes']:
return response['pushes'][0]
else:
return {}
def _on_push(self, data):
try:
data = json.loads(data) if isinstance(data, str) else push
except Exception as e:
logging.exception(e)
return
if data['type'] == 'tickle' and data['subtype'] == 'push':
push = self._get_latest_push()
elif data['type'] == 'push':
push = data['push']
else:
return # Not a push notification
logging.debug('Received push: {}'.format(push))
if 'body' not in push: return
body = push['body']
try: body = json.loads(body)
except ValueError as e: return
self.on_msg(body)
2017-11-27 09:56:59 +01:00
def _init_socket(self):
self.ws = websocket.WebSocketApp(
'wss://stream.pushbullet.com/websocket/' + self.token,
on_message = self._on_msg,
on_error = self._on_error)
self.ws.backend = self
2017-11-27 09:56:59 +01:00
def get_device_id(self):
response = requests.get(
u'https://api.pushbullet.com/v2/devices',
headers = { 'Access-Token': self.token },
).json()
devices = [dev for dev in response['devices'] if 'nickname' in dev
and dev['nickname'] == self.device_name]
if not devices:
raise RuntimeError('No such Pushbullet device: {}'
.format(self.device_name))
return devices[0]['iden']
def _send_msg(self, msg):
requests.post(
u'https://api.pushbullet.com/v2/pushes',
headers = { 'Access-Token': self.token },
json = {
'type': 'note',
'device_iden': self.pb_device_id,
'body': json.dumps(msg),
}
).json()
2017-11-27 09:56:59 +01:00
def run(self):
2017-11-29 03:37:33 +01:00
self._init_socket()
2017-12-12 19:26:23 +01:00
logging.info('Initialized Pushbullet backend - device_id: {}'
2017-12-12 20:16:00 +01:00
.format(self.device_name))
2017-12-12 19:26:23 +01:00
self.ws.run_forever()
# vim:sw=4:ts=4:et: