32 lines
971 B
Python
32 lines
971 B
Python
import json
|
|
import socket
|
|
|
|
|
|
class MpvClient:
|
|
"""Low-level mpv IPC client. Knows nothing about curses or UI."""
|
|
|
|
def __init__(self, socket_path):
|
|
self.socket_path = socket_path
|
|
|
|
def _send(self, data):
|
|
try:
|
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
|
|
sock.settimeout(2)
|
|
sock.connect(self.socket_path)
|
|
payload = json.dumps(data) + "\n"
|
|
sock.sendall(payload.encode())
|
|
response = sock.recv(8192).decode()
|
|
if response:
|
|
return json.loads(response)
|
|
return None
|
|
except Exception:
|
|
return None
|
|
|
|
def command(self, *args):
|
|
return self._send({"command": list(args)})
|
|
|
|
def get_property(self, prop):
|
|
result = self._send({"command": ["get_property", prop]})
|
|
if result and "data" in result:
|
|
return result["data"]
|
|
return None
|