| 1 | package Net::VKontakte::Standalone; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.008000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | my $self = bless {},$class; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | $self->{browser} = WWW::Mechanize::->new( | |||||||||||||||||||||||||||||||||||||||||
| 18 | agent => __PACKAGE__.$VERSION, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | autocheck => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | $self->{api_id} = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } elsif (@_ % 2 == 0) { # smells like hash | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | for my $key (qw/api_id errors_noauto captcha_handler/) { | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | $self->{$key} = $opt{$key} if defined $opt{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | croak "wrong number of arguments to constructor"; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | croak "api_id is required" unless $self->{api_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my ($self, $params, $base) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | (my $uri = URI::->new($base))->query_form($params); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return $self->{browser}->get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub auth { # dirty hack | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auth: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my ($self,$login,$password,$scope) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | @{$self}{"login","password","scope"} = ($login, $password, $scope); # reuse in case of reauth | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $self->{browser}->get($self->auth_uri($scope)); | |||||||||||||||||||||||||||||||||||||||||
| 45 | $self->{browser}->submit_form( | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | with_fields => { | |||||||||||||||||||||||||||||||||||||||||
| 47 | email => $login, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | pass => $password, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ); # log in | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $self->{browser}->submit unless $self->{browser}->uri =~ m|^https://oauth.vk.com/blank.html|; # allow access if requested to | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return $self->redirected($self->{browser}->uri); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub auth_uri { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auth_uri: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($self, $scope, $display) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | (my $uri = URI::->new("https://api.vkontakte.ru/oauth/authorize"))->query_form( | ||||||||||||||||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | client_id => $self->{api_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | redirect_uri => "blank.html", | |||||||||||||||||||||||||||||||||||||||||
| 61 | scope => $scope, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | response_type => "token", | ||||||||||||||||||||||||||||||||||||||||||
| 63 | display => $display, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $uri->canonical; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub redirected { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
redirected: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my %params = map { split /=/,$_,2 } split /&/,$1 if $uri =~ m|https://oauth.vk.com/blank.html#(.*)|; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | croak "No access_token returned (wrong login/password?)" unless defined $params{access_token}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $self->{access_token} = $params{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | croak "No token expiration time returned" unless $params{expires_in}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{auth_time} = time; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self->{expires_in} = $params{expires_in}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub api { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my ($self,$method,$params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak "Cannot make API calls unless authentificated" unless defined $self->{access_token}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (time - $self->{auth_time} > $self->{expires_in}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($self->{login} && $self->{password} && $self->{scope}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->{"login"} and $self->{"password"} and $self->{"scope"}
$self->{"login"} and $self->{"password"}
MC/DC0%0/3
$self->{"login"} and $self->{"password"} and $self->{"scope"}
$self->{"login"}
$self->{"password"}
$self->{"scope"}
Truth table0%0/4
A and B and C
A= $self->{"login"}B= $self->{"password"}C= $self->{"scope"}
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->auth($self->{"login","password","scope"}); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->{errors_noauto}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->{error} = "access_token expired"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (ref $self->{errors_noauto} and ref $self->{errors_noauto} eq "CODE") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
MC/DC0%0/2
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
ref $self->{"errors_noauto"}
ref $self->{"errors_noauto"} eq "CODE"
Truth table0%0/3
A and B
A= ref $self->{"errors_noauto"}B= ref $self->{"errors_noauto"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $self->{errors_noauto}->({error_code => "none", error_msg => "access_token expired"}); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | croak "access_token expired"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $params->{access_token} = $self->{access_token}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | REQUEST: { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $response = decode_json $self->_request($params,"https://api.vk.com/method/$method")->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($response->{response}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | return $response->{response}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } elsif ($response->{error}) { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($self->{errors_noauto}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $self->{error} = $response->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (ref $self->{errors_noauto} and ref $self->{errors_noauto} eq "CODE") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
MC/DC0%0/2
ref $self->{"errors_noauto"} and ref $self->{"errors_noauto"} eq "CODE"
ref $self->{"errors_noauto"}
ref $self->{"errors_noauto"} eq "CODE"
Truth table0%0/3
A and B
A= ref $self->{"errors_noauto"}B= ref $self->{"errors_noauto"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->{errors_noauto}->($response->{error}); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (6 == $response->{error}{error_code}) { # Too many requests per second. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | redo REQUEST; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } elsif (14 == $response->{error}{error_code}) { # Captcha is needed | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($self->{captcha_handler}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $params->{captcha_key} = $self->{captcha_handler}->($response->{error}{captcha_img}); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $params->{captcha_sid} = $response->{error}{captcha_sid}; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | redo REQUEST; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | croak "Captcha is needed and no captcha handler specified"; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | croak "API call returned error ".$response->{error}{error_msg}; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | croak "API call didn't return response or error". | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $Carp::Verbose ? eval { require Data::Dumper; Data::Dumper::Dumper($response) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | : ""; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub captcha_handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
captcha_handler: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my ($self, $handler) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | croak "\$handler is not a subroutine reference" unless ref $handler eq "CODE"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $self->{captcha_handler} = $handler; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | return shift->{error}; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub errors_noauto { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errors_noauto: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my ($self, $noauto) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $self->{errors_noauto} = $noauto; # whatever this means | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||||||||||||||||