Using websockets for real time event streams instead of pushbullet.py
This commit is contained in:
parent
5c4ac57e2d
commit
9f48b010b5
1 changed files with 62 additions and 34 deletions
96
notifier.py
96
notifier.py
|
@ -3,66 +3,94 @@
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import websocket
|
||||||
|
|
||||||
from pushbullet import Listener
|
from getopt import getopt
|
||||||
from pushbullet import Pushbullet
|
|
||||||
|
|
||||||
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
|
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
|
||||||
|
|
||||||
def get_hostname():
|
|
||||||
with open('/etc/hostname','r') as fp:
|
|
||||||
return fp.readline().strip()
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
hostname = get_hostname()
|
|
||||||
|
|
||||||
API_KEY = 'o.EHMMnZneJdpNQv9FSFbyY2busin7floe'
|
API_KEY = 'o.EHMMnZneJdpNQv9FSFbyY2busin7floe'
|
||||||
DEVICE_ID = 'ujBKDt14CaGsjAiAm2WPVA'
|
DEVICE_ID = socket.gethostname()
|
||||||
HTTP_PROXY_HOST = None
|
DEBUG = False
|
||||||
HTTP_PROXY_PORT = None
|
|
||||||
|
|
||||||
|
|
||||||
def on_push(data):
|
def on_open(ws):
|
||||||
global hostname
|
logging.info('Connection opened')
|
||||||
|
|
||||||
if 'type' not in data \
|
|
||||||
or 'push' not in data \
|
def on_close(ws):
|
||||||
or data['type'] != 'push':
|
logging.info('Connection closed')
|
||||||
|
|
||||||
|
|
||||||
|
def on_error(ws, error):
|
||||||
|
logging.error(error)
|
||||||
|
|
||||||
|
|
||||||
|
def _on_push(ws, data):
|
||||||
|
data = json.loads(data)
|
||||||
|
if data['type'] == 'tickle' and data['subtype'] == 'push':
|
||||||
|
logging.debug('Received push tickle')
|
||||||
|
return
|
||||||
|
|
||||||
|
if data['type'] != 'push':
|
||||||
return # Not a push notification
|
return # Not a push notification
|
||||||
|
|
||||||
push = data['push']
|
push = data['push']
|
||||||
logging.debug('Received push: {}'.format(push))
|
logging.debug('Received push: {}'.format(push))
|
||||||
|
|
||||||
if 'body' not in push:
|
|
||||||
return
|
|
||||||
|
|
||||||
body = push['body']
|
body = push['body']
|
||||||
try:
|
try:
|
||||||
body = json.loads(body)
|
body = json.loads(body)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return
|
return
|
||||||
|
|
||||||
if 'target' not in body or body['target'] != hostname:
|
if 'target' not in body or body['target'] != DEVICE_ID:
|
||||||
return # Not for me
|
return # Not for me
|
||||||
|
|
||||||
logging.info('Received message: {}'.format(body))
|
logging.info('Received push addressed to me: {}'.format(body))
|
||||||
if 'cmd' in body:
|
if 'exec' in body:
|
||||||
logging.info('Executing command: {}'.format(body['cmd']))
|
logging.info('Executing command: {}'.format(body['exec']))
|
||||||
os.system(body['cmd'])
|
os.system(body['exec'])
|
||||||
|
|
||||||
|
|
||||||
|
def on_push(ws, data):
|
||||||
|
try:
|
||||||
|
_on_push(ws, data)
|
||||||
|
except Error as e:
|
||||||
|
on_error(e)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pb = Pushbullet(API_KEY)
|
global DEBUG
|
||||||
|
|
||||||
s = Listener(account=pb,
|
optlist, args = getopt(sys.argv[1:], 'vh')
|
||||||
on_push=on_push,
|
for opt, arg in optlist:
|
||||||
http_proxy_host=HTTP_PROXY_HOST,
|
if opt == '-v':
|
||||||
http_proxy_port=HTTP_PROXY_PORT)
|
DEBUG = True
|
||||||
try:
|
elif opt == '-h':
|
||||||
s.run_forever()
|
print('''
|
||||||
except KeyboardInterrupt:
|
Usage: {} [-v] [-h]
|
||||||
s.close()
|
-v Enable debug mode
|
||||||
|
-h Show this help
|
||||||
|
'''.format(sys.argv[0]))
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue