platypush/notifier.py

71 lines
1.5 KiB
Python
Raw Normal View History

2017-10-29 05:17:42 +01:00
#!/usr/bin/env python
import os
import logging
import json
import subprocess
from pushbullet import Listener
from pushbullet import Pushbullet
__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'
2017-10-30 01:52:58 +01:00
DEVICE_ID = 'ujBKDt14CaGsjAiAm2WPVA'
2017-10-29 05:17:42 +01:00
HTTP_PROXY_HOST = None
HTTP_PROXY_PORT = None
def on_push(data):
global hostname
if 'type' not in data \
or 'push' not in data \
or data['type'] != 'push':
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'] != hostname:
return # Not for me
logging.info('Received message: {}'.format(body))
if 'cmd' in body:
logging.info('Executing command: {}'.format(body['cmd']))
os.system(body['cmd'])
def main():
pb = Pushbullet(API_KEY)
s = Listener(account=pb,
on_push=on_push,
http_proxy_host=HTTP_PROXY_HOST,
http_proxy_port=HTTP_PROXY_PORT)
try:
s.run_forever()
except KeyboardInterrupt:
s.close()
if __name__ == '__main__':
main()
2017-10-29 20:45:19 +01:00