Skip to content

Instantly share code, notes, and snippets.

@demetriusnunes
Created October 17, 2011 17:51
Show Gist options
  • Save demetriusnunes/1293256 to your computer and use it in GitHub Desktop.
Save demetriusnunes/1293256 to your computer and use it in GitHub Desktop.
Validador de redirects em clojure
(ns ego-checker.core
(:require [clj-http.client :as http]
[clojure.data.csv :as csv]
[clojure.java.io :as io]))
(def *file-urls* "lista-urls.txt")
(def *log-file-path* "error.log")
(defn test-url [url]
(try (let [resp (http/get url {:follow-redirects false})]
{:status (resp :status) :location ((resp :headers) "location")})
(catch Exception e {:status 1000 :location "Error"})))
(defn url-status [url]
(:status (test-url url)))
(defn match-redirect? [url-para {:keys [status location]}]
(and (= 301 status)
(= url-para location)
(= 200 (url-status url-para))))
(defn- error-log [& msg]
(spit *log-file-path* (apply str msg) :append true))
(defn -main []
(with-open [in-file (io/reader *file-urls*)]
(doseq [[url-de url-para] (csv/read-csv in-file)]
(let [resp (test-url url-de)]
(if (match-redirect? url-para resp)
(println "SUCESSO! DE:" url-de "PARA:" url-para)
(do
(println "ERRO! DE:" url-de "PARA:" url-para)
(error-log "URL_DE: " url-de " - URL_PARA: " url-para " - DEBUG: " resp "\n")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment