Don't create new socket for every MPD query

This commit is contained in:
siikamiika 2014-03-08 03:05:01 +02:00
parent 07fbfc6640
commit 850501430d

View File

@ -41,6 +41,8 @@ class MPD(IntervalModule):
host = "localhost" host = "localhost"
port = 6600 port = 6600
s = socket.create_connection((host, port))
s.recv(8192)
format = "{title} {status}" format = "{title} {status}"
format_sparse = None format_sparse = None
status = { status = {
@ -65,33 +67,29 @@ class MPD(IntervalModule):
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: fdict = {}
# Skip "OK MPD ..."
s.recv(8192)
fdict = {} status = self._mpd_command(self.s, "status")
currentsong = self._mpd_command(self.s, "currentsong")
status = self._mpd_command(s, "status") fdict = {
currentsong = self._mpd_command(s, "currentsong") "pos": int(status.get("song", 0)) + 1,
"len": int(status["playlistlength"]),
"status": self.status[status["state"]],
"volume": int(status["volume"]),
fdict = { "title": currentsong.get("Title", ""),
"pos": int(status.get("song", 0)) + 1, "album": currentsong.get("Album", ""),
"len": int(status["playlistlength"]), "artist": currentsong.get("Artist", ""),
"status": self.status[status["state"]], "song_length": TimeWrapper(currentsong.get("Time", 0)),
"volume": int(status["volume"]), "song_elapsed": TimeWrapper(float(status.get("elapsed", 0))),
"bitrate": int(status.get("bitrate", 0)),
"title": currentsong.get("Title", ""), }
"album": currentsong.get("Album", ""),
"artist": currentsong.get("Artist", ""),
"song_length": TimeWrapper(currentsong.get("Time", 0)),
"song_elapsed": TimeWrapper(float(status.get("elapsed", 0))),
"bitrate": int(status.get("bitrate", 0)),
} self.output = {
"full_text": formatp(self.format, **fdict).strip(),
self.output = { }
"full_text": formatp(self.format, **fdict).strip(),
}
def on_leftclick(self): def on_leftclick(self):
with socket.create_connection(("localhost", self.port)) as s: with socket.create_connection(("localhost", self.port)) as s: