From ee5b8e21a0e7275ee1da5edb5e995c4d521e8fd2 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 13 Dec 2017 23:55:38 +0100 Subject: [PATCH] * If we get a push tickle on PushBullet, get that latest push * Fixed device_id attribute name clash --- platypush/backend/__init__.py | 5 ++- platypush/backend/pushbullet/__init__.py | 45 ++++++++++++++++++------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/platypush/backend/__init__.py b/platypush/backend/__init__.py index 22f70a148..1e01972b5 100644 --- a/platypush/backend/__init__.py +++ b/platypush/backend/__init__.py @@ -47,10 +47,9 @@ class Backend(Thread): return isinstance(self, LocalBackend) def on_msg(self, msg): - if 'target' not in msg: - return # No target - + if 'target' not in msg: return # No target target = msg.pop('target') + if target != self.device_id and not self.is_local(): return # Not for me diff --git a/platypush/backend/pushbullet/__init__.py b/platypush/backend/pushbullet/__init__.py index 427a4c9fe..a9b870740 100644 --- a/platypush/backend/pushbullet/__init__.py +++ b/platypush/backend/pushbullet/__init__.py @@ -1,6 +1,7 @@ import logging import json import requests +import time import websocket from .. import Backend @@ -9,7 +10,7 @@ class PushbulletBackend(Backend): def _init(self, token, device): self.token = token self.device_name = device - self.device_id = self.get_device_id() + self.pb_device_id = self.get_device_id() @staticmethod def _on_init(ws): @@ -29,6 +30,30 @@ class PushbulletBackend(Backend): self.ws.close() self._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 @@ -36,20 +61,20 @@ class PushbulletBackend(Backend): logging.exception(e) return - if data['type'] != 'push': + 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 - push = data['push'] logging.debug('Received push: {}'.format(push)) - if 'body' not in push: - return + if 'body' not in push: return body = push['body'] - try: - body = json.loads(body) - except ValueError as e: - return + try: body = json.loads(body) + except ValueError as e: return self.on_msg(body) @@ -89,7 +114,7 @@ class PushbulletBackend(Backend): headers = { 'Access-Token': self.token }, json = { 'type': 'note', - 'device_iden': self.device_id, + 'device_iden': self.pb_device_id, 'body': msg, } ).json()