The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WebService::Antigate - Recognition of captches using antigate.com service (now anti-captcha.com)

SYNOPSIS

Simplest variant
 use WebService::Antigate;

 my $recognizer = WebService::Antigate->new(key => 'd41d8cd98f00b204e9800998ecf8427e');
 
 my $captcha = $recognizer->upload_and_recognize(file => "/tmp/Captcha.jpeg")
       or die $recognizer->errstr;

 print "Recognized captcha is: ", $captcha, "\n";
More control
 use WebService::Antigate;

 my $recognizer = WebService::Antigate->new(key => 'd41d8cd98f00b204e9800998ecf8427e', attempts => 25);

 my $id = $recognizer->upload(file => '/tmp/Captcha.jpeg');
 unless($id)
 {
       die "Error while uploading captcha: ", $recognizer->errno, " (", $recognizer->errstr, ")";
 }

 my $captcha = $recognizer->recognize($id);
 unless($captcha)
 {
       die "Error while recognizing captcha: ", $recognizer->errno, " (", $recognizer->errstr, ")";
 }

 print "Recognized captcha is: ", $captcha, "\n";
Control all operations yourself
 use WebService::Antigate;

 my $recognizer = WebService::Antigate->new(key => 'd41d8cd98f00b204e9800998ecf8427e');

 # will use captcha from variable, not from file for this example
 my $captcha_img = $recognizer->ua->get('http://some-site.com/captcha.php?id=4')->content;

 my $id;
 until($id = $recognizer->try_upload(content => $captcha_img))
 {
       warn "not uploaded yet: ", $recognizer->errno;
       sleep 5;
 }

 my $captcha;
 until($captcha = $recognizer->try_recognize($id))
 {
       warn "not recognized yet: ", $recognizer->errno;
       sleep 5;
 }

 print "Recognized captcha is: ", $captcha, "\n";
Using API v2 to recognize recaptcha
  use WebService::Antigate;
  
  my $recognizer = WebService::Antigate->new(key => 'd41d8cd98f00b204e9800998ecf8427e', api_version => 2);
  my $recaptcha_token = $recognizer->upload_and_recognize(
        type       => 'NoCaptchaTaskProxyless',
        websiteURL => "https://www.google.com/",
        websiteKey => "6LeZhwoTAAAAAP51ukBEOocjtdKGRDei9wFxFSpm"
  );
  
  warn $recaptcha_token;

DESCRIPTION

The WebService::Antigate is a class for captcha text recognition. It uses the API of anti-captcha.com service (versions 1 and 2 of API supported). You have to register with anti-captcha.com to obtain you private key. Thereafter you can upload captcha image to this service using this class and receive captcha text already recognized. Be aware not to use this service for any illegal activities.

METHODS

WebService::Antigate->new( %options )

This method constructs new object of WebService::Antigate::V1 or WebService::Antigate::V2 depending on api_version specified. Key / value pairs can be passed as an argument to specify the initial state. The following options correspond to attribute methods described below:

   KEY                  DEFAULT                                                OPTIONAL
   -----------          --------------------                                 ---------------
   key                   undef                                                 NO
   api_version           1                                                     yes
   ua                    LWP::UserAgent->new                                   yes
   scheme                http                                                  yes
   domain                $WebService::Antigate::DOMAIN = 'anti-captcha.com'    yes
   subdomain             undef                                                 yes
   wait                  $WebService::Antigate::WAIT = 220                     yes
   attempts              undef                                                 yes
   delay                 $WebService::Antigate::DELAY = 5                      yes

Options description:

   key         - your service private key
   api_version - version of API to use: 1 or 2
   ua          - LWP::UserAgent object used to upload captcha and receive the result (captcha recognition)
   scheme      - http or https, default value may be different in subclasses
   domain      - current domain of the service, can be changed in the future
   subdomain   - current subdomain of domain of the service (with dot at the end), default value may be different in subclasses
   wait        - maximum waiting time until captcha will be accepted  ( upload() ) or recognized ( recognize() ) by the service
   delay       - delay time before next attempt of captcha uploading or recognition after previous failure
   attempts    - maximum number of attempts that we can try_upload() or try_recognize()
   

If you specify `wait' and `attempts' options at the same time, than class will try to upload/recognize until time or attempts will over (which first). If you do not specify neither `wait', nor `attempts', than default value of `wait' will be used.

$recognizer->key
$recognizer->key($key)

This method gets or sets your service private key.

$recognizer->ua
$recognizer->ua($ua)

