Created
June 8, 2012 08:18
-
-
Save lstoll/2894438 to your computer and use it in GitHub Desktop.
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
;; Totally untested, but should give an idea. | |
(ns mn | |
(:use lstoll.utils) ; https://github.com/lstoll/clj-utils | |
(:require [clj-http.client :as http])) ; https://github.com/dakrone/clj-http | |
(def test-concurrency 10) | |
(defn test-url | |
"Tests a URL, returning it's status code" | |
[url] | |
(:status (http/get url {:throw-exceptions false}))) ; Does a HTTP get, getting the results status | |
(defn test-urls | |
"Taks a seq of URLs. Returns a lazy seq of results {:url URL :status HTTP-STATUS}" | |
[urls-to-test] | |
(pmap2 test-concurrency ; call to pmap2. Set concurrency to the saved var | |
(fn [u] {:url u :status (test-url u)}) ; Map function - call test-url, return with URL in map | |
urls-to-test)) ; Run this on the param | |
(defn broken-urls | |
"Prints broken URLS." | |
[urls] | |
;; Force evaluation of a sequence. The binding vector is like let. | |
(doseq [err-url (filter #(>= (:status %) 500) ; filter for all 500+ status codes | |
(test-urls urls))] ; in the result of testing all the urls | |
(println (str "URL " (:url err-url) " is returning a status of " (:status err-url))))) | |
(broken-urls ["http://google.com" "http://microsoft.com"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment