-
-
Save jrheard/f1d3fc8b7e4d45e5a0f5 to your computer and use it in GitHub Desktop.
finalized dom-logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns dom-logic.core | |
(:refer-clojure :exclude [==]) | |
(:require [clojure.core.logic :refer :all] | |
[clj-http.client :as client] | |
[pl.danieljanus.tagsoup :as tagsoup])) | |
(defn get-parsed-html [url] | |
(-> url | |
client/get | |
:body | |
tagsoup/parse-string)) | |
; Parsed html nodes look like: | |
; [:div {:class foo :other-attrs bar} &children] | |
(defne nodes-existo [condition nodes out] | |
([_ [?node . ?remaining-nodes] _] | |
(conde | |
[(condition ?node) (== ?node out)] | |
[(nodes-existo condition ?remaining-nodes out)] | |
[(matche [?node] | |
([[_ _ . ?children]] (nodes-existo condition ?children out)))]))) | |
(defne has-attrso [node attrs] | |
([[_ ?node-attrs . _] _] | |
(featurec ?node-attrs attrs))) | |
(defn get-elements-by-attrs [dom attrs] | |
(run* [q] | |
(nodes-existo #(has-attrso % attrs) dom q))) | |
(defn -main [] | |
(let [dom (get-parsed-html "http://www.yelp.com")] | |
(get-elements-by-attrs dom {:class "language-items"}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment