From 97ba1caced5fc3fa2eaecc19350f6415a0bd2aaa Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Thu, 11 Apr 2019 20:04:16 -0700 Subject: [PATCH 1/2] Add a cljquotes command for spouting random quotes about Clojure With quotes sourced from https://github.com/Azel4231/clojure-quotes --- src/yetibot/commands/cljquotes.clj | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/yetibot/commands/cljquotes.clj diff --git a/src/yetibot/commands/cljquotes.clj b/src/yetibot/commands/cljquotes.clj new file mode 100644 index 00000000..80e3dc31 --- /dev/null +++ b/src/yetibot/commands/cljquotes.clj @@ -0,0 +1,40 @@ +(ns yetibot.commands.cljquotes + (:require + [clojure.edn :as edn] + [clojure.string :as string] + + [clj-http.client :as client] + + [yetibot.core.hooks :refer [cmd-hook]] + )) + +(defn fetch-quotes + [] + (let [url "https://raw.githubusercontent.com/Azel4231/clojure-quotes/master/quotes.edn"] + (-> (client/get url) + :body + edn/read-string + ))) + +(def quotes (fetch-quotes)) + +(defn format-quote + [quote] + (let [{:clojure-quotes.core/keys [text quotee reference]} quote + {:clojure-quotes.core/keys [url time]} reference] + (str ">>> " text "\n" + "-- " quotee + (when url (str " (" url (when time (str " @" time)) ")"))))) + +(defn quote-cmd + "cljquote # random clojure quote" + [context] + (let [quote (rand-nth quotes)] + {:result/value (format-quote quote) + :result/data quote})) + +(cmd-hook #"cljquote" + _ quote-cmd) + +#_(format-quote (rand-nth quotes)) +#_(string/join "\n\n" (map format-quote quotes)) From 689e8313d7f9afde859e37b71abc57cb25c172ab Mon Sep 17 00:00:00 2001 From: Nate Jones Date: Fri, 12 Apr 2019 14:39:38 -0700 Subject: [PATCH 2/2] cache quotes daily --- src/yetibot/commands/cljquotes.clj | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/yetibot/commands/cljquotes.clj b/src/yetibot/commands/cljquotes.clj index 80e3dc31..d2dd6fc9 100644 --- a/src/yetibot/commands/cljquotes.clj +++ b/src/yetibot/commands/cljquotes.clj @@ -1,5 +1,6 @@ (ns yetibot.commands.cljquotes (:require + [clojure.core.memoize :as memo] [clojure.edn :as edn] [clojure.string :as string] @@ -13,10 +14,13 @@ (let [url "https://raw.githubusercontent.com/Azel4231/clojure-quotes/master/quotes.edn"] (-> (client/get url) :body - edn/read-string - ))) + edn/read-string))) -(def quotes (fetch-quotes)) +;; only fetch once per day +(def quote-fetch-delay (* 24 60 60 1000)) + +(def quotes + (memo/ttl fetch-quotes :ttl/threshold quote-fetch-delay)) (defn format-quote [quote] @@ -29,12 +33,12 @@ (defn quote-cmd "cljquote # random clojure quote" [context] - (let [quote (rand-nth quotes)] + (let [quote (rand-nth (quotes))] {:result/value (format-quote quote) :result/data quote})) (cmd-hook #"cljquote" _ quote-cmd) -#_(format-quote (rand-nth quotes)) -#_(string/join "\n\n" (map format-quote quotes)) +#_(format-quote (rand-nth (quotes))) +#_(string/join "\n\n" (map format-quote (quotes)))