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

(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="
                     out (io/output-stream (str dir c-num ".jpg"))]
           (io/copy in out))))))

Nenhum comentário:

Postar um comentário