#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))))