platypush/platypush/plugins/pushbullet.py

193 lines
6.4 KiB
Python
Raw Normal View History

import json
import os
2020-05-23 23:11:42 +02:00
from typing import Optional
import requests
from platypush.context import get_backend
from platypush.plugins import Plugin, action
class PushbulletPlugin(Plugin):
2018-06-25 19:57:43 +02:00
"""
This plugin allows you to send pushes and files to your PushBullet account.
Note: This plugin will only work if the :mod:`platypush.backend.pushbullet`
backend is configured.
Requires:
* **requests** (``pip install requests``)
* The :class:`platypush.backend.pushbullet.Pushbullet` backend enabled
2018-06-25 19:57:43 +02:00
"""
2020-05-23 23:11:42 +02:00
def __init__(self, token: str = None, **kwargs):
2019-12-09 12:00:31 +01:00
"""
:param token: Pushbullet API token. If not set the plugin will try to retrieve it from
the Pushbullet backend configuration, if available
"""
2020-05-23 23:11:42 +02:00
super().__init__(**kwargs)
2019-12-09 12:00:31 +01:00
if not token:
backend = get_backend('pushbullet')
if not backend or not backend.token:
raise AttributeError('No Pushbullet token specified')
self.token = backend.token
else:
self.token = token
self._devices = []
self._devices_by_id = {}
self._devices_by_name = {}
@action
def get_devices(self):
"""
Get the list of available devices
"""
resp = requests.get('https://api.pushbullet.com/v2/devices',
2020-05-23 23:11:42 +02:00
headers={'Authorization': 'Bearer ' + self.token,
'Content-Type': 'application/json'})
2019-12-09 12:00:31 +01:00
self._devices = resp.json().get('devices', [])
self._devices_by_id = {
2020-05-23 23:11:42 +02:00
dev['iden']: dev
for dev in self._devices
2019-12-09 12:00:31 +01:00
}
self._devices_by_name = {
2020-05-23 23:11:42 +02:00
dev['nickname']: dev
for dev in self._devices if 'nickname' in dev
2019-12-09 12:00:31 +01:00
}
@action
2019-12-09 12:00:31 +01:00
def get_device(self, device):
"""
:param device: Device ID or name
"""
output = None
refreshed = False
while not output:
if device in self._devices_by_id:
return self._devices_by_id[device]
if device in self._devices_by_name:
return self._devices_by_name[device]
if refreshed:
return None
self.get_devices()
refreshed = True
@action
def send_note(self, device: str = None, body: str = None, title: str = None, url: str = None, **kwargs):
2018-06-25 19:57:43 +02:00
"""
2018-10-01 15:06:26 +02:00
Send a note push.
2018-06-25 19:57:43 +02:00
2019-12-09 12:00:31 +01:00
:param device: Device ID or name (default: None, all devices)
:param body: Note body
:param title: Note title
:param url: URL attached to the note
2018-06-25 19:57:43 +02:00
:param kwargs: Push arguments, see https://docs.pushbullet.com/#create-push
"""
2019-12-09 12:00:31 +01:00
if device:
device = self.get_device(device).output
if not device:
raise RuntimeError('No such device')
2018-10-01 15:06:26 +02:00
kwargs['body'] = body
kwargs['title'] = title
2019-12-09 12:00:31 +01:00
kwargs['type'] = 'link' if url else 'note'
if device:
kwargs['device_iden'] = device['iden']
2018-10-01 15:06:26 +02:00
resp = requests.post('https://api.pushbullet.com/v2/pushes',
data=json.dumps(kwargs),
2019-12-09 12:00:31 +01:00
headers={'Authorization': 'Bearer ' + self.token,
'Content-Type': 'application/json'})
if resp.status_code >= 400:
raise Exception('Pushbullet push failed with status {}: {}'.
format(resp.status_code, resp.json()))
@action
2019-12-09 12:00:31 +01:00
def send_file(self, filename: str, device: str = None):
2018-06-25 19:57:43 +02:00
"""
Send a file.
2019-12-09 12:00:31 +01:00
:param device: Device ID or name (default: None, all devices)
2018-06-25 19:57:43 +02:00
:param filename: Path to the local file
"""
2019-12-09 12:00:31 +01:00
if device:
device = self.get_device(device).output
if not device:
raise RuntimeError('No such device')
resp = requests.post('https://api.pushbullet.com/v2/upload-request',
data=json.dumps({'file_name': os.path.basename(filename)}),
2019-12-09 12:00:31 +01:00
headers={'Authorization': 'Bearer ' + self.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',
2019-12-09 12:00:31 +01:00
headers={'Authorization': 'Bearer ' + self.token,
'Content-Type': 'application/json'},
data=json.dumps({
'type': 'file',
2019-12-09 12:00:31 +01:00
'device_iden': device['iden'] if device else None,
'file_name': r['file_name'],
'file_type': r['file_type'],
2020-05-23 23:11:42 +02:00
'file_url': r['file_url']}))
if resp.status_code >= 400:
raise Exception('Pushbullet file push failed with status {}'.
format(resp.status_code))
return {
'filename': r['file_name'],
'type': r['file_type'],
'url': r['file_url']
}
2020-05-23 23:11:42 +02:00
@action
def send_clipboard(self, text: str):
"""
Copy text to the clipboard of a device.
:param text: Text to be copied.
"""
backend = get_backend('pushbullet')
device_id = backend.get_device_id() if backend else None
2020-05-23 23:11:42 +02:00
resp = requests.post('https://api.pushbullet.com/v2/ephemerals',
data=json.dumps({
'type': 'push',
'push': {
'body': text,
'type': 'clip',
'source_device_iden': device_id,
},
}),
headers={'Authorization': 'Bearer ' + self.token,
'Content-Type': 'application/json'})
2020-05-23 23:11:42 +02:00
resp.raise_for_status()
# vim:sw=4:ts=4:et: