From c0c29974d3dbec92a354e2b8f716d41c827a21a5 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Wed, 28 Feb 2018 22:18:22 +0100 Subject: [PATCH] Add ability to seek --- grzegorz/api.py | 61 ++++++++++++++++++++++++++----------------------- grzegorz/mpv.py | 12 ++++++++-- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/grzegorz/api.py b/grzegorz/api.py index bdd86d5..20644d3 100644 --- a/grzegorz/api.py +++ b/grzegorz/api.py @@ -91,33 +91,36 @@ async def volume_set(request, mpv_control): .volume_set(int(request.args["volume"][0])) return locals() -#@bp.get("/something") -@doc.summary("") +@bp.get("/time") +@doc.summary("Get current playback position") @response_json -async def noe(request, mpv_control): - body = await mpv_control.time_pos_get() - return body +async def time_get(request, mpv_control): + value = { + "current": await mpv_control.time_pos_get(), + "left": await mpv_control.time_remaining_get(), + } + value["total"] = value["current"] + value["left"] + return locals() + +@bp.post("/time") +@doc.summary("Set playback position") +@doc.consumes({"pos": doc.Float("Seconds to seek to"), "pos": doc.Integer("Percent to seek to")}) +@response_json +async def time_set(request, mpv_control): + if "pos" in request.args: + success = await mpv_control.seek_absolute(float(request.args["pos"][0])) + elif "percent" in request.args: + success = await mpv_control.seek_percent(int(request.args["percent"][0])) + else: + raise APIError("No query parameter \"pos\" or \"percent\"provided") + return locals() #@bp.get("/something") @doc.summary("") @response_json async def noe(request, mpv_control): - body = await mpv_control.time_remaining_get() - return body - -#@bp.get("/something") -@doc.summary("") -@response_json -async def noe(request, mpv_control): - body = await mpv_control.seek_relative(seconds) - return body - -#@bp.get("/something") -@doc.summary("") -@response_json -async def noe(request, mpv_control): - body = await mpv_control.seek_percent(percent) - return body + value = await mpv_control.seek_percent(percent) + return locals() @bp.get("/playlist") @doc.summary("Get the current playlist") @@ -148,23 +151,23 @@ async def playlist_previous(request, mpv_control): #@bp.get("/something") @response_json async def noe(request, mpv_control): - body = await mpv_control.playlist_clear() - return body + value = await mpv_control.playlist_clear() + return locals() #@bp.get("/something") @response_json async def noe(request, mpv_control): - body = await mpv_control.playlist_remove(index=None) - return body + value = await mpv_control.playlist_remove(index=None) + return locals() #@bp.get("/something") @response_json async def noe(request, mpv_control): - body = await mpv_control.playlist_move(index1, index2) - return body + value = await mpv_control.playlist_move(index1, index2) + return locals() #@bp.get("/something") @response_json async def noe(request, mpv_control): - body = await mpv_control.playlist_shuffle() - return body + value = await mpv_control.playlist_shuffle() + return locals() diff --git a/grzegorz/mpv.py b/grzegorz/mpv.py index 0b16734..b63e29e 100644 --- a/grzegorz/mpv.py +++ b/grzegorz/mpv.py @@ -113,9 +113,17 @@ class MPVControl: raise MPVError("Unable to set volume!") return resp["error"] == "success" async def time_pos_get(self): - return await self.send_request({"command":["get_property", "time-pos"]}) + resp = await self.send_request({"command":["get_property", "time-pos"]}) + if "error" in resp and resp["error"] != "success": + raise MPVError("Unable to get time pos: " + resp["error"]) + return resp["data"] if "data" in resp else None async def time_remaining_get(self): - return await self.send_request({"command":["get_property", "time-remaining"]}) + resp = await self.send_request({"command":["get_property", "time-remaining"]}) + if "error" in resp and resp["error"] != "success": + raise MPVError("Unable to get time left:" + resp["error"]) + return resp["data"] if "data" in resp else None + async def seek_absolute(self, seconds): + return await self.send_request({"command":["seek", seconds, "absolute"]}) async def seek_relative(self, seconds): return await self.send_request({"command":["seek", seconds, "relative"]}) async def seek_percent(self, percent):