From d68749e6d31beffe45b6230f6b1428e79ceadb57 Mon Sep 17 00:00:00 2001 From: tilpner Date: Sun, 12 Aug 2018 21:16:34 +0200 Subject: Add listenbrainz module --- modules/3-listenbrainz.rkt | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 modules/3-listenbrainz.rkt (limited to 'modules') 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)))) -- cgit v1.2.3