| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Antigate::V2; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 713 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 54 |  | 
| 4 | 2 |  |  | 2 |  | 473 | use JSON::PP; | 
|  | 2 |  |  |  |  | 10272 |  | 
|  | 2 |  |  |  |  | 208 |  | 
| 5 | 2 |  |  | 2 |  | 523 | use MIME::Base64; | 
|  | 2 |  |  |  |  | 1152 |  | 
|  | 2 |  |  |  |  | 110 |  | 
| 6 | 2 |  |  | 2 |  | 12 | use parent 'WebService::Antigate'; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 27 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub new { | 
| 9 | 1 |  |  | 1 | 1 | 927 | my ($class, %args) = @_; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | # change some defaults | 
| 12 | 1 | 50 |  |  |  | 9 | $args{scheme} = 'https'   unless defined $args{scheme}; | 
| 13 | 1 | 50 |  |  |  | 7 | $args{subdomain} = 'api.' unless defined $args{subdomain}; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 1 |  |  |  |  | 16 | $class->SUPER::new(%args); | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub try_upload { | 
| 19 | 1 |  |  | 1 | 1 | 3 | my ($self, %opts) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 | 50 |  |  |  | 4 | if ( defined $opts{file} ) { | 
| 22 | 1 |  |  |  |  | 1 | $opts{content} = do { | 
| 23 | 1 |  |  |  |  | 4 | local $/; | 
| 24 |  |  |  |  |  |  | open my $fh, '<:raw', $opts{file} | 
| 25 | 1 | 50 |  |  |  | 29 | or Carp::croak "open `$opts{file}': ", $!; | 
| 26 | 1 |  |  |  |  | 18 | <$fh>; | 
| 27 |  |  |  |  |  |  | }; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 1 | 50 |  |  |  | 4 | if ( defined $opts{content} ) { | 
| 31 | 1 |  |  |  |  | 10 | $opts{body} = encode_base64( $opts{content}, '' ); | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 1 | 50 | 33 |  |  | 6 | if ( defined $opts{body} && !defined $opts{type} ) { | 
| 35 | 1 |  |  |  |  | 2 | $opts{type} = 'ImageToTextTask'; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | my $response = $self->{ua}->post( | 
| 39 |  |  |  |  |  |  | "$self->{scheme}://$self->{subdomain}$self->{domain}/createTask", | 
| 40 |  |  |  |  |  |  | Content => encode_json { | 
| 41 |  |  |  |  |  |  | clientKey => $self->{key}, | 
| 42 |  |  |  |  |  |  | exists $opts{softId} ? ( softId => delete $opts{softId} ) : (), | 
| 43 | 1 | 50 |  |  |  | 21 | exists $opts{languagePool} ? ( languagePool => delete $opts{languagePool} ) : (), | 
|  |  | 50 |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | task => { | 
| 45 |  |  |  |  |  |  | %opts | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 | 50 |  |  |  | 126500 | unless($response->is_success) { | 
| 51 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 52 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 53 | 0 |  |  |  |  | 0 | return undef; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 1 |  |  |  |  | 24 | my $result = decode_json $response->decoded_content; | 
| 57 | 1 | 50 |  |  |  | 12092 | if ($result->{errorId}) { | 
| 58 | 0 |  |  |  |  | 0 | $self->{errno} = $result->{errorCode}; | 
| 59 | 0 |  |  |  |  | 0 | $self->{errstr} = $result->{errorDescription}; | 
| 60 | 0 |  |  |  |  | 0 | return undef; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 1 |  |  |  |  | 28 | return $self->{last_captcha_id} = $result->{taskId}; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub try_recognize { | 
| 67 | 5 |  |  | 5 | 1 | 28 | my ($self, $id) = @_; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 5 | 50 |  |  |  | 36 | Carp::croak "Captcha id should be specified" unless defined $id; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | my $response = $self->{ua}->post( | 
| 72 |  |  |  |  |  |  | "$self->{scheme}://$self->{subdomain}$self->{domain}/getTaskResult", | 
| 73 |  |  |  |  |  |  | Content => encode_json { | 
| 74 |  |  |  |  |  |  | clientKey => $self->{key}, | 
| 75 | 5 |  |  |  |  | 93 | taskId    => $id | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | ); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 5 | 50 |  |  |  | 51203 | unless($response->is_success) { | 
| 80 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 81 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 82 | 0 |  |  |  |  | 0 | return undef; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 5 |  |  |  |  | 98 | my $result = decode_json $response->decoded_content; | 
| 86 | 5 | 100 |  |  |  | 7709 | if ($result->{errorId}) { | 
| 87 | 1 |  |  |  |  | 9 | $self->{errno} = $result->{errorCode}; | 
| 88 | 1 |  |  |  |  | 6 | $self->{errstr} = $result->{errorDescription}; | 
| 89 | 1 |  |  |  |  | 58 | return undef; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 4 | 100 |  |  |  | 18 | if ($result->{status} ne 'ready') { | 
| 93 | 2 |  |  |  |  | 6 | $self->{errno}  = 'CAPCHA_NOT_READY'; | 
| 94 | 2 |  |  |  |  | 6 | $self->{errstr} = 'captcha is not recognized yet'; | 
| 95 | 2 |  |  |  |  | 48 | return undef; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 2 |  |  |  |  | 6 | for my $key ( qw/text gRecaptchaResponse token/ ) { | 
| 99 | 2 | 50 |  |  |  | 38 | return $result->{solution}{$key} if exists $result->{solution}{$key}; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  | 0 | return $result->{solution}; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub abuse { | 
| 106 | 2 |  |  | 2 | 1 | 11 | my ($self, $id) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 2 | 50 |  |  |  | 13 | Carp::croak "Captcha id should be specified" unless defined $id; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | my $response = $self->{ua}->post( | 
| 111 |  |  |  |  |  |  | "$self->{scheme}://$self->{subdomain}$self->{domain}/reportIncorrectImageCaptcha", | 
| 112 |  |  |  |  |  |  | Content => encode_json { | 
| 113 |  |  |  |  |  |  | clientKey => $self->{key}, | 
| 114 | 2 |  |  |  |  | 41 | taskId    => $id | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | ); | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 2 | 50 |  |  |  | 27730 | unless($response->is_success) { | 
| 119 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 120 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 121 | 0 |  |  |  |  | 0 | return undef; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 2 |  |  |  |  | 55 | my $result = decode_json $response->decoded_content; | 
| 125 | 2 | 100 |  |  |  | 5013 | if ($result->{errorId}) { | 
| 126 | 1 | 50 |  |  |  | 10 | if ($result->{errorCode}) { | 
| 127 | 1 |  |  |  |  | 10 | $self->{errno} = $result->{errorCode}; | 
| 128 | 1 |  |  |  |  | 75 | $self->{errstr} = $result->{errorDescription}; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | else { | 
| 131 | 0 |  |  |  |  | 0 | $self->{errno} = 'ERROR_NO_SUCH_CAPCHA_ID'; | 
| 132 | 0 |  |  |  |  | 0 | $self->{errstr} = 'no such captcha id in the database'; | 
| 133 |  |  |  |  |  |  | } | 
| 134 | 1 |  |  |  |  | 70 | return undef; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 1 |  |  |  |  | 39 | return $result->{status}; | 
| 138 |  |  |  |  |  |  | } | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | sub balance { | 
| 141 | 2 |  |  | 2 | 1 | 9 | my $self = shift; | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | my $response = $self->{ua}->post( | 
| 144 |  |  |  |  |  |  | "$self->{scheme}://$self->{subdomain}$self->{domain}/getBalance", | 
| 145 |  |  |  |  |  |  | Content => encode_json { | 
| 146 |  |  |  |  |  |  | clientKey => $self->{key}, | 
| 147 |  |  |  |  |  |  | } | 
| 148 | 2 |  |  |  |  | 35 | ); | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 2 | 50 |  |  |  | 20784 | unless($response->is_success) { | 
| 151 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 152 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 153 | 0 |  |  |  |  | 0 | return undef; | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 2 |  |  |  |  | 47 | my $result = decode_json $response->decoded_content; | 
| 157 | 2 | 100 |  |  |  | 3710 | if ($result->{errorId}) { | 
| 158 | 1 |  |  |  |  | 6 | $self->{errno} = $result->{errorCode}; | 
| 159 | 1 |  |  |  |  | 5 | $self->{errstr} = $result->{errorDescription}; | 
| 160 | 1 |  |  |  |  | 21 | return undef; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 1 |  |  |  |  | 29 | return $result->{balance}; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | 1; | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | __END__ |