File Coverage

blib/lib/VK/App.pm
Criterion Covered Total %
statement 15 88 17.0
branch 0 64 0.0
condition 0 8 0.0
subroutine 5 15 33.3
pod 5 5 100.0
total 25 180 13.8


line stmt bran cond sub pod time code
1             package VK::App;
2            
3 1     1   16081 use strict;
  1         2  
  1         35  
4 1     1   3 use warnings;
  1         1  
  1         23  
5 1     1   18997 use LWP;
  1         59136  
  1         44  
6 1     1   675 use LWP::Protocol::https;
  1         100971  
  1         43  
7 1     1   641 use JSON;
  1         9648  
  1         7  
8            
9             our $VERSION = 0.12;
10            
11             sub new {
12 0     0 1   my ($class, %args) = @_;
13 0 0         die "USAGE:\nVK::App->new(api_id => ... login => ... password => ...)\n",
14             "VK::App->new(api_id => ... cookie_file => ...)\n" unless _valid_new_args(\%args);
15            
16 0           my $self;
17 0 0         $self->{api_id} = $args{api_id} if exists $args{api_id};
18 0 0         $self->{login} = $args{login} if exists $args{login};
19 0 0         $self->{password} = $args{password} if exists $args{password};
20 0 0         $self->{cookie_file} = $args{cookie_file} if exists $args{cookie_file};
21 0 0         (exists $args{scope})?($self->{scope} = $args{scope}):($self->{scope} = 'friends,photos,audio,video,wall,groups,messages,offline');
22 0 0         (exists $args{format})?($self->{format} = $args{format}):($self->{format} = 'Perl');
23 0 0         (exists $args{cookie_file})?($self->{ua} = _create_ua($args{cookie_file})):($self->{ua} = _create_ua());
24            
25 0           bless $self, $class;
26            
27 0 0         die 'ERROR: login failed' unless($self->_login());
28 0 0         die 'ERROR: authorize app failed' unless($self->_authorize_app());
29            
30 0 0         $self->{ua}->cookie_jar()->save($self->{cookie_file}) if (exists $self->{cookie_file});
31            
32 0           return $self;
33             }
34            
35             sub _login {
36 0     0     my $self = shift;
37 0           my $lpage = $self->{ua}->get('http://vk.com/login.php');
38 0 0         return 0 unless ($lpage->is_success); # network problem?
39 0 0         my $action = $1 if $lpage->content =~ /action=\"(.+?)\"/;
40 0 0         return 1 unless $action; # log in already? go to the next step
41 0           my $res = $self->{ua}->post($action, {
42             #my $res = $self->{ua}->post('https://login.vk.com/?act=login', {
43             email => $self->{login},
44             pass => $self->{password},
45             });
46 0 0         return 0 if $res->status_line ne "302 Found";
47 0 0         return 0 if $res->header('location') !~ /__q_hash/;
48 0           $res = $self->{ua}->get($res->header('location'));
49 0 0         return 0 unless $res->is_success;
50 0           return $res->message;
51             }
52            
53             sub _authorize_app {
54 0     0     my $self = shift;
55 0           push @{ $self->{ua}->requests_redirectable }, 'POST';
  0            
56 0           my %authorize;
57 0           $authorize{request} = 'http://oauth.vk.com/authorize?'.
58             'client_id='.$self->{api_id}.
59             '&scope='.$self->{scope}.
60             '&redirect_uri=http://api.vk.com/blank.html'.
61             '&display=wap'.
62             '&response_type=token';
63 0           my $res = $self->{ua}->post($authorize{request});
64 0 0         return 0 unless $res->is_success;
65 0           my $contect = $res->decoded_content;
66 0 0         $authorize{approve} = $1 if $contect =~ /action=\"(.+)\"/;
67 0 0         if (exists $authorize{approve}) {
68 0           $res = $self->{ua}->post($authorize{approve});
69 0 0         return 0 unless $res->is_success;
70             }
71 0 0         if ($res->request()->uri() =~ /access_token=(.+)&expires_in=0&user_id=(\d+)/) {
72 0           $self->{access_token} = $1;
73 0           $self->{uid} = $2;
74             }
75 0           return $res->message;
76             }
77            
78             sub _create_ua {
79 0     0     my $ua = LWP::UserAgent->new(agent => "VK::App $VERSION");
80             #push @{ $ua->requests_redirectable }, 'POST';
81             #$ua->ssl_opts(verify_hostname => 0);
82 0 0         ($_[0])?($ua->cookie_jar( {file=>$_[0],autosave => 1} )):($ua->cookie_jar( { } ));
83 0           return $ua;
84             }
85            
86             sub _clean_cookie {
87 0     0     my $self = shift;
88 0           $self->{ua}->cookie_jar()->clear();
89 0           return 1;
90             }
91            
92             sub _valid_new_args {
93 0     0     my $args = shift;
94 0 0         return 0 unless ref($args) eq 'HASH';
95 0 0 0       if (!$args->{api_id} ||
      0        
96             ((!$args->{login} || !$args->{password}) && !$args->{cookie_file}) ) {
97 0           return 0;
98             }
99 0           return 1;
100             }
101            
102             sub ua {
103 0     0 1   my $self = shift;
104 0 0         die "Can't get UserAgent object" unless exists $self->{ua};
105 0           return $self->{ua};
106             }
107            
108             sub access_token {
109 0     0 1   my $self = shift;
110 0 0         die "Can't get access token" unless exists $self->{access_token};
111 0           return $self->{access_token};
112             }
113            
114             sub uid {
115 0     0 1   my $self = shift;
116 0 0         die "Can't get user id" unless exists $self->{uid};
117 0           return $self->{uid};
118             }
119            
120             sub request {
121 0     0 1   my $self = shift;
122 0           my $method = shift;
123 0 0         $method .= '.xml' if $self->{format} eq "XML";
124 0   0       my $params = shift || {};
125 0           my $url = 'https://api.vk.com/method/'.$method;
126 0           my $res = $self->{ua}->post($url, { %$params, access_token => $self->{access_token} });
127 0 0         return 0 unless $res->is_success;
128 0           my $content = $res->content;
129 0 0         return $content if ($self->{format} eq "XML");
130 0 0         return $content if ($self->{format} eq "JSON");
131 0           return decode_json($content);
132             }
133            
134             1;
135            
136             __END__