Added clipboard backend
This commit is contained in:
parent
b5f0e2b4df
commit
82a9aa1232
9 changed files with 95 additions and 11 deletions
|
@ -19,6 +19,7 @@ Backends
|
||||||
platypush/backend/button.flic.rst
|
platypush/backend/button.flic.rst
|
||||||
platypush/backend/camera.pi.rst
|
platypush/backend/camera.pi.rst
|
||||||
platypush/backend/chat.telegram.rst
|
platypush/backend/chat.telegram.rst
|
||||||
|
platypush/backend/clipboard.rst
|
||||||
platypush/backend/covid19.rst
|
platypush/backend/covid19.rst
|
||||||
platypush/backend/foursquare.rst
|
platypush/backend/foursquare.rst
|
||||||
platypush/backend/google.fit.rst
|
platypush/backend/google.fit.rst
|
||||||
|
|
|
@ -15,6 +15,7 @@ Events
|
||||||
platypush/events/button.flic.rst
|
platypush/events/button.flic.rst
|
||||||
platypush/events/camera.rst
|
platypush/events/camera.rst
|
||||||
platypush/events/chat.telegram.rst
|
platypush/events/chat.telegram.rst
|
||||||
|
platypush/events/clipboard.rst
|
||||||
platypush/events/covid19.rst
|
platypush/events/covid19.rst
|
||||||
platypush/events/distance.rst
|
platypush/events/distance.rst
|
||||||
platypush/events/foursquare.rst
|
platypush/events/foursquare.rst
|
||||||
|
|
5
docs/source/platypush/backend/clipboard.rst
Normal file
5
docs/source/platypush/backend/clipboard.rst
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
``platypush.backend.clipboard``
|
||||||
|
===============================
|
||||||
|
|
||||||
|
.. automodule:: platypush.backend.clipboard
|
||||||
|
:members:
|
5
docs/source/platypush/events/clipboard.rst
Normal file
5
docs/source/platypush/events/clipboard.rst
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
``platypush.message.event.clipboard``
|
||||||
|
=====================================
|
||||||
|
|
||||||
|
.. automodule:: platypush.message.event.clipboard
|
||||||
|
:members:
|
34
platypush/backend/clipboard.py
Normal file
34
platypush/backend/clipboard.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import pyperclip
|
||||||
|
|
||||||
|
from platypush.backend import Backend
|
||||||
|
from platypush.message.event.clipboard import ClipboardEvent
|
||||||
|
|
||||||
|
|
||||||
|
class ClipboardBackend(Backend):
|
||||||
|
"""
|
||||||
|
This backend monitors for changes in the clipboard and generates even when the user copies a new text.
|
||||||
|
|
||||||
|
Requires:
|
||||||
|
|
||||||
|
- **pyperclip** (``pip install pyperclip``)
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self._last_text: Optional[str] = None
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while not self.should_stop():
|
||||||
|
text = pyperclip.paste()
|
||||||
|
if text and text != self._last_text:
|
||||||
|
self.bus.post(ClipboardEvent(text=text))
|
||||||
|
|
||||||
|
self._last_text = text
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
# vim:sw=4:ts=4:et:
|
9
platypush/message/event/clipboard.py
Normal file
9
platypush/message/event/clipboard.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
from platypush.message.event import Event
|
||||||
|
|
||||||
|
|
||||||
|
class ClipboardEvent(Event):
|
||||||
|
def __init__(self, text: str, *args, **kwargs):
|
||||||
|
super().__init__(*args, text=text, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# vim:sw=4:ts=4:et:
|
|
@ -1,5 +1,7 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from platypush.context import get_backend
|
from platypush.context import get_backend
|
||||||
|
@ -18,12 +20,12 @@ class PushbulletPlugin(Plugin):
|
||||||
* The :class:`platypush.backend.pushbullet.Pushbullet` backend enabled
|
* The :class:`platypush.backend.pushbullet.Pushbullet` backend enabled
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, token: str = None, *args, **kwargs):
|
def __init__(self, token: str = None, **kwargs):
|
||||||
"""
|
"""
|
||||||
:param token: Pushbullet API token. If not set the plugin will try to retrieve it from
|
:param token: Pushbullet API token. If not set the plugin will try to retrieve it from
|
||||||
the Pushbullet backend configuration, if available
|
the Pushbullet backend configuration, if available
|
||||||
"""
|
"""
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
if not token:
|
if not token:
|
||||||
backend = get_backend('pushbullet')
|
backend = get_backend('pushbullet')
|
||||||
|
@ -124,7 +126,6 @@ class PushbulletPlugin(Plugin):
|
||||||
if not device:
|
if not device:
|
||||||
raise RuntimeError('No such device')
|
raise RuntimeError('No such device')
|
||||||
|
|
||||||
pushbullet = get_backend('pushbullet')
|
|
||||||
resp = requests.post('https://api.pushbullet.com/v2/upload-request',
|
resp = requests.post('https://api.pushbullet.com/v2/upload-request',
|
||||||
data=json.dumps({'file_name': os.path.basename(filename)}),
|
data=json.dumps({'file_name': os.path.basename(filename)}),
|
||||||
headers={'Authorization': 'Bearer ' + self.token,
|
headers={'Authorization': 'Bearer ' + self.token,
|
||||||
|
@ -151,7 +152,7 @@ class PushbulletPlugin(Plugin):
|
||||||
'device_iden': device['iden'] if device else None,
|
'device_iden': device['iden'] if device else None,
|
||||||
'file_name': r['file_name'],
|
'file_name': r['file_name'],
|
||||||
'file_type': r['file_type'],
|
'file_type': r['file_type'],
|
||||||
'file_url': r['file_url'] }))
|
'file_url': r['file_url']}))
|
||||||
|
|
||||||
if resp.status_code >= 400:
|
if resp.status_code >= 400:
|
||||||
raise Exception('Pushbullet file push failed with status {}'.
|
raise Exception('Pushbullet file push failed with status {}'.
|
||||||
|
@ -163,6 +164,29 @@ class PushbulletPlugin(Plugin):
|
||||||
'url': r['file_url']
|
'url': r['file_url']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
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'})
|
||||||
|
|
||||||
|
resp.raise_for_status()
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
|
@ -264,3 +264,6 @@ croniter
|
||||||
|
|
||||||
# Support for SSH integration
|
# Support for SSH integration
|
||||||
# paramiko
|
# paramiko
|
||||||
|
|
||||||
|
# Support for clipboard integration
|
||||||
|
# pyperclip
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -301,5 +301,7 @@ setup(
|
||||||
'samsungtv': ['samsungtvws'],
|
'samsungtv': ['samsungtvws'],
|
||||||
# Support for SSH integration
|
# Support for SSH integration
|
||||||
'ssh': ['paramiko'],
|
'ssh': ['paramiko'],
|
||||||
|
# Support for clipboard integration
|
||||||
|
'clipboard': ['pyperclip'],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue