degesch: Lua: finish the last-fm plugin

This commit is contained in:
Přemysl Eric Janouch 2016-01-14 04:13:03 +01:00
parent 56858a97dd
commit 91f3bd60df
1 changed files with 39 additions and 14 deletions

View File

@ -45,9 +45,9 @@ local report_error = function (buffer, error)
end end
-- Process data return by the server and extract the now playing song -- Process data return by the server and extract the now playing song
local process = function (buffer, data) local process = function (buffer, data, action)
-- There's no reasonable Lua package to parse HTTP that I could find -- There's no reasonable Lua package to parse HTTP that I could find
local s, e, v, status, message = string.find (data, "(%S+) (%S+) (%S+)\r\n") local s, e, v, status, message = string.find (data, "(%S+) (%S+) .+\r\n")
if not s then return "server returned unexpected data" end if not s then return "server returned unexpected data" end
if status ~= "200" then return status .. " " .. message end if status ~= "200" then return status .. " " .. message end
@ -75,17 +75,17 @@ local process = function (buffer, data)
end end
if not name then if not name then
buffer:log ("Not playing anything right now") action (false)
else else
local np = "Now playing: \"" .. name .. "\"" local np = "\"" .. name .. "\""
if artist then np = np .. " by " .. artist end if artist then np = np .. " by " .. artist end
if album then np = np .. " from " .. album end if album then np = np .. " from " .. album end
buffer:log (np) action (np)
end end
end end
-- Set up the connection and make the request -- Set up the connection and make the request
local on_connected = function (buffer, c, host) local on_connected = function (buffer, c, host, action)
-- Buffer data in the connection object -- Buffer data in the connection object
c.data = "" c.data = ""
c.on_data = function (data) c.on_data = function (data)
@ -94,7 +94,7 @@ local on_connected = function (buffer, c, host)
-- And process it after we receive everything -- And process it after we receive everything
c.on_eof = function () c.on_eof = function ()
error = process (buffer, c.data) error = process (buffer, c.data, action)
if error then report_error (buffer, error) end if error then report_error (buffer, error) end
c:close () c:close ()
end end
@ -118,7 +118,7 @@ end
local running local running
-- Initiate a connection to last.fm servers -- Initiate a connection to last.fm servers
local make_request = function (buffer) local make_request = function (buffer, action)
if not user or not api_key then if not user or not api_key then
report_error (buffer, "configuration is incomplete") report_error (buffer, "configuration is incomplete")
return return
@ -128,7 +128,7 @@ local make_request = function (buffer)
running = degesch.connect ("ws.audioscrobbler.com", 80, { running = degesch.connect ("ws.audioscrobbler.com", 80, {
on_success = function (c, host) on_success = function (c, host)
on_connected (buffer, c, host) on_connected (buffer, c, host, action)
running = nil running = nil
end, end,
on_error = function (e) on_error = function (e)
@ -140,15 +140,40 @@ end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- TODO: local now_playing
-- /np? to just retrieve the song and print it in the buffer
-- /np! to execute "/me is listening to " .. last retrieved song local tell_song = function (buffer)
-- /np to do both in succession if now_playing == nil then
buffer:log ("last-fm: I don't know what you're listening to")
elseif not now_playing then
buffer:log ("last-fm: not playing anything right now")
else
buffer:log ("last-fm: now playing: " .. now_playing)
end
end
local send_song = function (buffer)
if not now_playing then
tell_song (buffer)
else
buffer:execute ("/me is listening to " .. now_playing)
end
end
-- Hook input to simulate new commands -- Hook input to simulate new commands
degesch.hook_input (function (hook, buffer, input) degesch.hook_input (function (hook, buffer, input)
if input == "/np" then if input == "/np" then
make_request (buffer) make_request (buffer, function (np)
now_playing = np
send_song (buffer)
end)
elseif input == "/np?" then
make_request (buffer, function (np)
now_playing = np
tell_song (buffer)
end)
elseif input == "/np!" then
send_song (buffer)
else else
return input return input
end end