Antigate.pm

filter: Statementstmt 86.4% 70 / 81 Branchbran 60.7% 17 / 28 Conditioncond 53.4% 23 / 43 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 68.8% 135 / 196 SCAR 38.9
CC
30
Cov
72%
upload
16.9
new
12.0
recognize
7.2
CRAP 49.0
↑ Summary Antigate/V2.pm »
1 package WebService::Antigate;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.11';
8  
9 4 our $DOMAIN = 'anti-captcha.com'; # service domain often changes because of the abuse
10 4 our $WAIT = 220; # default time that recognize() or upload() can work
11 4 our $DELAY = 5; # sleep time before retry while uploading or recognizing captcha
12 4 our $FNAME = 'captcha.jpg'; # default name of the uploaded captcha if name not specified and can't be determined
13  
14 my %API_VERSIONS = (
15 3 ▶ 1 => sub { require WebService::Antigate::V1; 'WebService::Antigate::V1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 ▶ 2 => sub { require WebService::Antigate::V2; 'WebService::Antigate::V2' },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 4 );
18  
19 sub new {
20 8 ▶ my ($class, %args) = @_;
Subroutine new: called CC=11 SCAR=24.9
Pod documented
21
22 8 ▶ Carp::croak "Option `key' should be specified" unless defined $args{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"key"} 1 7 8
23
24 7 my $self = {};
25 7 $self->{key} = $args{key};
26 7 $self->{wait} = $args{wait};
27 7 $self->{attempts} = $args{attempts};
28 7 ▶ $self->{ua} = $args{ua} || LWP::UserAgent->new();
Condition33%1/3
$args{'ua'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
070
MC/DC0%0/2
$args{'ua'} || 'LWP::UserAgent'->new
$args{'ua'} 'LWP::UserAgent'->new
Truth table33%1/3
A || B
A= $args{'ua'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
29 7 ▶ $self->{domain} = $args{domain} || $DOMAIN;
Condition66%2/3
$args{'domain'} || $WebService::Antigate::DOMAIN
l!l&&r!l&&!r
250
MC/DC0%0/2
$args{'domain'} || $WebService::Antigate::DOMAIN
$args{'domain'} $WebService::Antigate::DOMAIN
Truth table66%2/3
A || B
A= $args{'domain'}B= $WebService::Antigate::DOMAIN
ABresult
1X1
011
000
30 7 ▶ $self->{delay} = $args{delay} || $DELAY;
Condition66%2/3
$args{'delay'} || $WebService::Antigate::DELAY
l!l&&r!l&&!r
250
MC/DC0%0/2
$args{'delay'} || $WebService::Antigate::DELAY
$args{'delay'} $WebService::Antigate::DELAY
Truth table66%2/3
A || B
A= $args{'delay'}B= $WebService::Antigate::DELAY
ABresult
1X1
011
000
31 7 ▶ $self->{scheme} = $args{scheme} || 'http';
Condition100%2/2
$args{'scheme'} || 'http'
l!l
25
MC/DC100%1/1
$args{'scheme'} || 'http'
$args{'scheme'}
Truth table100%2/2
A
A= $args{'scheme'}
Aresult
11
00
32 7 $self->{subdomain} = $args{subdomain};
33 7 ▶ $self->{api_version} = $args{api_version} || 1;
Condition100%2/2
$args{'api_version'} || 1
l!l
34
MC/DC100%1/1
$args{'api_version'} || 1
$args{'api_version'}
Truth table100%2/2
A
A= $args{'api_version'}
Aresult
11
00
34
35 7 ▶ $self->{wait} = $WAIT unless defined($self->{wait}) || defined($self->{attempts});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"wait"} or defined $self->{"attempts"} 7 0 7
Condition33%1/3
defined $self->{"wait"} or defined $self->{"attempts"}
l!l&&r!l&&!r
007
MC/DC0%0/2
defined $self->{"wait"} or defined $self->{"attempts"}
defined $self->{"wait"} defined $self->{"attempts"}
Truth table33%1/3
A or B
A= defined $self->{"wait"}B= defined $self->{"attempts"}
ABresult
1X1
011
000
36
37 7 ▶ Carp::croak "Unsupported `api_version' specified" unless exists $API_VERSIONS{ $self->{api_version} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $API_VERSIONS{$self->{"api_version"}} 0 7 7
38 7 ▶ bless $self, $class eq __PACKAGE__ ? $API_VERSIONS{ $self->{api_version} }->() : $class;
Branch: 1 branch
BranchTrueFalseTotal
$class eq __PACKAGE__ ? : 5 2 7
39 }
40  
41 # generate sub's for get/set object properties using closure
42 4 foreach my $key (qw(key wait attempts ua scheme domain subdomain delay)) {
43 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 *$key = sub {
45 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=8.1
46
47 2 ▶ return $self->{$key} = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 2 0 2
48 0 return $self->{$key};
49 }
50 32 }
51  
52 sub errno {
53 4 ▶ return $_[0]->{errno};
Subroutine errno: called CC=1 SCAR=0.0
Pod documented
54 }
55  
56 sub errstr {
57 1 ▶ return $_[0]->{errstr};
Subroutine errstr: called CC=1 SCAR=0.0
Pod documented
58 }
59  
60 sub upload {
61 3 ▶ my ($self, %opts) = @_;
Subroutine upload: called CC=8 SCAR=28.3
Pod documented
62
63 3 my $start = time();
64 3 my $attempts = 0;
65 3 my $captcha_id;
66
67 do {
68 3 $attempts ++;
69 3 $captcha_id = $self->try_upload(%opts);
70 }
71 while ( !$captcha_id &&
72 ($self->{errno} eq 'ERROR_NO_SLOT_AVAILABLE' || $self->{errno} eq 'HTTP_ERROR') &&
73 (defined($self->{wait}) ? ( time() - $start ) < $self->{wait} : 1) &&
74 $attempts != $self->{attempts} &&
75 sleep( $self->{delay} )
76 3 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"} and sleep $self->{"delay"} and do { do { ++$attempts; $captcha_id = $self->try_upload(%opts) } } 0 3 3
defined $self->{'wait'} ? : 0 0 0
Condition26%4/15
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"} and sleep $self->{"delay"}
!ll&&!rl&&r
300
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"}
!ll&&!rl&&r
300
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1
!ll&&!rl&&r
300
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR"
!ll&&!rl&&r
300
$self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" or $self->{"errno"} eq "HTTP_ERROR"
l!l&&r!l&&!r
000
MC/DC0%0/6
not $captcha_id and $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"} and sleep $self->{"delay"}
not $captcha_id $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE" $self->{"errno"} eq "HTTP_ERROR" defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 $attempts != $self->{"attempts"} sleep $self->{"delay"}
Truth table10%1/10
not(A) and B or C and D and E and F
A= not $captcha_idB= $self->{"errno"} eq "ERROR_NO_SLOT_AVAILABLE"C= $self->{"errno"} eq "HTTP_ERROR"D= defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1E= $attempts != $self->{"attempts"}F= sleep $self->{"delay"}
ABCDEFresult
0XXXXX0
100XXX0
11X0XX0
1010XX0
11X10X0
10110X0
11X1100
1011100
11X1111
1011111
77  
78 3 return $captcha_id;
79 }
80  
81 sub recognize {
82 4 ▶ my ($self, $id) = @_;
Subroutine recognize: called CC=7 SCAR=19.7
Pod documented
83
84 4 my $start = time();
85 4 my $captcha_text;
86 4 my $attempts = 0;
87
88 do
89 {
90 5 $attempts ++;
91 5 sleep( $self->{delay} );
92 5 $captcha_text = $self->try_recognize($id);
93 }
94 while ( !$captcha_text &&
95 ($self->{errno} eq 'CAPCHA_NOT_READY' || $self->{errno} eq 'HTTP_ERROR') &&
96 (defined($self->{wait}) ? ( time() - $start ) < $self->{wait} : 1) &&
97 $attempts != $self->{attempts}
98 4 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
not $captcha_text and $self->{"errno"} eq "CAPCHA_NOT_READY" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"} and do { do { ++$attempts; sleep $self->{"delay"}; $captcha_text = $self->try_recognize($id) } } 1 4 5
defined $self->{'wait'} ? : 1 0 1
Condition75%9/12
not $captcha_text and $self->{"errno"} eq "CAPCHA_NOT_READY" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"}
!ll&&!rl&&r
401
not $captcha_text and $self->{"errno"} eq "CAPCHA_NOT_READY" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1
!ll&&!rl&&r
401
not $captcha_text and $self->{"errno"} eq "CAPCHA_NOT_READY" || $self->{"errno"} eq "HTTP_ERROR"
!ll&&!rl&&r
221
$self->{"errno"} eq "CAPCHA_NOT_READY" or $self->{"errno"} eq "HTTP_ERROR"
l!l&&r!l&&!r
102
MC/DC40%2/5
not $captcha_text and $self->{"errno"} eq "CAPCHA_NOT_READY" || $self->{"errno"} eq "HTTP_ERROR" and defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 and $attempts != $self->{"attempts"}
not $captcha_text $self->{"errno"} eq "CAPCHA_NOT_READY" $self->{"errno"} eq "HTTP_ERROR" defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1 $attempts != $self->{"attempts"}
Truth table37%3/8
not(A) and B or C and D and E
A= not $captcha_textB= $self->{"errno"} eq "CAPCHA_NOT_READY"C= $self->{"errno"} eq "HTTP_ERROR"D= defined $self->{"wait"} ? time - $start < $self->{"wait"} : 1E= $attempts != $self->{"attempts"}
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
99
100 4 return $captcha_text;
101 }
102  
103 sub upload_and_recognize {
104 0 ▶ my ($self, %opts) = @_;
Subroutine upload_and_recognize: not called CC=2 SCAR=17.9
Pod documented
105
106 0 my $captcha_id;
107 0 ▶ unless($captcha_id = $self->upload(%opts))
Branch: 1 branch
BranchTrueFalseTotal
unless ($captcha_id = $self->upload(%opts)) 0 0 0
108 {
109 0 return undef;
110 }
111
112 0 return $self->recognize($captcha_id);
113 }
114  
115 sub last_captcha_id {
116 2 ▶ my ($self) = @_;
Subroutine last_captcha_id: called CC=1 SCAR=0.0
Pod documented
117 2 return $self->{last_captcha_id};
118 }
119  
120 sub _name_by_signature {
121 4 ▶ if ($_[1] =~ /^\x47\x49\x46\x38(?:\x37|\x39)\x61/)
Subroutine _name_by_signature: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ /^\x47\x49\x46\x38(?:\x37|\x39)\x61/) 1 3 4
122 {
123 1 return 'captcha.gif';
124 }
125  
126 3 ▶ if ($_[1] =~ /^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A/)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ /^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A/) 1 2 3
127 {
128 1 return 'captcha.png';
129 }
130  
131 2 ▶ if ($_[1] =~ /^\xFF\xD8\xFF\xE0..\x4A\x46\x49\x46\x00/)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ /^\xFF\xD8\xFF\xE0..\x4A\x46\x49\x46\x00/) 1 1 2
132 {
133 1 return 'captcha.jpg';
134 }
135
136 1 return $FNAME;
137 }
138  
139 sub _name_by_file_signature {
140 0 ▶ my $self = shift;
Subroutine _name_by_file_signature: not called CC=2 SCAR=17.9
141
142 0 ▶ open my $fh, '<:raw', $_[0] or return $self->_name_by_signature('');
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $_[0] or return $self->_name_by_signature("") 0 0 0
143 0 sysread($fh, my $buf, 20);
144 0 close $fh;
145 0 return $self->_name_by_signature($buf);
146 }
147  
148 4 1;
149  
150 __END__
↑ Summary Antigate/V2.pm »