def sendline(): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(("localhost", 6600)) except ConnectionRefusedError: time.sleep(1) return except OSError: time.sleep(1) return try: version = sock.recv(2048) except InterruptedError: pass assert version == b"OK MPD 0.19.0\n" sock.send(b"currentsong\n") currsong = mpd2dict(sock.recv(2048).decode("UTF-8")) if currsong == {}: return sock.send(b"status\n") status = mpd2dict(sock.recv(2048).decode("UTF-8")) infodict = currsong.copy() infodict.update(status) artistcolour = "#a1b56c" titlecolour = "#ac4142" albumcolour = "#6a9fb5" if infodict["state"] == "pause": titlecolour = darken(titlecolour) albumcolour = darken(albumcolour) artistcolour = darken(artistcolour) block = "<span foreground='{}'>{}</span>" for item in ["Artist", "Title", "Album"]: if item not in infodict: infodict[item] = "Unknown {}".format(item) fmline = "{} - {} - {}".format( block.format(artistcolour, infodict["Artist"]), block.format(titlecolour, infodict["Title"]), block.format(albumcolour, infodict["Album"]), ) formatcodes = fmline.replace("&", "&") linelib.sendblock(ID, {"full_text": formatcodes, "markup": "pango"}) linelib.sendPID(ID) linelib.waitsig(1) click = linelib.getclick(ID).decode("UTF-8") if click != "": x = json.loads(click) if x["button"] == 1: sock.send(b"pause\n")
def sendline(): try: x = r.get_unread(fetch=True) except requests.exceptions.HTTPError: #happens when reddit returns a 503. It should come back up soon. return x = list(x) if len(x) > 0: linelib.sendblock(ID, { "full_text": "[{}]".format(str(len(x))), "color": "#ff4500" }) linelib.sendPID(ID) linelib.waitsig(5)
import signal import linelib import subprocess def handler(x, y): pass signal.signal(signal.SIGUSR1, handler) signal.signal(signal.SIGALRM, handler) ID = "ping" toping = "8.8.8.8" while True: try: raw = subprocess.check_output(["ping", "-c1", toping]) line = raw.decode("UTF8").split("\n")[1] time = line.split(" ")[6][5:] linelib.sendblock(ID, {"full_text": "{}ms".format(time)}) linelib.sendPID(ID) linelib.waitsig(5) except subprocess.CalledProcessError: linelib.sendblock(ID, { "full_text": "NOT CONNECTED".format(time), "color": "#ff0000" }) linelib.waitsig(1)