From 068e52e766bf176adfe648dc9aa266f12c950471 Mon Sep 17 00:00:00 2001 From: tilpner Date: Sun, 12 Aug 2018 21:14:28 +0200 Subject: Fix exception while encountering the supposed URL ':3', preventing the actual URL from bein processed --- modules/3-youtube.rkt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'modules') diff --git a/modules/3-youtube.rkt b/modules/3-youtube.rkt index 835bee9..0ddd1a1 100644 --- a/modules/3-youtube.rkt +++ b/modules/3-youtube.rkt @@ -1,5 +1,7 @@ #lang racket/base -(require irc/bot racket/string net/url net/uri-codec json) +(require irc/bot irc/clean + racket/string racket/format racket/function + net/url net/uri-codec json) (provide (all-defined-out)) (define (youtube-query url) @@ -8,13 +10,18 @@ (uri-encode (url->string url)))) get-pure-port read-json)) +(define (string->maybe-url s) + (with-handlers ([url-exception? (const #f)]) + (string->url s))) + ; catch youtube.com and youtu.be (define (extract-youtube-url s) (define (is-yt-url u) - (and (url-host u) + (and u + (url-host u) (string-contains? (url-host u) "youtu"))) - (let* ([urls (map string->url (string-split s))]) + (let* ([urls (map (compose1 string->maybe-url clean-control) (string-split s))]) (findf is-yt-url urls))) (on (and (command-is 'PRIVMSG) @@ -23,4 +30,6 @@ (when url (let* ([response (youtube-query url)] [title (hash-ref response 'title)]) - (reply (format "\"~a\"" title)))))) + (reply (~v title)))))) + +;(format "\"~a\"" title)))))) -- cgit v1.2.3