Implemented send_msg on Pushbullet backend
This commit is contained in:
parent
e9e0512a52
commit
50413dd89d
3 changed files with 24 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue