first commit

This commit is contained in:
Soph :3 2024-08-02 18:08:48 +03:00
commit 0480c2ddd0
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
2 changed files with 88 additions and 0 deletions

41
main.py Normal file
View file

@ -0,0 +1,41 @@
import asyncio, time, json, discordrpc, socket
from websockets.server import serve
def is_port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
if(is_port_in_use(52121)):
exit(1)
rpc = discordrpc.RPC(app_id="863873014587326494")
rpc.set_activity(details="Not doing.. anything..", large_image="small")
lastMessage = 0;
async def echo(websocket):
global lastMessage;
async for message in websocket:
data = json.loads(message);
rpc.set_activity(state=f"Cursor position: {data["cursorPosition"]}", details=f"{data["name"]}", large_image="small")
lastMessage = time.time()
async def main():
print("started ws")
loop = asyncio.get_event_loop()
async def heartbeatChecker():
if lastMessage != 0:
if time.time()-lastMessage > 5:
exit(0)
loop.call_later(2, lambda: asyncio.ensure_future(heartbeatChecker()))
await heartbeatChecker();
async with await serve(echo, "localhost", 52121):
await asyncio.Future()
asyncio.run(main())

47
main.qml Normal file
View file

@ -0,0 +1,47 @@
import QOwnNotesTypes 1.0
import QtQml 2.0
import QtWebSockets 1.1
Script {
property string scriptDirPath
property QtObject timer
property QtObject websocket
websocket: WebSocket {
id: socket
url: "ws://localhost:52121"
onTextMessageReceived: {
script.log("Received message: " + message);
}
onStatusChanged: {
if (socket.status == WebSocket.Error)
script.log("Error: " + socket.errorString);
else if (socket.status == WebSocket.Open)
script.log("Open!");
else if (socket.status == WebSocket.Closed)
script.log("Socket closed");
}
active: true
}
function init() {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath);
}
timer: Timer {
interval: 1000
running: true
repeat: true
onTriggered: {
var note = script.currentNote();
websocket.sendTextMessage(JSON.stringify({
name: note.name,
cursorPosition: script.noteTextEditCursorPosition(),
}))
}
}
}