platypush/notifier.py

154 lines
3.5 KiB
Python
Raw Normal View History

2017-10-29 05:17:42 +01:00
#!/usr/bin/env python
import importlib
2017-10-29 05:17:42 +01:00
import os
import logging
import json
import socket
2017-10-29 05:17:42 +01:00
import subprocess
import sys
import time
import websocket
2017-11-03 01:56:27 +01:00
import yaml
2017-10-29 05:17:42 +01:00
from threading import Thread
from getopt import getopt
2017-10-29 05:17:42 +01:00
2017-11-03 01:56:27 +01:00
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
#-----------#
curdir = os.path.dirname(os.path.realpath(__file__))
lib_dir = curdir + os.sep + 'lib'
sys.path.insert(0, lib_dir)
2017-11-03 01:56:27 +01:00
config_file = curdir + os.sep + 'config.yaml'
config = {}
with open(config_file,'r') as f:
config = yaml.load(f)
2017-10-29 05:17:42 +01:00
2017-11-03 01:56:27 +01:00
API_KEY = config['pushbullet_token']
DEVICE_ID = config['device_id'] \
if 'device_id' in config else socket.gethostname()
DEBUG = config['debug'] if 'debug' in config else False
2017-10-29 05:17:42 +01:00
def on_open(ws):
logging.info('Connection opened')
def on_close(ws):
logging.info('Connection closed')
2017-10-29 05:17:42 +01:00
def on_error(ws, error):
logging.error(error)
def _exec_func(body):
module = None
try:
module = importlib.import_module('plugins.{}'.format(body['plugin']))
except ModuleNotFoundError as e:
logging.warn('No such plugin: {}'.format(body['plugin']))
return
logging.info('Received push addressed to me: {}'.format(body))
args = body['args'] if 'args' in body else {}
cls = getattr(module, body['plugin'].title() + 'Plugin')
instance = cls()
out, err = instance.run(args)
logging.info('Command output: {}'.format(out))
if err:
logging.warn('Command error: {}'.format(err))
def _on_push(ws, data):
data = json.loads(data)
if data['type'] == 'tickle' and data['subtype'] == 'push':
logging.debug('Received push tickle')
return
2017-10-29 05:17:42 +01:00
if data['type'] != 'push':
2017-10-29 05:17:42 +01:00
return # Not a push notification
push = data['push']
logging.debug('Received push: {}'.format(push))
if 'body' not in push:
return
2017-10-29 20:45:19 +01:00
body = push['body']
2017-10-29 05:17:42 +01:00
try:
2017-10-29 20:45:19 +01:00
body = json.loads(body)
2017-10-29 05:17:42 +01:00
except ValueError as e:
return
if 'target' not in body or body['target'] != DEVICE_ID:
2017-10-29 05:17:42 +01:00
return # Not for me
if 'plugin' not in body:
return # No plugin specified
2017-10-29 05:17:42 +01:00
thread = Thread(target=_exec_func, args=(body,))
thread.start()
2017-10-29 05:17:42 +01:00
def on_push(ws, data):
2017-10-29 05:17:42 +01:00
try:
_on_push(ws, data)
except Exception as e:
on_error(ws, e)
def main():
2017-11-03 01:56:27 +01:00
DEBUG = False
config_file = curdir + os.sep + 'config.yaml'
optlist, args = getopt(sys.argv[1:], 'vh')
for opt, arg in optlist:
2017-11-03 01:56:27 +01:00
if opt == '-c':
config_file = arg
if opt == '-v':
DEBUG = True
elif opt == '-h':
print('''
2017-11-03 01:56:27 +01:00
Usage: {} [-v] [-h] [-c <config_file>]
-v Enable debug mode
-h Show this help
2017-11-03 01:56:27 +01:00
-c Path to the configuration file (default: ./config.yaml)
'''.format(sys.argv[0]))
2017-10-30 02:59:35 +01:00
return
2017-11-03 01:56:27 +01:00
config = {}
with open(config_file,'r') as f:
config = yaml.load(f)
API_KEY = config['pushbullet_token']
DEVICE_ID = config['device_id'] \
if 'device_id' in config else socket.gethostname()
if 'debug' in config:
DEBUG = config['debug']
if DEBUG:
logging.basicConfig(level=logging.DEBUG)
websocket.enableTrace(True)
else:
logging.basicConfig(level=logging.INFO)
ws = websocket.WebSocketApp('wss://stream.pushbullet.com/websocket/' +
API_KEY,
on_message = on_push,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
2017-10-29 05:17:42 +01:00
if __name__ == '__main__':
main()
2017-10-29 20:45:19 +01:00