add lastfm now playing command

This commit is contained in:
darkf 2015-03-17 00:56:20 -07:00
parent 25a335f5a4
commit ce553010a8
1 changed files with 29 additions and 0 deletions

View File

@ -1,4 +1,6 @@
import("std/list").
import("std/str").
import("std/http").
-- config constants
@ -6,6 +8,8 @@ HOST = "127.0.0.1".
PORT = 6667.
NICK = "lambot".
LASTFM_API_KEY = "YOUR_LASTFM_API_KEY_HERE".
-- nicks of administrators
ADMINS = ["darkf"].
@ -74,6 +78,19 @@ map_remove((k,v)::xs, key) ->
if k == key then xs
else (k,v) :: map_remove(xs, key).
-- dead simple XML tag line parser
tag_contents("<"::xs, tag) -> do
len = list\length(tag);
if str\takeS(len, xs) == tag then do
_ :: rest = str\dropWhileS(\c -> c != ">", xs);
("just", str\takeUntilS(\c -> c == "<", rest))
end
else
tag_contents(str\dropS(len+1, xs), tag)
end.
tag_contents(_::xs, tag) -> tag_contents(xs, tag).
tag_contents(_, tag) -> ("nothing",). -- assert?
-- irc stuff
-- Splits a string by spaces, or until it encounters a :, whereby the following is considered one element.
@ -162,6 +179,18 @@ handleMessage(s, nick, chan, "$eval "::line) -> do
s
end.
handleMessage(s, nick, chan, "$np "::lastfm_user) -> do
http\async_http_get("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=" + lastfm_user + "&api_key=" + LASTFM_API_KEY + "&limit=1",
\resp -> do
("ok", xml) = resp;
("just", artist) = tag_contents(xml, "artist");
("just", album) = tag_contents(xml, "album");
("just", title) = tag_contents(xml, "name");
say(chan, nick + ": " + lastfm_user + " is listening to " + artist + " - " + title + " (from " + album + ")")
end);
s
end.
handleMessage(s, nick, chan, "$join "::j) -> do
if isAdmin(nick) then do
fputstr(sock, "JOIN " + j + "\r\n");