aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-01-14 04:13:03 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2016-01-14 04:13:03 +0100
commit91f3bd60df515ca81a0a5d54467976c7c148e533 (patch)
tree87b50919cd907810d4e3d30cc75b9578c08e68e4 /plugins
parent56858a97dd32f2e45a8ef023ca7dfe1e5f5d7460 (diff)
downloadxK-91f3bd60df515ca81a0a5d54467976c7c148e533.tar.gz
xK-91f3bd60df515ca81a0a5d54467976c7c148e533.tar.xz
xK-91f3bd60df515ca81a0a5d54467976c7c148e533.zip
degesch: Lua: finish the last-fm plugin
Diffstat (limited to 'plugins')
-rw-r--r--plugins/degesch/last-fm.lua53
1 files changed, 39 insertions, 14 deletions
diff --git a/plugins/degesch/last-fm.lua b/plugins/degesch/last-fm.lua
index 54f5d1c..20843e6 100644
--- a/plugins/degesch/last-fm.lua
+++ b/plugins/degesch/last-fm.lua
@@ -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