From d9b51c94c033b24509d7eb15a42020c9c92fd495 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 14 Jun 2018 15:35:15 +0000 Subject: [PATCH] Added Pushbullet plugin to send pushes and files --- platypush/plugins/pushbullet.py | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 platypush/plugins/pushbullet.py diff --git a/platypush/plugins/pushbullet.py b/platypush/plugins/pushbullet.py new file mode 100644 index 000000000..1d4eac3d0 --- /dev/null +++ b/platypush/plugins/pushbullet.py @@ -0,0 +1,70 @@ +import json +import os +import requests + +from platypush.context import get_backend +from platypush.message.response import Response +from platypush.plugins import Plugin + + +class PushbulletPlugin(Plugin): + def send_push(self, **kwargs): + pushbullet = get_backend('pushbullet') + resp = requests.post('https://api.pushbullet.com/v2/ephemerals', + data=json.dumps({ + 'type':'push', + 'push': kwargs, + }), + + headers={'Authorization': 'Bearer ' + pushbullet.token, + 'Content-Type': 'application/json'}) + + if resp.status_code >= 400: + raise Exception('Pushbullet push failed with status {}: {}'. + format(resp.status_code, resp.json())) + + return Response(output={'status':'ok'}) + + + def send_file(self, filename): + pushbullet = get_backend('pushbullet') + resp = requests.post('https://api.pushbullet.com/v2/upload-request', + data=json.dumps({'file_name': os.path.basename(filename)}), + headers={'Authorization': 'Bearer ' + pushbullet.token, + 'Content-Type': 'application/json'}) + + if resp.status_code != 200: + raise Exception('Pushbullet file upload request failed with status {}'. + format(resp.status_code)) + + r = resp.json() + resp = requests.post(r['upload_url'], data=r['data'], + files={'file': open(filename, 'rb')}) + + if resp.status_code != 204: + raise Exception('Pushbullet file upload failed with status {}'. + format(resp.status_code)) + + resp = requests.post('https://api.pushbullet.com/v2/pushes', + headers={'Authorization': 'Bearer ' + pushbullet.token, + 'Content-Type': 'application/json'}, + + data=json.dumps({ + 'type': 'file', + 'file_name': r['file_name'], + 'file_type': r['file_type'], + 'file_url': r['file_url'] })) + + if resp.status_code >= 400: + raise Exception('Pushbullet file push failed with status {}'. + format(resp.status_code)) + + return Response(output={ + 'filename': r['file_name'], + 'type': r['file_type'], + 'url': r['file_url'] + }) + + +# vim:sw=4:ts=4:et: +