platypush/runbullet/bin/pusher

77 lines
1.8 KiB
Text
Raw Normal View History

2017-11-03 15:06:29 +01:00
#!python
2017-11-03 11:34:26 +01:00
import json
import os
import sys
import yaml
from getopt import getopt
from pushbullet import Pushbullet
2017-11-03 15:06:29 +01:00
from runbullet import parse_config_file
2017-11-03 11:34:26 +01:00
def print_usage():
print ('''Usage: {} [-h|--help] <-t|--target <target name>> <-a|--action <action name>> payload
2017-11-03 11:34:26 +01:00
-h, --help:\t\tShow this help and exit
-t, --target:\tName of the target device/host
-a, --action\tAction to run, it includes both the package name and the method (e.g. shell.exec or music.mpd.play)
payload:\t\tArguments to the action
2017-11-03 11:34:26 +01:00
'''.format(sys.argv[0]))
def main():
2017-11-03 15:06:29 +01:00
config = parse_config_file()
2017-11-03 11:34:26 +01:00
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:a:',
['help', 'target=', 'action='])
2017-11-03 11:34:26 +01:00
target = None
action = None
2017-11-03 11:34:26 +01:00
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 == 'a' or opt == '--action':
action = arg
2017-11-03 11:34:26 +01:00
if len(args):
payload = json.loads(args[0])
else:
payload = json.loads(sys.stdin.read())
2017-11-03 11:34:26 +01:00
if not (target and action):
2017-11-03 11:34:26 +01:00
print_usage()
return
msg = {
'target': target,
'action': action,
**payload,
2017-11-03 11:34:26 +01:00
}
print('msg: {}'.format(msg))
2017-11-03 11:34:26 +01:00
pb.push_note('', json.dumps(msg), device)
if __name__ == '__main__':
main()
# vim:sw=4:ts=4:et: