Created
February 4, 2014 09:49
-
-
Save kawasima/8800811 to your computer and use it in GitHub Desktop.
CAPTCHA画像を予め作りおいていて、一定数以下になったら自動的に作り足します。ガラケーなどreCAPTCHAが使えないアプリにもCAPTCHAの機能を追加するための仕組みです。
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
(ns captcha-supplier.core | |
(:gen-class) | |
(:require [clojure.java.io :as io]) | |
(:import [com.google.code.kaptcha.util Config] | |
[java.util Properties] | |
[javax.imageio ImageIO] | |
[java.nio.file WatchService]) | |
(:use [nio2.io :only [path]] | |
[nio2.watch :only [watch-seq]])) | |
(def ^:const available-chars "abcdefghijklmnopqrstuvwxyz0123456789") | |
(defn create-captchas [image-dir n] | |
(dotimes [i n] | |
(let [props (Properties.) | |
config (Config. props) | |
producer (.getProducerImpl config) | |
rand-text (clojure.string/join (for [x (range 0 8)] (rand-nth available-chars))) | |
image-file (io/file image-dir (str rand-text ".jpg"))] | |
(with-open [out (io/output-stream image-file)] | |
(ImageIO/write (.createImage producer rand-text) | |
"jpg" out))))) | |
(defn- supply-captcha! [image-dir threshold] | |
(when (< (count (.listFiles image-dir)) threshold) | |
(create-captchas image-dir 100))) | |
(defn -main [& args] | |
(let [image-dir (io/file "captchas")] | |
(when-not (.exists image-dir) (.mkdirs image-dir)) | |
(supply-captcha! image-dir 1) | |
(doseq [ev (watch-seq (path (.getAbsolutePath image-dir)) :delete)] | |
(supply-captcha! image-dir 50)))) |
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
(defproject captcha-supplier "0.1.0" | |
:dependencies [[org.clojure/clojure "1.5.1"] | |
[info.hoetzel/clj-nio2 "0.1.1"] | |
[com.github.axet/kaptcha "0.0.9"]] | |
:aot :all | |
:main captcha.core) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment