Safe Haskell | None |
---|
Example:
import Text.Recognition.Antigate import Data.Default import Network import Control.Monad import Control.Monad.IO.Class import Data.ByteString hiding (putStrLn) import System.Timeout myAntigateKey :: String myAntigateKey = "0123456789abcdef0123456789abcdef" downloadJpegCaptcha :: Manager -> IO ByteString downloadJpegCaptcha = undefined answerCaptcha :: String -> Manager -> IO Bool answerCaptcha = undefined main :: IO () main = withSocketsDo $ do res <- timeout (30*1000000) $ withManager $ \m -> do bytes <- liftIO $ downloadJpegCaptcha m (id, answer) <- solveCaptcha (3*1000000) (3*1000000) myAntigateKey def{phrase=True} "captcha.jpg" bytes m res <- liftIO $ answerCaptcha answer m unless res $ reportBad myAntigateKey id m return res case res of Nothing -> do putStrLn "Timed out" Just True -> do putStrLn "Solved successfully" Just False -> do putStrLn "Couldn't solve"
- type AntigateKey = String
- type CaptchaID = Int
- data CaptchaConf = CaptchaConf {}
- data UploadResult
- = UPLOAD_OK CaptchaID
- | ERROR_WRONG_USER_KEY
- | UPLOAD_ERROR_KEY_DOES_NOT_EXIST
- | ERROR_ZERO_BALANCE
- | ERROR_NO_SLOT_AVAILABLE
- | ERROR_ZERO_CAPTCHA_FILESIZE
- | ERROR_TOO_BIG_CAPTCHA_FILESIZE
- | ERROR_WRONG_FILE_EXTENSION
- | ERROR_IMAGE_TYPE_NOT_SUPPORTED
- | ERROR_IP_NOT_ALLOWED
- | UPLOAD_ERROR_UNKNOWN String
- data CheckResult
- data SolveException
- solveCaptcha :: (MonadBaseControl IO m, MonadResource m) => Int -> Int -> AntigateKey -> CaptchaConf -> FilePath -> ByteString -> Manager -> m (CaptchaID, String)
- solveCaptchaFromFile :: (MonadBaseControl IO m, MonadResource m) => Int -> Int -> AntigateKey -> CaptchaConf -> FilePath -> Manager -> m (CaptchaID, String)
- uploadCaptcha :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaConf -> FilePath -> ByteString -> Manager -> m UploadResult
- checkCaptcha :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaID -> Manager -> m CheckResult
- checkCaptchas :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> [CaptchaID] -> Manager -> m [CheckResult]
- reportBad :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaID -> Manager -> m ()
- getBalance :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> Manager -> m Double
- data Manager
- newManager :: ManagerSettings -> IO Manager
- closeManager :: Manager -> IO ()
- withManager :: (MonadIO m, MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m) => (Manager -> ResourceT m a) -> m a
- parseUploadResult :: String -> UploadResult
- parseCheckResult :: String -> CheckResult
- parseCheckResults :: String -> [CheckResult]
- parseCheckResultNoOK :: String -> CheckResult
- renderUploadResult :: UploadResult -> String
- renderCheckResult :: CheckResult -> String
Documentation
type AntigateKey = StringSource
data CaptchaConf Source
CaptchaConf | |
|
data UploadResult Source
UPLOAD_OK CaptchaID | result is positive, your captcha is accepted for recognition and its ID follows. You may now attempt to retrieve captcha status with this ID. |
ERROR_WRONG_USER_KEY | user authorization key is invalid (its length is not 32 bytes as it should be) |
UPLOAD_ERROR_KEY_DOES_NOT_EXIST | you have set wrong user authorization key in request |
ERROR_ZERO_BALANCE | account has zero or negative balance |
ERROR_NO_SLOT_AVAILABLE | no idle captcha workers are available at the moment, please try a bit later or try increasing your bid |
ERROR_ZERO_CAPTCHA_FILESIZE | the size of the captcha you are uploading or pointing to is zero |
ERROR_TOO_BIG_CAPTCHA_FILESIZE | your captcha size is exceeding 100kb limit |
ERROR_WRONG_FILE_EXTENSION | your captcha file has wrong extension, the only allowed extensions are gif,jpg,jpeg,png |
ERROR_IMAGE_TYPE_NOT_SUPPORTED | Could not determine captcha file type, only allowed formats are JPG, GIF, PNG |
ERROR_IP_NOT_ALLOWED | Request with current account key is not allowed from your IP. Please refer to IP list section |
UPLOAD_ERROR_UNKNOWN String |
data CheckResult Source
CHECK_OK String | the captcha is recognized, the guessed text follows |
CAPCHA_NOT_READY | captcha is not recognized yet, repeat request withing 1-5 seconds |
CHECK_ERROR_KEY_DOES_NOT_EXIST | you have set wrong user authorization key in request |
ERROR_WRONG_ID_FORMAT | the captcha ID you are sending is non-numeric |
CHECK_ERROR_UNKNOWN String |
High level
data SolveException Source
:: (MonadBaseControl IO m, MonadResource m) | |
=> Int | how much to sleep while waiting for available slot. Microseconds. |
-> Int | how much to sleep between captcha checks. Microseconds. |
-> AntigateKey | |
-> CaptchaConf | |
-> FilePath | image filename (antigate guesses filetype by file extension) |
-> ByteString | image contents |
-> Manager | HTTP connection manager to use |
-> m (CaptchaID, String) |
High level function to solve captcha, blocks until answer is provided (about 2-10 seconds).
throws SolveException
or HttpException
when something goes wrong.
solveCaptchaFromFile :: (MonadBaseControl IO m, MonadResource m) => Int -> Int -> AntigateKey -> CaptchaConf -> FilePath -> Manager -> m (CaptchaID, String)Source
Same as solveCaptcha
, but read contents from a file.
Core functions
uploadCaptcha :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaConf -> FilePath -> ByteString -> Manager -> m UploadResultSource
upload captcha for recognition
throws HttpException
on network errors.
checkCaptcha :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaID -> Manager -> m CheckResultSource
retrieve captcha status
throws HttpException
on network errors.
checkCaptchas :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> [CaptchaID] -> Manager -> m [CheckResult]Source
retrieve multiple captcha status
throws HttpException
on network errors.
reportBad :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> CaptchaID -> Manager -> m ()Source
report bad captcha result
throws HttpException
on network errors.
getBalance :: (MonadBaseControl IO m, MonadResource m) => AntigateKey -> Manager -> m DoubleSource
retrieve your current account balance
throws HttpException
on network errors.
Connection manager
data Manager
Keeps track of open connections for keep-alive. May be used concurrently by multiple threads.
newManager :: ManagerSettings -> IO Manager
Create a Manager
. You must manually call closeManager
to shut it down.
closeManager :: Manager -> IO ()
withManager :: (MonadIO m, MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m) => (Manager -> ResourceT m a) -> m a
Create a new manager, use it in the provided function, and then release it.
This function uses the default manager settings. For more control, use
newManager
.
Miscellaneous
parseUploadResult :: String -> UploadResultSource
Parse antigate's upload response
parseCheckResult :: String -> CheckResultSource
Parse antigate's check response
parseCheckResults :: String -> [CheckResult]Source
Parse antigate's multi-check response
parseCheckResultNoOK :: String -> CheckResultSource
Parse antigate's multi-check response
renderUploadResult :: UploadResult -> StringSource
Marshal UploadResult back to its text form
renderCheckResult :: CheckResult -> StringSource
Marshal CheckResult back to its text form