Skip to content

Instantly share code, notes, and snippets.

@jrheard
Last active December 13, 2015 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrheard/f1d3fc8b7e4d45e5a0f5 to your computer and use it in GitHub Desktop.
Save jrheard/f1d3fc8b7e4d45e5a0f5 to your computer and use it in GitHub Desktop.
finalized dom-logic
(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