Files
knorke/pyload.lua

74 lines
2.4 KiB
Lua
Raw Normal View History

-- TODO: support https
-- TODO: more info from api (see: https://github.com/pyload/pyload/blob/stable/module/Api.py)
-- TODO: provide icon
2016-05-14 15:46:54 +02:00
-- TODO: flashing on captcha request
-- TODO: handle offline pyload server (error: ..../json.lua:352: bad argument #1 to 'len' (string expected, got nil))
local setmetatable = setmetatable
local io = { popen = io.popen }
2016-05-14 15:46:54 +02:00
local awful = require("awful")
local wibox = require("wibox")
local helpers = require("knorke.helpers")
local json = require("knorke.json")
local pyload = { mt = {} }
local thiswidget = wibox.widget.textbox()
-- all values in returned table are converted to string values
2016-05-14 15:46:54 +02:00
local function update_data(args)
local pyload_state = {
["pause"] = "N/A", -- boolean
-- ["captcha"] = "N/A", -- boolean available in /json/status
["queue"] = "N/A", -- integer
["download"] = "N/A", -- boolean
["reconnect"] = "N/A", -- boolean
["active"] = "N/A", -- integer
["total"] = "N/A", -- integer
["speed"] = "N/A" -- float
}
-- first argument host, second port - with fallback to localhost:8000
2016-05-14 15:46:54 +02:00
local host = args.host or "127.0.0.1"
local port = args.port or "8000"
local username = args.user or ""
local password = args.passwd or ""
2016-05-14 15:46:54 +02:00
local baseurl = "http://" .. host .. ":" ..port
local loginurl = baseurl .. "/api/login"
local statusurl = baseurl .. "/api/statusServer"
-- the /json/status url needs activated webinterface
-- the cookie from login will be passed to the secound curl command via pipe
local json_string = io.popen("curl --connect-timeout 1 -fsm 3 --data \"username=" .. username .. "&password=" .. password .. "\" --cookie-jar - " .. loginurl .. " | curl --connect-timeout 1 -fsm 3 --cookie - " .. statusurl)
for k, v in pairs(json.decode(json_string:read())) do
-- if k == "pause" then
pyload_state[k] = tostring(v)
end
json_string:close()
2016-05-14 15:46:54 +02:00
thiswidget:set_text(helpers.sub_format_string(args.format, pyload_state))
2016-05-14 15:46:54 +02:00
-- open firefox with onclick
thiswidget:buttons(awful.util.table.join(
awful.button({ }, 1, function()
awful.util.spawn("firefox " .. baseurl)
end)))
end
local function setup_update(args)
mytimer = timer({timeout = tonumber(args.timeout)})
mytimer:connect_signal("timeout", function() update_data(args) end)
mytimer:start()
end
function pyload.new(args)
thiswidget.update = setup_update(args)
return thiswidget
end
function pyload.mt:__call(...)
return pyload.new(...)
end
return setmetatable(pyload, pyload.mt)