Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a last.fm plugin.

  • Loading branch information...
commit 55ec4258188165f428caf7a3a4173dbfaca4423f 1 parent 37d0607
@Raynes authored
Showing with 69 additions and 1 deletion.
  1. +2 −1  project.clj
  2. +67 −0 src/lazybot/plugins/lastfm.clj
View
3  project.clj
@@ -28,7 +28,8 @@
[tentacles "0.2.1"]
[findfn "0.1.3"]
[me.raynes/laser "0.1.16"]
- [hiccup "1.0.2"]]
+ [hiccup "1.0.2"]
+ [me.raynes/least "0.1.2"]]
:uberjar-name "lazybot.jar"
:main lazybot.run
:copy-deps true
View
67 src/lazybot/plugins/lastfm.clj
@@ -0,0 +1,67 @@
+(ns lazybot.plugins.lastfm
+ (:require [me.raynes.least :as least]
+ [clojure.java.io :refer [file]]
+ [clojure.edn :as edn]
+ [lazybot.info :refer [*lazybot-dir*]]
+ [lazybot.registry :refer [send-message defplugin]])
+ (:import java.io.FileWriter))
+
+(def association-file
+ (file *lazybot-dir* "lastfmassociations.clj"))
+
+(when-not (.exists association-file)
+ (.createNewFile association-file))
+
+(def associations (agent (or (edn/read-string (slurp association-file))
+ {})))
+
+(defn write-associations [assocs]
+ (binding [*out* (FileWriter. association-file)]
+ (pr-str assocs)))
+
+(defn update-agent [state server nick user]
+ (doto (assoc-in state [server nick] user)
+ (write-associations)))
+
+(defn add-assoc [server nick user]
+ (send-off associations update-agent server nick user))
+
+(defn get-api-key [bot]
+ (get-in @bot [:config :lastfm :api-key]))
+
+(defn get-latest-song [bot server nick]
+ (let [user (get-in @associations [server nick] nick)]
+ (when-let [latest (first (get-in (least/read "user.getRecentTracks"
+ (get-api-key bot)
+ {:user user
+ :limit 1})
+ [:recenttracks :track]))]
+ (format "%s %s: %s - %s [%s]"
+ user
+ (if (= "true" (get-in latest [(keyword "@attr") :nowplaying]))
+ "is listening to"
+ "last listened to")
+ (get-in latest [:artist :#text])
+ (:name latest)
+ (get-in latest [:album :#text])))))
+
+(defplugin
+ (:cmd
+ "Get the latest song played by a user (yourself by default)."
+ #{"last"}
+ (fn [{:keys [nick com bot args] :as com-m}]
+ (send-message
+ com-m
+ (or (get-latest-song bot (:server @com) (or (first args) nick))
+ "Couldn't find that user."))))
+
+ (:cmd
+ "Associate your nickname with a lastfm username"
+ #{"lfmassoc"}
+ (fn [{:keys [nick com args] :as com-m}]
+ (send-message
+ com-m
+ (if-let [user (first args)]
+ (do (add-assoc (:server @com) nick user)
+ "Associated your username.")
+ "Well, I can't guess your username. I need an argument, please.")))))
Please sign in to comment.
Something went wrong with that request. Please try again.