summaryrefslogtreecommitdiff
path: root/modules/3-listenbrainz.rkt
blob: e4e54379ed8c28d0a34706a8750fe424dae2fc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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))))