Implement mpv play/pause, next, previous and get playlist

This commit is contained in:
Peder Bergebakken Sundt 2018-02-24 21:31:52 +01:00
parent 577959432b
commit 7f6b9fcba9
3 changed files with 81 additions and 48 deletions

View File

@ -9,7 +9,8 @@ from . import api
mpv_control = None#mpv.MPVControl() mpv_control = None#mpv.MPVControl()
async def test(): 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): def main(host="0.0.0.0", port=8080, tasks:list = None):
app = Sanic(__name__) app = Sanic(__name__)

View File

@ -8,8 +8,15 @@ bp = Blueprint("grzegorz-api")
#route decorators: #route decorators:
def response_json(func): def response_json(func):
async def newfunc(*args, **kwargs): async def newfunc(*args, **kwargs):
#try:
body = await func(*args, **kwargs) body = await func(*args, **kwargs)
if "error" not in body:
body["error"] = False
if "request" in body:
del body["request"]
return response.json(body) return response.json(body)
#except Exception as e:
# return response.json({"error": e.__class__.__name__, "error_msg": e.args})
return newfunc return newfunc
def response_text(func): def response_text(func):
async def newfunc(*args, **kwargs): async def newfunc(*args, **kwargs):
@ -23,82 +30,101 @@ def response_text(func):
async def root(request): async def root(request):
return "Hello World!" return "Hello World!"
#@bp.get("/something") @bp.post("/load")
@response_json @response_json
async def noe(request): async def loadfile(request):
return request.app.config["mpv_control"].loadfile(file) 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): 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") #@bp.get("/something")
@response_json @response_json
async def noe(request): async def noe(request):
return request.app.config["mpv_control"].playlist_next() body = await request.app.config["mpv_control"].playlist_shuffle()
return body
#@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()

View File

@ -21,7 +21,7 @@ class MPV:
'--fullscreen', '--fullscreen',
'--no-terminal', '--no-terminal',
'--keep-open', # Keep last frame of video on end of video '--keep-open', # Keep last frame of video on end of video
'--no-input-default-bindings', #'--no-input-default-bindings',
) )
while self.is_running(): while self.is_running():
@ -92,11 +92,14 @@ class MPVControl:
#Shorthand command requests: #Shorthand command requests:
async def loadfile(self, file):#appends to playlist and start playback if paused 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): 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): 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): async def volume_get(self):
return await self.send_request({"command":["get_property", "volume"]}) return await self.send_request({"command":["get_property", "volume"]})
async def volume_set(self, volume): async def volume_set(self, volume):
@ -110,11 +113,14 @@ class MPVControl:
async def seek_percent(self, percent): async def seek_percent(self, percent):
return await self.send_request({"command":["seek", percent, "absolute-percent"]}) return await self.send_request({"command":["seek", percent, "absolute-percent"]})
async def playlist_get(self): 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): 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): 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): async def playlist_clear(self):
return await self.send_request({"command":["playlist-clear"]}) return await self.send_request({"command":["playlist-clear"]})
async def playlist_remove(self, index=None): async def playlist_remove(self, index=None):