summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/3-listenbrainz.rkt29
1 files changed, 29 insertions, 0 deletions
diff --git a/modules/3-listenbrainz.rkt b/modules/3-listenbrainz.rkt
new file mode 100644
index 0000000..e4e5437
--- /dev/null
+++ b/modules/3-listenbrainz.rkt
@@ -0,0 +1,29 @@
+#lang racket/base
+(require irc/core irc/storage irc/bot irc/command
+ racket/format
+ net/url net/uri-codec json)
+
+(define get-listens "https://api.listenbrainz.org/1/user/~a/listens?count=1")
+(define (now-playing lb-user)
+ (let* ([safe-user (uri-encode lb-user)]
+ [port (get-pure-port (string->url (format get-listens safe-user)))]
+ [result (read-json port)]
+ [listens (ref result 'payload 'listens)])
+ (ref (car listens) 'track_metadata)))
+
+(define-command (lb-set-name nick)
+ #:help "Set ListenBrainz handle to be associated with your IRC user"
+ (ref-set! (user-storage (source))
+ (~a nick)
+ 'listenbrainz 'name)
+ (persist-user (source)))
+
+(define-command (np)
+ #:help "Display last played song from your associated ListenBrainz handle"
+ (define lb-name (ref (user-storage (source)) 'listenbrainz 'name))
+ (unless lb-name
+ (error-user "You have not set a name yet. Use (lb-set-name yourlbhandle)"))
+ (define last-meta (now-playing lb-name))
+ (reply (format "\"~a\" by ~a"
+ (ref last-meta 'track_name)
+ (ref last-meta 'artist_name))))