(import '(org.htmlparser Parser)
'(org.htmlparser.visitors NodeVisitor)
'(org.htmlparser.tags Div Span))
(defn- metacritic-node-visitor [tag]
(when (and (instance? Div tag)
(= (.getAttribute tag "id") "metascore"))
(.getChildrenHTML tag)))
(defn- rotten-node-visitor [tag]
(when (and (instance? Span tag)
(= (.getAttribute tag "class") "percent")
(= (.getAttribute tag "property") "v:average"))
(.getChildrenHTML tag)))
(defn- find-score [url node-visitor-fn]
(let [score (ref nil)
parser (Parser. url)
visitor (proxy [NodeVisitor] []
(visitTag [tag]
(if-let [found-score (node-visitor-fn tag)]
(dosync (ref-set score found-score)))))]
(.visitAllNodesWith parser visitor)
@score))
(defmulti fetch-score #(first (re-seq #"rottentomatoes|metacritic" %)))
(defmethod fetch-score "metacritic" [url]
(find-score url metacritic-node-visitor))
(defmethod fetch-score "rottentomatoes" [url]
(find-score url rotten-node-visitor))
(defmethod fetch-score :default [url])
(dorun (map #(println (fetch-score %)) *command-line-args*))