ClipSync/client.py

86 lines
2.5 KiB
Python

"""
This script is a daemon that, on event, send and sync the clipboard with a
distant one
"""
import threading
import time
import sys
import json
import pyperclip
import requests
import socketio
import zc.lockfile
import notification as notif
# to put in a conf file
ip = 'localhost'
port = "9564"
hostname = "WarMachine"
username = "neotaku67"
password = "un bon mot de passe de prefererance mais en sah tant qu'il est hashe ca passe"
sign = "[AllSync] "
ip = f"http://{ip}:{port}"
sio = socketio.Client()
sio.connect(ip)
print("[Debug] Connected to Server .w.")
auth = requests.post(f"{ip}/user",
data={"username": username, "password": password},
timeout=10000)
if auth.status_code != 200:
print("invalid credentials")
sys.exit()
token = json.loads(auth.content.decode())['token']
notify_stop_event = threading.Event()
# clipboard_stop_event = threading.Event()
def send_notification():
notif.start_monitoring()
while not notify_stop_event.is_set():
while not notif.notification_queue.empty():
notification = notif.notification_queue.get()
if notification.title.find(sign) == -1:
continue
requests.put(f"{ip}/notification",
data={"token": token,
"title": notification.title,
"content": notification.content,
"deviceName": hostname},
timeout=5000)
time.sleep(1)
notification_thread = threading.Thread(target=send_notification)
# clipboard_thread = threading.Thread(target=)
@sio.event
def NotificationUpdate(data):
if data["device_name"] == hostname:
return
response = requests.get(f"{ip}/notification/-1?token={token}",
timeout=2000)
response = json.loads(response.content.decode())["notifications"]
notification = notif.Notification(title=sign+response["title"],
content=response["content"])
notification.show()
print("[NotificationEvent] received data")
@sio.event
def ClipboardUpdate(data):
if data["device_name"] == hostname:
return
clipboard = requests.get(f"{ip}/clipboard/-1?token={token}",
timeout=2000)
clipboard = json.loads(clipboard.content.decode())
clipboard = clipboard["clipboard"]
pyperclip.copy(clipboard)
print("[ClipboardEvent] received data")
if __name__ == "__main__":
notification_thread.start()
sio.wait()