Implemented send_msg on Pushbullet backend

This commit is contained in:
Fabio Manganiello 2017-12-11 19:49:08 +01:00
parent e9e0512a52
commit 50413dd89d
3 changed files with 24 additions and 28 deletions

View file

@ -16,7 +16,6 @@ class LocalBackend(Backend):
if isinstance(msg, dict): if isinstance(msg, dict):
msg = json.dumps(msg) msg = json.dumps(msg)
if not isinstance(msg, str): if not isinstance(msg, str):
msg = json.dumps(msg)
raise RuntimeError('Invalid non-JSON message') raise RuntimeError('Invalid non-JSON message')
msglen = len(msg)+1 # Include \n msglen = len(msg)+1 # Include \n

View file

@ -2,9 +2,15 @@ import logging
import json import json
import websocket import websocket
from pushbullet import Pushbullet
from .. import Backend from .. import Backend
class PushbulletBackend(Backend): class PushbulletBackend(Backend):
_requires = [
'pushbullet'
]
def _init(self, token, device): def _init(self, token, device):
self.token = token self.token = token
self.device = device self.device = device
@ -61,6 +67,20 @@ class PushbulletBackend(Backend):
self.ws.backend = self 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): def run(self):
self._init_socket() self._init_socket()
self.ws.run_forever() self.ws.run_forever()

View file

@ -7,10 +7,10 @@ import re
import sys import sys
import yaml import yaml
from pushbullet import Pushbullet
from runbullet import parse_config_file from runbullet import parse_config_file
from runbullet.backend.kafka import KafkaBackend from runbullet.backend.kafka import KafkaBackend
from runbullet.backend.local import LocalBackend from runbullet.backend.local import LocalBackend
from runbullet.backend.pushbullet import PushbulletBackend
def print_usage(): def print_usage():
@ -29,24 +29,7 @@ def get_pb_device_by_name(pb, name):
return devices[0] if devices else None 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): 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 \ if 'backend.local' in config \
and 'pusher' in config['backend.local'] \ and 'pusher' in config['backend.local'] \
and config['backend.local']['pusher']: and config['backend.local']['pusher']:
@ -55,8 +38,8 @@ def get_backend(config):
elif 'backend.pushbullet' in config \ elif 'backend.pushbullet' in config \
and 'pusher' in config['backend.pushbullet'] \ and 'pusher' in config['backend.pushbullet'] \
and config['backend.pushbullet']['pusher']: and config['backend.pushbullet']['pusher']:
API_KEY = config['backend.pushbullet']['token'] c = config['backend.pushbullet']
return Pushbullet(API_KEY) return PushbulletBackend({'token':c['token'], 'device':c['device']})
elif 'backend.kafka' in config \ elif 'backend.kafka' in config \
and 'pusher' in config['backend.kafka'] \ and 'pusher' in config['backend.kafka'] \
and config['backend.kafka']['pusher']: and config['backend.kafka']['pusher']:
@ -113,13 +96,7 @@ def main():
print('msg: {}'.format(msg)) print('msg: {}'.format(msg))
if isinstance(backend, Pushbullet): backend.send_msg(msg)
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)
if __name__ == '__main__': if __name__ == '__main__':
main() main()