QOwnNotesDiscordRPC/main.py

41 lines
1.1 KiB
Python
Raw Normal View History

2024-08-02 15:08:48 +00:00
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())