From 50413dd89d19459efa4bc209e703291013efc686 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 11 Dec 2017 19:49:08 +0100 Subject: [PATCH] Implemented send_msg on Pushbullet backend --- runbullet/backend/local/__init__.py | 1 - runbullet/backend/pushbullet/__init__.py | 20 +++++++++++++++ runbullet/bin/pusher | 31 +++--------------------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/runbullet/backend/local/__init__.py b/runbullet/backend/local/__init__.py index 6d74cf9a1..9cdd9d907 100644 --- a/runbullet/backend/local/__init__.py +++ b/runbullet/backend/local/__init__.py @@ -16,7 +16,6 @@ class LocalBackend(Backend): if isinstance(msg, dict): msg = json.dumps(msg) if not isinstance(msg, str): - msg = json.dumps(msg) raise RuntimeError('Invalid non-JSON message') msglen = len(msg)+1 # Include \n diff --git a/runbullet/backend/pushbullet/__init__.py b/runbullet/backend/pushbullet/__init__.py index b8583d1fd..f42ee9792 100644 --- a/runbullet/backend/pushbullet/__init__.py +++ b/runbullet/backend/pushbullet/__init__.py @@ -2,9 +2,15 @@ import logging import json import websocket +from pushbullet import Pushbullet + from .. import Backend class PushbulletBackend(Backend): + _requires = [ + 'pushbullet' + ] + def _init(self, token, device): self.token = token self.device = device @@ -61,6 +67,20 @@ class PushbulletBackend(Backend): self.ws.backend = self + def send_msg(self, msg): + if isinstance(msg, dict): + msg = json.dumps(msg) + if not isinstance(msg, str): + raise RuntimeError('Invalid non-JSON message') + + pb = Pushbullet(self.token) + devices = [dev for dev in pb.devices if dev.nickname == self.device] + if not devices: + raise RuntimeError('No such device: {}'.format(self.device)) + + device = devices[0] + pb.push_note('', msg, device) + def run(self): self._init_socket() self.ws.run_forever() diff --git a/runbullet/bin/pusher b/runbullet/bin/pusher index d2d2458fa..b10017fab 100755 --- a/runbullet/bin/pusher +++ b/runbullet/bin/pusher @@ -7,10 +7,10 @@ import re import sys import yaml -from pushbullet import Pushbullet from runbullet import parse_config_file from runbullet.backend.kafka import KafkaBackend from runbullet.backend.local import LocalBackend +from runbullet.backend.pushbullet import PushbulletBackend def print_usage(): @@ -29,24 +29,7 @@ def get_pb_device_by_name(pb, name): return devices[0] if devices else None -def send_pb_message(pb, device_name, msg): - device = get_pb_device_by_name(pb, name=device_name) - if not device: - print('Device {} not found - please create a virtual device on ' + - 'your PushBullet account'.format(config['backend.pushbullet']['device'])) - return - - pb.push_note('', json.dumps(msg), device) - -def send_kafka_message(backend, msg): - backend.send_msg(msg) - -def send_local_message(backend, msg): - backend.send_msg(msg) - def get_backend(config): - # TODO Refactor this as something better and reuse the same - # backend classes from the runbullet consumer module if 'backend.local' in config \ and 'pusher' in config['backend.local'] \ and config['backend.local']['pusher']: @@ -55,8 +38,8 @@ def get_backend(config): elif 'backend.pushbullet' in config \ and 'pusher' in config['backend.pushbullet'] \ and config['backend.pushbullet']['pusher']: - API_KEY = config['backend.pushbullet']['token'] - return Pushbullet(API_KEY) + c = config['backend.pushbullet'] + return PushbulletBackend({'token':c['token'], 'device':c['device']}) elif 'backend.kafka' in config \ and 'pusher' in config['backend.kafka'] \ and config['backend.kafka']['pusher']: @@ -113,13 +96,7 @@ def main(): print('msg: {}'.format(msg)) - if isinstance(backend, Pushbullet): - send_pb_message(backend, config['backend.pushbullet']['device'], msg) - elif isinstance(backend, KafkaBackend): - send_kafka_message(backend, msg) - elif isinstance(backend, LocalBackend): - send_local_message(backend, msg) - + backend.send_msg(msg) if __name__ == '__main__': main()