Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored to a unifying $ selector

added yahoo! baseball-specific selectors
  • Loading branch information...
commit 2b9c6002b27d4b1f8f9a85c38ca6b8bc979397e8 1 parent e27a351
@youngnh authored
View
18 html-selector/src/twoguys/baseball_selector.clj
@@ -0,0 +1,18 @@
+(ns twoguys.baseball-selector
+ (:use twoguys.html-selector))
+
+(defn player-line-sel [$table]
+ (for [$row ($ $table "tbody" "tr")]
+ (let [$row (list $row)
+ pos ($ $row ".pos")
+ name ($ $row ".player" ".name")
+ stats ($ $row ".stat")]
+ (concat pos name stats))))
+
+(defn matchup-line-sel [$table] ;; runs over a single ($ #matchup-summary-table tbody tr)
+ (for [$row ($ $table "tbody" "tr")]
+ (let [$row (list $row)]
+ ($ $row "td"))))
+
+(defn minimum-innings-sel [document]
+ ($ document "#minreached" "p"))
View
43 html-selector/src/twoguys/html_selector.clj
@@ -20,27 +20,30 @@
(cons (.item node-list i) (internal (inc i))))))]
(internal 0)))
-(defmulti element-sel (fn [context elt-name]
- (condp instance? context
- Document Document
- :default)))
-
-(defmethod element-sel Document [document elt-name]
- (nodelist-seq (.getElementsByTagName document elt-name)))
-
-(defmethod element-sel :default [nodes elt-name]
+(defn selector [nodes pred]
(let [children (apply concat
(for [node nodes]
(nodelist-seq (.getChildNodes node))))]
(lazy-cat
- (filter #(= elt-name (element-tagname %)) children)
+ (filter pred children)
(when-not (empty? children)
- (element-sel children elt-name)))))
+ (selector children pred)))))
(defn element-tagname [elt]
(when (= Node/ELEMENT_NODE (.getNodeType elt))
(.getNodeName elt)))
+(defmulti element-sel (fn [context elt-name]
+ (condp instance? context
+ Document Document
+ :default)))
+
+(defmethod element-sel Document [document elt-name]
+ (nodelist-seq (.getElementsByTagName document elt-name)))
+
+(defmethod element-sel :default [nodes elt-name]
+ (selector nodes #(= elt-name (element-tagname %))))
+
(defn get-attribute [elt attr]
(.?. elt getAttributes (getNamedItem attr) getValue))
@@ -49,11 +52,13 @@
(some #(= class %) (split class-attr #" "))))
(defn class-sel [nodes class]
- (let [class-name (.substring class 1)
- children (apply concat
- (for [node nodes]
- (nodelist-seq (.getChildNodes node))))]
- (lazy-cat
- (filter #(hasclass? % class-name) children)
- (when-not (empty? children)
- (class-sel children class)))))
+ (selector nodes #(hasclass? % (.substring class 1))))
+
+(defn compile-selector [s]
+ (condp = (.charAt s 0)
+ \# #(id-sel % s)
+ \. #(class-sel % s)
+ #(element-sel % s)))
+
+(defn $ [context & selectors]
+ (reduce (fn [c f-sel] (f-sel c)) context (map compile-selector selectors)))
View
25 html-selector/test/twoguys/baseball_selector.clj
@@ -0,0 +1,25 @@
+(ns twoguys.baseball-selector
+ (:use [twoguys.baseball-selector] :reload-all)
+ (:use [clojure.test]))
+
+(deftest test-player-line-selector
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")
+ $batter-rows ($ document "#statTable1")
+ $pitcher-rows ($ document "#statTable3")]
+ (testing "batters"
+ (is (= ["C" "Yadier Molina" "-" "-" "-" "-" "-"]
+ (map #(.getTextContent %) (first (player-line-sel $batter-rows))))))
+ (testing "pitchers"
+ (is (= ["P" "Jonathan Broxton" "0" "0.00" "3.00" "16.20" ".286"]
+ (map #(.getTextContent %) (first (player-line-sel $pitcher-rows))))))))
+
+(deftest test-matchup-line-selector
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")
+ $table ($ document "#matchup-summary-table")]
+ (is (= ["Takeit2thebank" "31" "32" "21" "91" ".345" "5" "2.61" "2.64" "6.87" ".284" "3"]
+ (map #(.getTextContent %) (first (matchup-line-sel $table)))))))
+
+(deftest test-minimum-innings-sel
+ (let [document (build-document "data/matchup_mid1_mid2_week1.html")]
+ (is (= "Takeit2thebank did not meet minimum requirements."
+ (.getTextContent (first (minimum-innings-sel document)))))))
View
16 html-selector/test/twoguys/html_selector.clj
@@ -1,4 +1,4 @@
-(ns twoguys.html-selector-test
+(ns twoguys.html-selector
(:use [twoguys.html-selector] :reload-all)
(:use [clojure.test])
(:import [org.w3c.dom Document]))
@@ -8,13 +8,13 @@
(deftest test-element-tagname
(let [document (build-document "data/matchup_mid1_mid2_week1.html")
- table-elts (element-sel document "table")]
+ table-elts ($ document "table")]
(is (= "table" (element-tagname (first table-elts))))))
(deftest test-element-sel
(let [document (build-document "data/matchup_mid1_mid2_week1.html")
- $matchup-summary (id-sel document "#matchup-summary")
- $matchup (id-sel document "#matchup")]
+ $matchup-summary ($ document "#matchup-summary")
+ $matchup ($ document "#matchup")]
(testing "given a Document, the context is the whole DOM"
(is (= 13 (count (element-sel document "table")))))
@@ -26,9 +26,7 @@
(deftest test-class-sel
(let [document (build-document "data/matchup_mid1_mid2_week1.html")
- $statTable1 (id-sel document "#statTable1")
- $rows (-> $statTable1
- (element-sel "tbody")
- (element-sel "tr"))]
+ $statTable1 ($ document "#statTable1")
+ $rows ($ $statTable1 "tbody" "tr")]
(is (= ["C" "1B" "2B" "3B" "SS" "LF" "CF" "RF" "Util" "BN" "BN" "BN" "BN" "BN" "BN" "--" "--" "--"]
- (map #(.getTextContent %) (class-sel $rows ".pos"))))))
+ (map #(.getTextContent %) (class-sel $rows ".pos"))))))
Please sign in to comment.
Something went wrong with that request. Please try again.