line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::2Captcha; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13527
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
21
|
|
4
|
1
|
|
|
1
|
|
18
|
use 5.008_005; |
|
1
|
|
|
|
|
2
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
3
|
use Carp 'croak'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
52
|
|
8
|
1
|
|
|
1
|
|
584
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
35132
|
|
|
1
|
|
|
|
|
30
|
|
9
|
1
|
|
|
1
|
|
8
|
use URI; |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
19
|
|
10
|
1
|
|
|
1
|
|
414
|
use MIME::Base64; |
|
1
|
|
|
|
|
476
|
|
|
1
|
|
|
|
|
49
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
5
|
use vars qw/$errstr/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
685
|
|
13
|
0
|
|
|
0
|
0
|
|
sub errstr { $errstr } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
17
|
0
|
0
|
|
|
|
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
0
|
0
|
|
|
|
|
$args{key} or croak "key is required.\n"; |
20
|
0
|
|
0
|
|
|
|
$args{ua} ||= LWP::UserAgent->new; |
21
|
0
|
|
0
|
|
|
|
$args{url} ||= 'http://2captcha.com/res.php'; |
22
|
0
|
|
0
|
|
|
|
$args{sleep} ||= 3; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
return bless \%args, $class; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub decaptcha { |
28
|
0
|
|
|
0
|
1
|
|
my ($self, $file_or_content, %params) = @_; |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
|
my $captcha_id = $self->upload($file_or_content, %params) or return; |
31
|
0
|
|
|
|
|
|
sleep $self->{sleep}; # put a little sleep since that's really not so fast |
32
|
0
|
|
|
|
|
|
while (1) { |
33
|
0
|
|
|
|
|
|
my $text = $self->get($captcha_id); |
34
|
0
|
0
|
|
|
|
|
if ($text) { |
35
|
|
|
|
|
|
|
return { |
36
|
0
|
|
|
|
|
|
text => $text, |
37
|
|
|
|
|
|
|
id => $captcha_id # for reportbad |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
} |
40
|
0
|
0
|
|
|
|
|
if ($self->errstr =~ /CAPCHA_NOT_READY/) { |
41
|
0
|
|
|
|
|
|
sleep $self->{sleep}; |
42
|
|
|
|
|
|
|
} else { |
43
|
0
|
|
|
|
|
|
return; # just bad |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub upload { |
49
|
0
|
|
|
0
|
1
|
|
my ($self, $file_or_content, %params) = @_; |
50
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
|
if (-e $file_or_content) { |
52
|
0
|
0
|
|
|
|
|
open(my $fh, '<', $file_or_content) or croak "Can't open $file_or_content: $!"; |
53
|
0
|
|
|
|
|
|
$file_or_content = do { |
54
|
0
|
|
|
|
|
|
local $/; |
55
|
0
|
|
|
|
|
|
<$fh>; |
56
|
|
|
|
|
|
|
}; |
57
|
0
|
|
|
|
|
|
close($fh); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
my $res = $self->request( |
61
|
|
|
|
|
|
|
url => 'http://2captcha.com/in.php', |
62
|
|
|
|
|
|
|
__method => 'POST', |
63
|
|
|
|
|
|
|
method => 'base64', |
64
|
|
|
|
|
|
|
body => encode_base64($file_or_content), |
65
|
|
|
|
|
|
|
%params |
66
|
|
|
|
|
|
|
); |
67
|
0
|
0
|
|
|
|
|
if ($res !~ /OK/) { |
68
|
0
|
|
|
|
|
|
$errstr = $res; |
69
|
0
|
|
|
|
|
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
$res =~ s/^OK\|//; |
72
|
0
|
|
|
|
|
|
return $res; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get { |
76
|
0
|
|
|
0
|
1
|
|
my ($self, $id) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $res = $self->request(action => 'get', id => $id); |
79
|
0
|
0
|
|
|
|
|
if ($res !~ /OK/) { |
80
|
0
|
|
|
|
|
|
$errstr = $res; |
81
|
0
|
|
|
|
|
|
return; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
|
$res =~ s/^OK\|//; |
84
|
0
|
|
|
|
|
|
return $res; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub get_multi { |
88
|
0
|
|
|
0
|
1
|
|
my ($self, @ids) = @_; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
my $res = $self->request(action => 'get', ids => join(',', @ids)); |
91
|
0
|
0
|
|
|
|
|
return wantarray ? split(/\|/, $res) : $res; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub getbalance { |
95
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
$self->request(action => 'getbalance'); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub reportbad { |
101
|
0
|
|
|
0
|
1
|
|
my ($self, $id) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$self->request(action => 'reportbad', id => $id); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub getstats { |
107
|
0
|
|
|
0
|
1
|
|
my ($self, $date) = @_; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
$self->request(action => 'getstats', date => $date); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub load { |
113
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$self->request(url => 'http://2captcha.com/load.php'); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub userrecaptcha { |
119
|
0
|
|
|
0
|
1
|
|
my ($self, $googlekey, $pageurl) = @_; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
my $res = $self->request( |
122
|
|
|
|
|
|
|
url => 'http://2captcha.com/in.php', |
123
|
|
|
|
|
|
|
method => 'userrecaptcha', |
124
|
|
|
|
|
|
|
googlekey => $googlekey, |
125
|
|
|
|
|
|
|
pageurl => $pageurl, |
126
|
|
|
|
|
|
|
); |
127
|
0
|
0
|
|
|
|
|
if ($res !~ /OK/) { |
128
|
0
|
|
|
|
|
|
$errstr = $res; |
129
|
0
|
|
|
|
|
|
return; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
|
|
|
|
|
$res =~ s/^OK\|//; |
132
|
0
|
|
|
|
|
|
return $res; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub request { |
136
|
0
|
|
|
0
|
1
|
|
my ($self, %params) = @_; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
0
|
|
|
|
$params{key} ||= $self->{key}; |
139
|
0
|
|
0
|
|
|
|
my $url = delete $params{url} || $self->{url}; |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my $res; |
142
|
0
|
|
0
|
|
|
|
my $method = delete $params{__method} || 'GET'; |
143
|
0
|
0
|
|
|
|
|
if ($method eq 'POST') { |
144
|
0
|
|
|
|
|
|
$res = $self->{ua}->post($url, \%params); |
145
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
146
|
0
|
|
|
|
|
|
$errstr = "Failed to post $url: " . $res->status_line; |
147
|
0
|
|
|
|
|
|
return; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} else { |
150
|
0
|
|
|
|
|
|
my $uri = URI->new($url); |
151
|
0
|
|
|
|
|
|
$uri->query_form(%params); |
152
|
0
|
|
|
|
|
|
$res = $self->{ua}->get($uri->as_string); |
153
|
0
|
0
|
|
|
|
|
unless ($res->is_success) { |
154
|
0
|
|
|
|
|
|
$errstr = "Failed to get " . $uri->as_string . ": " . $res->status_line; |
155
|
0
|
|
|
|
|
|
return; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# print Dumper(\$res); use Data::Dumper; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
return $res->decoded_content; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
__END__ |