This method gets or sets an LWP::UserAgent object associated with class. Thus we can configure this object: set proxy, etc. See LWP::UserAgent for details.

Example:

   $recognizer->ua->proxy(http => 'http://localhost:8080');
$recognizer->scheme
$recognizer->scheme($scheme)

This method gets or sets API url scheme.

$recognizer->domain
$recognizer->domain($domain)

This method gets or sets the domain of the service.

$recognizer->subdomain
$recognizer->subdomain($domain)

This method gets or sets subdomain of the service (with dot at the end).

$recognizer->wait
$recognizer->wait($time)

This method gets or sets maximum waiting time. See above.

$recognizer->delay
$recognizer->delay($time)

This method gets or sets delay time before next attempt. See above.

$recognizer->attempts
$recognizer->attempts($attempts)

This method gets or sets maximum number of attempts. See above.

$recognizer->errno

This method gets an error from previous unsuccessful operation. The Error is returned as a string constant associated with this error type. For example:

  'ERROR_KEY_DOES_NOT_EXIST'
  'ERROR_WRONG_USER_KEY'
  'ERROR_NO_SLOT_AVAILABLE'
  'ERROR_ZERO_CAPTCHA_FILESIZE'
  'ERROR_TOO_BIG_CAPTCHA_FILESIZE'
  'ERROR_WRONG_FILE_EXTENSION'
  'ERROR_IP_NOT_ALLOWED'
  'ERROR_WRONG_ID_FORMAT'
  'ERROR_NO_SUCH_CAPCHA_ID'
  'ERROR_URL_METHOD_FORBIDDEN'
  'ERROR_IMAGE_IS_NOT_PNG'
  'ERROR_IMAGE_IS_NOT_JPEG'
  'ERROR_IMAGE_IS_NOT_GIF'
  'ERROR_ZERO_BALANCE'
  'ERROR_CAPTCHA_UNSOLVABLE'
  'ERROR_BAD_DUPLICATES'
  'HTTP_ERROR'

For complete list of errors see API documentation

$recognizer->errstr

This method gets an error from previous unsuccessful operation. The Error is returned as a string which describes the problem.

$recognizer->try_upload(%options)

This method tries to upload captcha image (if captcha is image related) to the service or just creates task (if not) on the service. For available options see speecific API version: "try_upload" in WebService::Antigate::V1 or "try_upload" in WebService::Antigate::V2. On success will return captcha id. On failure returns undef and sets errno and errstr.

$recognizer->upload(%options)

This method attempts to upload a captcha image to the service until exceeds allotted time limit or attempts or a captcha will not be uploaded. The parameter %options is identical with the one in method try_upload(). On success will return captcha id. On failure returns undef and sets errno and errstr.

$recognizer->try_recognize($captcha_id)

This method tries to recognize captcha (get result for previously uploaded captcha) with id $captcha_id - value returned by method upload() or try_upload(). On success will return recognized captcha text. On failure returns undef and sets errno and errstr.

$recognizer->recognize($captcha_id)

This method tries to recognize captcha as it does method try_recognize() but will make attempts until time limit or attempts exceeds or captcha will be recognized. On success will return recognized captcha text. On failure returns undef and sets errno and errstr.

$recognizer->upload_and_recognize(%options)

This method uploads and recognizes captcha in one step. It is easier but less flexible. Parameter %options is identical with the one in method try_upload(). On success will return recognized captcha text. On failure returns undef and sets errno and errstr.

$recognizer->last_captcha_id

This method returns id of the last successfully uploaded captcha. This can be useful when you used upload_and_recognize() and then want to use some method that accepts captcha id as argument (abuse() for example).

$recognizer->abuse($captcha_id)

This method sends a message to the service that captcha with $captcha_id was recognized incorrectly. On success will return a true value. On failure returns undef and sets errno and errstr.

$recognizer->balance()

This method gets user balance. On success will return user balance as a float number. On failure returns undef and sets errno and errstr.

PACKAGE VARIABLES

$WebService::Antigate::DOMAIN = 'anti-captcha.com'; # service domain often changes because of the abuse

$WebService::Antigate::WAIT = 220; # default time that recognize() or upload() can work

$WebService::Antigate::DELAY = 5; # sleep time before retry while uploading or recognizing captcha

$WebService::Antigate::FNAME = 'captcha.jpg'; # default name of the uploaded captcha if name not specified and can't be determined

SEE ALSO

WebService::Antigate::V1, WebService::Antigate::V2

COPYRIGHT

Oleg G <oleg@cpan.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.