From ce553010a80ceb691505cbd3d1b58b6d80d44f14 Mon Sep 17 00:00:00 2001 From: darkf Date: Tue, 17 Mar 2015 00:56:20 -0700 Subject: [PATCH] add lastfm now playing command --- irc.lamb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/irc.lamb b/irc.lamb index 2b111f9..93ec7de 100644 --- a/irc.lamb +++ b/irc.lamb @@ -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");