Created
June 23, 2014 04:40
-
-
Save moserrya/3c9ba19f9e9279036b6f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 erdos-c.core | |
[:require [yokogiri.core :refer :all]]) | |
(def client (make-client :javascript false)) | |
(defn page [uri] | |
(get-page client (str "http://en.wikipedia.org" uri))) | |
(defn link-snippets [uri] | |
(xpath (page uri) "//a")) | |
(defn links [uri] | |
(->> uri | |
(link-snippets) | |
(map attrs) | |
(map :href) | |
(remove nil?))) | |
(defn wikifilter [uri] | |
(re-find #"\A\/wiki\/" uri)) | |
(defn wiki-links [uri] | |
(filter wikifilter (links uri))) | |
(defn search [start target] | |
(loop [queue (conj (clojure.lang.PersistentQueue/EMPTY) (list start)) | |
visited #{}] | |
(let [links (peek queue) | |
test-link (first links) | |
page-links (filter #(not (contains? visited %)) (wiki-links test-link))] | |
(if (some #(= target %) page-links) | |
links | |
(recur (pop (apply conj queue (map #(cons % links) page-links))) | |
(apply conj visited page-links)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment