diff options
Diffstat (limited to 'deckit/modules/music.py')
| -rw-r--r-- | deckit/modules/music.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/deckit/modules/music.py b/deckit/modules/music.py index af13688..f0baacb 100644 --- a/deckit/modules/music.py +++ b/deckit/modules/music.py @@ -2,7 +2,7 @@ import subprocess -from ..module import Module +from ..module import Module, ModuleError SEP = "\x1f" @@ -29,6 +29,7 @@ def _popen(cmd): def make(cfg: dict) -> Module: run = cfg.get("run", _run) + popen = cfg.get("popen", _popen) def state(): fmt = SEP.join(["{{status}}", "{{title}}", "{{artist}}", "{{album}}", "{{mpris:length}}", @@ -40,10 +41,22 @@ def make(cfg: dict) -> Module: if not out: return {"status": "Stopped", "volume": volume} status, title, artist, album, length, pos, art, name = (out.split(SEP) + [""] * 8)[:8] - art_src = art if art.startswith(("http://", "https://")) else None + if art.startswith(("http://", "https://")): + art_src = art + elif art.startswith("file://"): + art_src = "/api/art?v=" + str(abs(hash(art))) + else: + art_src = None return {"status": status, "title": title, "artist": artist, "album": album, "length": int(length or 0) / 1e6, "position": int(pos or 0) / 1e6, "art": art_src, "player": name.split(".")[0], "volume": volume} - actions = {name: (lambda cmd: (lambda body: _popen(cmd)))(cmd) for name, cmd in BASE_ACTIONS.items()} + def seek(body): + pos = body.get("position") + if not isinstance(pos, (int, float)) or pos < 0: + raise ModuleError("position must be a number") + popen(["playerctl", "position", str(round(float(pos), 1))]) + + actions = {name: (lambda cmd: (lambda body: popen(cmd)))(cmd) for name, cmd in BASE_ACTIONS.items()} + actions["seek"] = seek return Module(name="music", title="Music", state=state, actions=actions) |
