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
-- 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
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 status ~= "200" then return status .. " " .. message end
@ -75,17 +75,17 @@ local process = function (buffer, data)
end
if not name then
buffer:log ("Not playing anything right now")
action (false)
else
local np = "Now playing: \"" .. name .. "\""
local np = "\"" .. name .. "\""
if artist then np = np .. " by " .. artist end
if album then np = np .. " from " .. album end
buffer:log (np)
action (np)
end
end
-- 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
c.data = ""
c.on_data = function (data)
@ -94,7 +94,7 @@ local on_connected = function (buffer, c, host)
-- And process it after we receive everything
c.on_eof = function ()
error = process (buffer, c.data)
error = process (buffer, c.data, action)
if error then report_error (buffer, error) end
c:close ()
end
@ -118,7 +118,7 @@ end
local running
-- 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
report_error (buffer, "configuration is incomplete")
return
@ -128,7 +128,7 @@ local make_request = function (buffer)
running = degesch.connect ("ws.audioscrobbler.com", 80, {
on_success = function (c, host)
on_connected (buffer, c, host)
on_connected (buffer, c, host, action)
running = nil
end,
on_error = function (e)
@ -140,15 +140,40 @@ end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-- TODO:
-- /np? to just retrieve the song and print it in the buffer
-- /np! to execute "/me is listening to " .. last retrieved song
-- /np to do both in succession
local now_playing
local tell_song = function (buffer)
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
degesch.hook_input (function (hook, buffer, input)
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
return input
end