From 5e05e5e7e5c363131d6fb3e2f11e761c1f69c222 Mon Sep 17 00:00:00 2001 From: tilpner Date: Tue, 17 Apr 2018 23:13:23 +0200 Subject: Initial commit --- modules/3-youtube.rkt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 modules/3-youtube.rkt (limited to 'modules/3-youtube.rkt') diff --git a/modules/3-youtube.rkt b/modules/3-youtube.rkt new file mode 100644 index 0000000..835bee9 --- /dev/null +++ b/modules/3-youtube.rkt @@ -0,0 +1,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)))))) -- cgit v1.2.3