Merge pull request #39 from siikamiika/master
Don't create new socket for every MPD query
This commit is contained in:
commit
3a79ed5dc7
@ -41,6 +41,7 @@ class MPD(IntervalModule):
|
|||||||
|
|
||||||
host = "localhost"
|
host = "localhost"
|
||||||
port = 6600
|
port = 6600
|
||||||
|
s = None
|
||||||
format = "{title} {status}"
|
format = "{title} {status}"
|
||||||
format_sparse = None
|
format_sparse = None
|
||||||
status = {
|
status = {
|
||||||
@ -52,28 +53,31 @@ class MPD(IntervalModule):
|
|||||||
vol = 100
|
vol = 100
|
||||||
|
|
||||||
def _mpd_command(self, sock, command):
|
def _mpd_command(self, sock, command):
|
||||||
|
try:
|
||||||
sock.send((command + "\n").encode("utf-8"))
|
sock.send((command + "\n").encode("utf-8"))
|
||||||
|
except Exception as e:
|
||||||
|
self.s = socket.create_connection((self.host, self.port))
|
||||||
|
sock = self.s
|
||||||
|
sock.recv(8192)
|
||||||
|
sock.send((command + "\n").encode("utf-8"))
|
||||||
|
try:
|
||||||
reply = sock.recv(16384).decode("utf-8")
|
reply = sock.recv(16384).decode("utf-8")
|
||||||
replylines = reply.split("\n")[:-2]
|
replylines = reply.split("\n")[:-2]
|
||||||
|
|
||||||
return dict(
|
return dict(
|
||||||
(line.split(": ", 1)[0], line.split(": ", 1)[1]) for line in replylines
|
(line.split(": ", 1)) for line in replylines
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return None
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
if not self.format_sparse:
|
if not self.format_sparse:
|
||||||
self.format_sparse = self.format
|
self.format_sparse = self.format
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
with socket.create_connection((self.host, self.port)) as s:
|
try:
|
||||||
# Skip "OK MPD ..."
|
status = self._mpd_command(self.s, "status")
|
||||||
s.recv(8192)
|
currentsong = self._mpd_command(self.s, "currentsong")
|
||||||
|
|
||||||
fdict = {}
|
|
||||||
|
|
||||||
status = self._mpd_command(s, "status")
|
|
||||||
currentsong = self._mpd_command(s, "currentsong")
|
|
||||||
|
|
||||||
fdict = {
|
fdict = {
|
||||||
"pos": int(status.get("song", 0)) + 1,
|
"pos": int(status.get("song", 0)) + 1,
|
||||||
"len": int(status["playlistlength"]),
|
"len": int(status["playlistlength"]),
|
||||||
@ -88,25 +92,26 @@ class MPD(IntervalModule):
|
|||||||
"bitrate": int(status.get("bitrate", 0)),
|
"bitrate": int(status.get("bitrate", 0)),
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.output = {
|
self.output = {
|
||||||
"full_text": formatp(self.format, **fdict).strip(),
|
"full_text": formatp(self.format, **fdict).strip(),
|
||||||
}
|
}
|
||||||
|
except Exception as e:
|
||||||
|
self.output = {"full_text": "error connecting MPD"}
|
||||||
|
|
||||||
def on_leftclick(self):
|
def on_leftclick(self):
|
||||||
with socket.create_connection(("localhost", self.port)) as s:
|
try:
|
||||||
s.recv(8192)
|
self._mpd_command(self.s, "pause %i" %
|
||||||
|
(0 if self._mpd_command(self.s, "status")["state"] == "pause" else 1))
|
||||||
self._mpd_command(s, "pause %i" %
|
except Exception as e:
|
||||||
(0 if self._mpd_command(s, "status")["state"] == "pause" else 1))
|
pass
|
||||||
|
|
||||||
def on_rightclick(self):
|
def on_rightclick(self):
|
||||||
with socket.create_connection(("localhost", self.port)) as s:
|
try:
|
||||||
s.recv(8192)
|
vol = int(self._mpd_command(self.s, "status")["volume"])
|
||||||
|
|
||||||
vol = int(self._mpd_command(s, "status")["volume"])
|
|
||||||
if vol == 0:
|
if vol == 0:
|
||||||
self._mpd_command(s, "setvol %i" % self.vol)
|
self._mpd_command(self.s, "setvol %i" % self.vol)
|
||||||
else:
|
else:
|
||||||
self.vol = vol
|
self.vol = vol
|
||||||
self._mpd_command(s, "setvol 0")
|
self._mpd_command(self.s, "setvol 0")
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user