Add volume control, errorhandling and MPVError exception class
This commit is contained in:
parent
7f6b9fcba9
commit
226e807f24
@ -3,20 +3,27 @@ import asyncio
|
||||
from . import mpv
|
||||
|
||||
bp = Blueprint("grzegorz-api")
|
||||
#this blueprint assumes a mpv.MPVControl instance is available at request.app.config["mpv_control"]
|
||||
# this blueprint assumes a mpv.MPVControl instance is available
|
||||
# at request.app.config["mpv_control"]
|
||||
|
||||
#route decorators:
|
||||
def response_json(func):
|
||||
async def newfunc(*args, **kwargs):
|
||||
#try:
|
||||
body = await func(*args, **kwargs)
|
||||
try:
|
||||
mpv_control = args[0].app.config["mpv_control"]
|
||||
body = await func(*args, mpv_control, **kwargs)
|
||||
if "error" not in body:
|
||||
body["error"] = False
|
||||
if "request" in body:
|
||||
del body["request"]
|
||||
if "mpv_control" in body:
|
||||
del body["mpv_control"]
|
||||
return response.json(body)
|
||||
#except Exception as e:
|
||||
# return response.json({"error": e.__class__.__name__, "error_msg": e.args})
|
||||
except Exception as e:
|
||||
return response.json({
|
||||
"error": e.__class__.__name__,
|
||||
"errortext": str(e)
|
||||
})
|
||||
return newfunc
|
||||
def response_text(func):
|
||||
async def newfunc(*args, **kwargs):
|
||||
@ -32,99 +39,102 @@ async def root(request):
|
||||
|
||||
@bp.post("/load")
|
||||
@response_json
|
||||
async def loadfile(request):
|
||||
async def loadfile(request, mpv_control):
|
||||
if "source" not in request.json:
|
||||
return {"error", "no param \"source\""}
|
||||
success = await request.app.config["mpv_control"].loadfile(request.json["source"])
|
||||
success = await 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
|
||||
async def get_play(request, mpv_control):
|
||||
value = await mpv_control.pause_get() == False
|
||||
return locals()
|
||||
|
||||
@bp.post("/play")
|
||||
@response_json
|
||||
async def set_play(request):
|
||||
async def set_play(request, mpv_control):
|
||||
success = await request.app.config["mpv_control"] \
|
||||
.pause_set(request.form["play"][0] not in ["true", "1"])
|
||||
return locals()
|
||||
|
||||
@bp.get("/volume")
|
||||
@response_json
|
||||
async def get_volume(request, mpv_control):
|
||||
value = await mpv_control.volume_get()
|
||||
return locals()
|
||||
|
||||
@bp.post("/volume")
|
||||
@response_json
|
||||
async def set_volume(request, mpv_control):
|
||||
success = await request.app.config["mpv_control"] \
|
||||
.volume_set(int(request.form["volume"][0]))
|
||||
return locals()
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].volume_get()
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.time_pos_get()
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].volume_set(volume)
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.time_remaining_get()
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].time_pos_get()
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.seek_relative(seconds)
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].time_remaining_get()
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].seek_relative(seconds)
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].seek_percent(percent)
|
||||
async def noe(request, mpv_control):
|
||||
body = await 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()
|
||||
async def noe(request, mpv_control):
|
||||
value = await mpv_control.playlist_get()
|
||||
for i, v in enumerate(value):
|
||||
pass
|
||||
return locals()
|
||||
|
||||
@bp.post("/playlist/next")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
success = await request.app.config["mpv_control"].playlist_next()
|
||||
async def noe(request, mpv_control):
|
||||
success = await 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()
|
||||
async def noe(request, mpv_control):
|
||||
success = await mpv_control.playlist_prev()
|
||||
return locals()
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].playlist_clear()
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.playlist_clear()
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].playlist_remove(index=None)
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.playlist_remove(index=None)
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].playlist_move(index1, index2)
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.playlist_move(index1, index2)
|
||||
return body
|
||||
|
||||
#@bp.get("/something")
|
||||
@response_json
|
||||
async def noe(request):
|
||||
body = await request.app.config["mpv_control"].playlist_shuffle()
|
||||
async def noe(request, mpv_control):
|
||||
body = await mpv_control.playlist_shuffle()
|
||||
return body
|
||||
|
@ -67,6 +67,8 @@ class MPV:
|
||||
else: # response
|
||||
self.responses.put_nowait(msg)
|
||||
|
||||
class MPVError(Exception): pass
|
||||
|
||||
class MPVControl:
|
||||
def __init__(self):
|
||||
self.mpv = MPV()
|
||||
@ -101,9 +103,13 @@ class MPVControl:
|
||||
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"]})
|
||||
resp = await self.send_request({"command":["get_property", "volume"]})
|
||||
if "error" in resp and resp["error"] != "success" or 1:
|
||||
raise MPVError("Unable to set volume!")
|
||||
return resp["data"] if "data" in resp else None
|
||||
async def volume_set(self, volume):
|
||||
return await self.send_request({"command":["set_property", "volume", volume]})
|
||||
resp = await self.send_request({"command":["set_property", "volume", volume]})
|
||||
return resp["error"] == "success"
|
||||
async def time_pos_get(self):
|
||||
return await self.send_request({"command":["get_property", "time-pos"]})
|
||||
async def time_remaining_get(self):
|
||||
|
Loading…
Reference in New Issue
Block a user