summaryrefslogtreecommitdiff
path: root/modules/3-youtube.rkt
blob: 835bee931834152a9b91646302eb7cacfb3e2cd2 (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
#lang racket/base
(require irc/bot racket/string net/url net/uri-codec json)
(provide (all-defined-out))

(define (youtube-query url)
  (call/input-url
    (string->url (format "https://noembed.com/embed?url=~a"
                         (uri-encode (url->string url))))
    get-pure-port read-json))

; catch youtube.com and youtu.be
(define (extract-youtube-url s)
  (define (is-yt-url u)
    (and (url-host u)
         (string-contains? (url-host u) "youtu")))

  (let* ([urls (map string->url (string-split s))])
    (findf is-yt-url urls)))

(on (and (command-is 'PRIVMSG)
         (context-is "#code"))
    (let ([url (extract-youtube-url (suffix))])
      (when url
        (let* ([response (youtube-query url)]
               [title (hash-ref response 'title)])
          (reply (format "\"~a\"" title))))))