summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortilpner2018-08-12 21:16:34 +0200
committertilpner2018-08-12 21:16:34 +0200
commitd68749e6d31beffe45b6230f6b1428e79ceadb57 (patch)
treeed412e09d8a5277a56e840194741cf69a5e0041e
parent3ad85b9e1336fe977e985ae74e56615f5062adc5 (diff)
downloadmeep-d68749e6d31beffe45b6230f6b1428e79ceadb57.tar.gz
meep-d68749e6d31beffe45b6230f6b1428e79ceadb57.tar.xz
meep-d68749e6d31beffe45b6230f6b1428e79ceadb57.zip
Add listenbrainz module
-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))))