Add ability to seek

This commit is contained in:
Peder Bergebakken Sundt 2018-02-28 22:18:22 +01:00
parent 6b88676dc4
commit c0c29974d3
2 changed files with 42 additions and 31 deletions

View File

@ -91,33 +91,36 @@ async def volume_set(request, mpv_control):
.volume_set(int(request.args["volume"][0])) .volume_set(int(request.args["volume"][0]))
return locals() return locals()
#@bp.get("/something") @bp.get("/time")
@doc.summary("") @doc.summary("Get current playback position")
@response_json @response_json
async def noe(request, mpv_control): async def time_get(request, mpv_control):
body = await mpv_control.time_pos_get() value = {
return body "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") #@bp.get("/something")
@doc.summary("") @doc.summary("")
@response_json @response_json
async def noe(request, mpv_control): async def noe(request, mpv_control):
body = await mpv_control.time_remaining_get() value = await mpv_control.seek_percent(percent)
return body return locals()
#@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
@bp.get("/playlist") @bp.get("/playlist")
@doc.summary("Get the current playlist") @doc.summary("Get the current playlist")
@ -148,23 +151,23 @@ async def playlist_previous(request, mpv_control):
#@bp.get("/something") #@bp.get("/something")
@response_json @response_json
async def noe(request, mpv_control): async def noe(request, mpv_control):
body = await mpv_control.playlist_clear() value = await mpv_control.playlist_clear()
return body return locals()
#@bp.get("/something") #@bp.get("/something")
@response_json @response_json
async def noe(request, mpv_control): async def noe(request, mpv_control):
body = await mpv_control.playlist_remove(index=None) value = await mpv_control.playlist_remove(index=None)
return body return locals()
#@bp.get("/something") #@bp.get("/something")
@response_json @response_json
async def noe(request, mpv_control): async def noe(request, mpv_control):
body = await mpv_control.playlist_move(index1, index2) value = await mpv_control.playlist_move(index1, index2)
return body return locals()
#@bp.get("/something") #@bp.get("/something")
@response_json @response_json
async def noe(request, mpv_control): async def noe(request, mpv_control):
body = await mpv_control.playlist_shuffle() value = await mpv_control.playlist_shuffle()
return body return locals()

View File

@ -113,9 +113,17 @@ class MPVControl:
raise MPVError("Unable to set volume!") raise MPVError("Unable to set volume!")
return resp["error"] == "success" return resp["error"] == "success"
async def time_pos_get(self): 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): 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): async def seek_relative(self, seconds):
return await self.send_request({"command":["seek", seconds, "relative"]}) return await self.send_request({"command":["seek", seconds, "relative"]})
async def seek_percent(self, percent): async def seek_percent(self, percent):