pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin - collaborative debugging tool View Help


Posted by gnuvince on Wed 19 Nov 03:11
report abuse | View followups from Anonymous | download | new post

  1. (import '(java.net URL)
  2.         '(java.lang StringBuilder)
  3.         '(java.io BufferedReader InputStreamReader)
  4.         '(org.htmlparser Parser)
  5.         '(org.htmlparser.visitors NodeVisitor)
  6.         '(org.htmlparser.tags ImageTag))
  7.  
  8. (def *comics*
  9.   [{:name "Penny-Arcade"
  10.     :url "http://www.penny-arcade.com/comic/"
  11.     :regex #"images/\d{4}/.+?(?:png|gif|jpg)"
  12.     :prefix "http://www.penny-arcade.com/"
  13.     }
  14.    {:name "We The Robots"
  15.     :url "http://www.wetherobots.com/"
  16.     :regex #"comics/.+?[.](?:jpg|png|gif)"
  17.     }
  18.    {:name "Xkcd"
  19.     :url "http://www.xkcd.com"
  20.     :regex #"comics"
  21.     :type :tooltip-comic
  22.     }
  23.   ])
  24.  
  25. (defn fetch-url
  26.   "Return the web page as a string."
  27.   [address]
  28.   (let [url (URL. address)]
  29.     (with-open [stream (. url (openStream))]
  30.       (let [buf (BufferedReader. (InputStreamReader. stream))]
  31.         (apply str (line-seq buf))))))
  32.  
  33. (defmulti fetch-comic :type)
  34.  
  35. (defmethod fetch-comic :default [comic]
  36.   (let [src (fetch-url (:url comic))
  37.         image (re-find (:regex comic) src)]
  38.     (str (or (:prefix comic) (:url comic))
  39.          image)))
  40.  
  41. (defmethod fetch-comic :tooltip-comic [comic]
  42.   (let [img-tags (ref [])
  43.         parser (Parser. (:url comic))
  44.         visitor (proxy [NodeVisitor] []
  45.                   (visitTag [tag]
  46.                             (when (and (instance? ImageTag tag)
  47.                                        (re-find (:regex comic)
  48.                                                 (.getImageURL tag)))
  49.                               (dosync (alter img-tags conj tag)))))]
  50.     (.visitAllNodesWith parser visitor)
  51.     [(.getImageURL (first @img-tags))
  52.      (.getAttribute (first @img-tags) "title")]))
  53.  
  54. (doseq [comic *comics*]
  55.   (println (:name comic) ":" (fetch-comic comic)))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post