Skip to content

Commit

Permalink
feat: add generic property accessor APIs
Browse files Browse the repository at this point in the history
This makes it easy to tweak properties by clients without enumerating
every property with explicit APIs.
  • Loading branch information
vapier committed Aug 21, 2020
1 parent eb1e5c7 commit ec2fe74
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ You can also directly talk to the endpoints:
| /api/toggle_pause | POST | | Toggle play/pause |
| /api/fullscreen | POST | | Toggle fullscreen |
| /api/quit | POST | | Quit the program |
| /api/add/:name/:value | POST | `string` and `int` or `float` | Add `:value` (default of `1`) to the `:name` property |
| /api/cycle/:name/:value | POST | `string` and `up` or `down` | Cycle `:name` by `:value` (default of `up`) |
| /api/multiply/:name/:value | POST | `string` and `int` or `float` | Multiply `:name` by `:value` |
| /api/set/:name/:value | POST | `string` and anything | Set `:name` to `:value` |
| /api/toggle/:name | POST | `string` | Toggle the boolean property |
| /api/seek/:seconds | POST | `int` or `float` (can be negative) | Seek |
| /api/set_position/:seconds | POST | | Go to position :seconds |
| /api/playlist_prev | POST | | Go to previous media in playlist |
Expand Down
48 changes: 48 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,54 @@ def test_speed(mpv_instance):
for (endpoint, arg, value) in TESTS:
assert send(endpoint, arg=arg, status="speed") == value

@staticmethod
def test_add(mpv_instance):
def add(arg=None):
return send("add/volume", arg=arg, status="volume")

send("set/volume", "10")
assert add() == 11
assert add("") == 12
assert round(add("5.1"), 1) == 17.1
assert round(add("-3"), 1) == 14.1

@staticmethod
def test_cycle(mpv_instance):
def cycle(arg=None):
return send("cycle/pause", arg=arg, status="pause")

send("set/pause", "yes")
assert cycle() is False
assert cycle("") is True
assert cycle("up") is False
assert cycle("down") is True

@staticmethod
def test_multiply(mpv_instance):
def multiply(arg):
return send("multiply/volume", arg=arg, status="volume")

send("set/volume", "10")
assert multiply("2") == 20
assert multiply("1.1") == 22

@staticmethod
def test_set(mpv_instance):
def set(arg):
return send("set/volume", arg=arg, status="volume")

assert round(set("91.2"), 1) == 91.2
assert round(set("107.3"), 1) == 107.3

@staticmethod
def test_toggle(mpv_instance):
def toggle():
return send("toggle/fullscreen", status="fullscreen")

send("set/fullscreen", arg="yes")
assert toggle() is False
assert toggle() is True

@staticmethod
def test_playlist(mpv_instance):
def get_order(s):
Expand Down
8 changes: 3 additions & 5 deletions webui-page/webui.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ var DEBUG = false;
blockPosSlider = false;
blockVolSlider = false;

function send(command, param){
DEBUG && console.log('Sending command: ' + command + ' - param: ' + param);
function send(command, ...args) {
DEBUG && console.log(`Sending command: ${command} params: ${args}`);
if ('mediaSession' in navigator) {
audioLoad();
}
var path = 'api/' + command;
if (param !== undefined)
path += "/" + param;
const path = ['api', command, ...args].join('/');

var request = new XMLHttpRequest();
request.open("post", path);
Expand Down
91 changes: 90 additions & 1 deletion webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ local function validate_number_param(param)
end
end

local function validate_name_param(param)
if not string.match(param, '^[a-z0-9/-]+$') then
return false, 'Parameter name contains invalid characters'
else
return true, nil
end
end

local function validate_value_param(param)
if not string.match(param, '^%g+$') then
return false, 'Parameter value contains invalid characters'
else
return true, nil
end
end

local function validate_cycle_param(param)
if param ~= 'up' and param ~= 'down' then
return false, 'Cycle paramater is not "up" or "down"'
else
return true, nil
end
end

local function validate_loop_param(param, valid_table)
for _, value in pairs(valid_table) do
if value == param then
Expand All @@ -42,7 +66,7 @@ local function validate_loop_param(param, valid_table)
end
valid, msg = validate_number_param(param)
if not valid then
return false, "Invalid parameter!p"
return false, "Invalid parameter!"
end
return true, nil
end
Expand Down Expand Up @@ -113,6 +137,71 @@ local commands = {
return pcall(mp.command, "seek "..t)
end,

add = function(name, value)
local valid, msg = validate_name_param(name)
if not valid then
return true, false, msg
end
if value ~= nil and value ~= '' then
local valid, msg = validate_number_param(value)
if not valid then
return true, false, msg
end
return pcall(mp.commandv, 'add', name, value)
else
return pcall(mp.commandv, 'add', name)
end
end,

cycle = function(name, value)
local valid, msg = validate_name_param(name)
if not valid then
return true, false, msg
end
if value ~= nil and value ~= '' then
local valid, msg = validate_cycle_param(value)
if not valid then
return true, false, msg
end
return pcall(mp.commandv, 'cycle', name, value)
else
return pcall(mp.commandv, 'cycle', name)
end
end,

multiply = function(name, value)
local valid, msg = validate_name_param(name)
if not valid then
return true, false, msg
end
local valid, msg = validate_number_param(value)
if not valid then
return true, false, msg
end
return pcall(mp.commandv, 'multiply', name, value)
end,

set = function(name, value)
local valid, msg = validate_name_param(name)
if not valid then
return true, false, msg
end
local valid, msg = validate_value_param(value)
if not valid then
return true, false, msg
end
return pcall(mp.commandv, 'set', name, value)
end,

toggle = function(name)
local valid, msg = validate_name_param(name)
if not valid then
return true, false, msg
end
local curr = mp.get_property_bool(name)
return pcall(mp.set_property_bool, name, not curr)
end,

set_position = function(t)
local valid, msg = validate_number_param(t)
if not valid then
Expand Down

0 comments on commit ec2fe74

Please sign in to comment.