Permalink
Browse files

Make title-scraper O(n), not O(n^2)

  • Loading branch information...
1 parent 2039f95 commit fe7481bedb06ae55eebd819964d12d4e9875fa03 @amalloy committed Jul 23, 2011
Showing with 9 additions and 9 deletions.
  1. +9 −9 src/lazybot/plugins/title.clj
@@ -20,15 +20,15 @@
(defn slurp-or-default [url]
(try
- (with-open [readerurl (reader url)]
- (loop [acc [] lines (line-seq readerurl)]
- (cond
- (not (seq lines)) nil
- (some #(re-find #"</title>|</TITLE>" %) acc) (->> acc (apply str)
- (#(.replace % "\n" " "))
- (re-find titlere))
- :else (recur (conj acc (first lines)) (rest lines)))))
- (catch java.lang.Exception e nil)))
+ (with-open [readerurl (reader url)]
+ (let [acc (StringBuilder.)]
+ (loop [lines (line-seq readerurl)]
+ (when-let [[line & lines] (seq lines)]
+ (.append acc line)
+ (if (re-find #"(?i)</title>" line)
+ (->> acc str (re-find titlere))
+ (recur lines))))))
+ (catch Exception _ nil)))
(defn url-blacklist-words [com bot] (:url-blacklist ((:config @bot) (:server @com))))

0 comments on commit fe7481b

Please sign in to comment.