terça-feira, 15 de maio de 2012

A recaptcha downloader in clojure.

A small snippet for the community and for myself in the future. I think the code is simple enough to be self explanatory.
(ns blanket.recaptcha-downloader
  (:require [clj-http.client :as client])
  (:use [clojure.java.io :as io]))

(def recaptcha-address
  "http://www.google.com/recaptcha/api/reload?c=03AHJ_VutgYGIAR51CgxyMvWU_8nVtoUhbTV9w3tCRIKvAxWPN7OMidVd8XCGH_gljkGAdXBvLC3axQ3n4ei4_pe6MFhw_WwhPkGEzPLqHYn1xqcAbE_SFs8zPtW47_yICuNmB35gp_ue8jLlpR6OqKTfoXH4D1fJLNA&k=6Ld4iQsAAAAAAM3nfX_K0vXaUudl2Gk0lpTF3REf&reason=r&type=image")

(defn fetch 
  "Downloads a number of recaptchas into a given directory, or data/
  as a default dir."
  ([N] (fetch N "data/"))
  ([N dir]
     (doseq [i (range N)]
       (let [c-num (second (re-find #"\'(.*)\',"
                                    (:body (client/get recaptcha-address))))]
         (with-open [in (io/input-stream
                         (str "http://www.google.com/recaptcha/api/image?c="
                              c-num))
                     out (io/output-stream (str dir c-num ".jpg"))]
           (io/copy in out))))))



Nenhum comentário:

Postar um comentário