From 7f6b9fcba9739cc23a2b00301d08de68f04a0f9a Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sat, 24 Feb 2018 21:31:52 +0100 Subject: [PATCH] Implement mpv play/pause, next, previous and get playlist --- grzegorz/__init__.py | 3 +- grzegorz/api.py | 106 +++++++++++++++++++++++++++---------------- grzegorz/mpv.py | 20 +++++--- 3 files changed, 81 insertions(+), 48 deletions(-) diff --git a/grzegorz/__init__.py b/grzegorz/__init__.py index 34d40aa..3d52899 100644 --- a/grzegorz/__init__.py +++ b/grzegorz/__init__.py @@ -9,7 +9,8 @@ from . import api mpv_control = None#mpv.MPVControl() async def test(): - await mpv_control.loadfile('grzegorz/res/logo.jpg') + resp = await mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg') + #print(resp) def main(host="0.0.0.0", port=8080, tasks:list = None): app = Sanic(__name__) diff --git a/grzegorz/api.py b/grzegorz/api.py index e94d1d1..e5553a4 100644 --- a/grzegorz/api.py +++ b/grzegorz/api.py @@ -8,8 +8,15 @@ bp = Blueprint("grzegorz-api") #route decorators: def response_json(func): async def newfunc(*args, **kwargs): - body = await func(*args, **kwargs) - return response.json(body) + #try: + body = await func(*args, **kwargs) + if "error" not in body: + body["error"] = False + if "request" in body: + del body["request"] + return response.json(body) + #except Exception as e: + # return response.json({"error": e.__class__.__name__, "error_msg": e.args}) return newfunc def response_text(func): async def newfunc(*args, **kwargs): @@ -23,82 +30,101 @@ def response_text(func): async def root(request): return "Hello World!" -#@bp.get("/something") +@bp.post("/load") @response_json -async def noe(request): - return request.app.config["mpv_control"].loadfile(file) +async def loadfile(request): + if "source" not in request.json: + return {"error", "no param \"source\""} + success = await request.app.config["mpv_control"].loadfile(request.json["source"]) + return locals() + +@bp.get("/play") +@response_json +async def get_play(request): + value = await request.app.config["mpv_control"].pause_get() == False + return locals() + +@bp.post("/play") +@response_json +async def set_play(request): + success = await request.app.config["mpv_control"] \ + .pause_set(request.form["play"][0] not in ["true", "1"]) + return locals() #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].pause_get() + body = await request.app.config["mpv_control"].volume_get() + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].pause_set(state) + body = await request.app.config["mpv_control"].volume_set(volume) + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].volume_get() + body = await request.app.config["mpv_control"].time_pos_get() + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].volume_set(volume) + body = await request.app.config["mpv_control"].time_remaining_get() + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].time_pos_get() + body = await request.app.config["mpv_control"].seek_relative(seconds) + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].time_remaining_get() + body = await request.app.config["mpv_control"].seek_percent(percent) + return body + +@bp.get("/playlist") +@response_json +async def noe(request): + value = await request.app.config["mpv_control"].playlist_get() + return locals() + +@bp.post("/playlist/next") +@response_json +async def noe(request): + success = await request.app.config["mpv_control"].playlist_next() + return locals() + +@bp.post("/playlist/previous") +@response_json +async def noe(request): + success = await request.app.config["mpv_control"].playlist_prev() + return locals() #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].seek_relative(seconds) + body = await request.app.config["mpv_control"].playlist_clear() + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].seek_percent(percent) + body = await request.app.config["mpv_control"].playlist_remove(index=None) + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].playlist_get() + body = await request.app.config["mpv_control"].playlist_move(index1, index2) + return body #@bp.get("/something") @response_json async def noe(request): - return request.app.config["mpv_control"].playlist_next() - -#@bp.get("/something") -@response_json -async def noe(request): - return request.app.config["mpv_control"].playlist_prev() - -#@bp.get("/something") -@response_json -async def noe(request): - return request.app.config["mpv_control"].playlist_clear() - -#@bp.get("/something") -@response_json -async def noe(request): - return request.app.config["mpv_control"].playlist_remove(index=None) - -#@bp.get("/something") -@response_json -async def noe(request): - return request.app.config["mpv_control"].playlist_move(index1, index2) - -#@bp.get("/something") -@response_json -async def noe(request): - return request.app.config["mpv_control"].playlist_shuffle() + body = await request.app.config["mpv_control"].playlist_shuffle() + return body diff --git a/grzegorz/mpv.py b/grzegorz/mpv.py index 69b8542..1b35cce 100644 --- a/grzegorz/mpv.py +++ b/grzegorz/mpv.py @@ -21,7 +21,7 @@ class MPV: '--fullscreen', '--no-terminal', '--keep-open', # Keep last frame of video on end of video - '--no-input-default-bindings', + #'--no-input-default-bindings', ) while self.is_running(): @@ -92,11 +92,14 @@ class MPVControl: #Shorthand command requests: async def loadfile(self, file):#appends to playlist and start playback if paused - return await self.send_request({"command":["loadfile", file, "append-play"]}) + resp = await self.send_request({"command":["loadfile", file, "append-play"]}) + return resp["error"] == "success" async def pause_get(self): - return await self.send_request({"command":["get_property", "pause"]}) + resp = await self.send_request({"command":["get_property", "pause"]}) + return resp["data"] if "data" in resp else None async def pause_set(self, state): - return await self.send_request({"command":["set_property", "pause", bool(state)]}) + resp = await self.send_request({"command":["set_property", "pause", bool(state)]}) + return resp["error"] == "success" async def volume_get(self): return await self.send_request({"command":["get_property", "volume"]}) async def volume_set(self, volume): @@ -110,11 +113,14 @@ class MPVControl: async def seek_percent(self, percent): return await self.send_request({"command":["seek", percent, "absolute-percent"]}) async def playlist_get(self): - return await self.send_request({"command":["get_property", "playlist"]}) + resp = await self.send_request({"command":["get_property", "playlist"]}) + return resp["data"] if "data" in resp else None async def playlist_next(self): - return await self.send_request({"command":["playlist-next", "weak"]}) + resp = await self.send_request({"command":["playlist-next", "weak"]}) + return resp["error"] == "success" async def playlist_prev(self): - return await self.send_request({"command":["playlist-prev", "weak"]}) + resp = await self.send_request({"command":["playlist-prev", "weak"]}) + return resp["error"] == "success" async def playlist_clear(self): return await self.send_request({"command":["playlist-clear"]}) async def playlist_remove(self, index=None):