From 1bb0527adc5f41d16d65ac30733cac47caba4cd1 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 3 Nov 2017 11:34:26 +0100 Subject: [PATCH] New script for pushing messages --- pusher.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 pusher.py diff --git a/pusher.py b/pusher.py new file mode 100644 index 00000000..1aaddeb6 --- /dev/null +++ b/pusher.py @@ -0,0 +1,81 @@ +import json +import os +import sys +import yaml + +from getopt import getopt +from pushbullet import Pushbullet + +def get_config(): + curdir = os.path.dirname(os.path.realpath(__file__)) + config_file = curdir + os.sep + 'config.yaml' + config = {} + + with open(config_file,'r') as f: + config = yaml.load(f) + + return config + +def print_usage(): + print ('''Usage: {} [-h|--help] <-t|--target > <-p|--plugin > payload + -h, --help:\t\tShow this help and exit + -t, --target:\tName of the target device/host + -p, --plugin:\tPlugin to use (e.g. shell or music.mpd) + payload:\t\tArguments to the plugin +'''.format(sys.argv[0])) + +def main(): + config = get_config() + API_KEY = config['pushbullet']['token'] + pb = Pushbullet(API_KEY) + + devices = [ + _ for _ in pb.devices if _.nickname == config['pushbullet']['device'] + ] + + if len(devices) > 0: + device = devices[0] + else: + print('Device {} not found - please create a virtual device on ' + + 'your PushBullet account'.format(config['pushbullet']['device'])) + return + + optlist, args = getopt(sys.argv[1:], 'ht:p:', + ['help', 'target=', 'plugin=']) + + target = None + plugin = None + payload = {} + + for opt, arg in optlist: + if opt == 'h' or opt == '--help': + print_usage() + return + elif opt == 't' or opt == '--target': + target = arg + elif opt == 'p' or opt == '--plugin': + plugin = arg + + if len(args): + payload = json.loads(args[0]) + else: + payload = sys.stdin.read() + + if not (target and plugin): + print_usage() + return + + msg = { + 'target': target, + 'plugin': plugin, + 'args': payload, + } + + pb.push_note('', json.dumps(msg), device) + + +if __name__ == '__main__': + main() + +# vim:sw=4:ts=4:et: +