| 1 | package VKontakte::API; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use Digest::MD5 qw(md5 md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use WWW::Mechanize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =pod | ||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | VKontakte::API - Module for login into vkontakte.ru and sending requests | ||||||||||
| 16 | |||||||||||
| 17 | =head1 VERSION | ||||||||||
| 18 | |||||||||||
| 19 | Version 0.04 | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | 2 | our $VERSION = '0.04'; | |||||||||
| 24 | |||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||
| 26 | |||||||||||
| 27 | First of all register you application at http://vkontakte.ru/apps.php?act=add | ||||||||||
| 28 | get api_id and secret_key to use it like this: | ||||||||||
| 29 | |||||||||||
| 30 | #1. | ||||||||||
| 31 | use VKontakte::API; | ||||||||||
| 32 | |||||||||||
| 33 | my $vk = VKontakte::API->new('api_id', 'secret_key'); | ||||||||||
| 34 | my $data=$vk->sendRequest('getProfiles', {'domains'=>'deevaas'}); | ||||||||||
| 35 | |||||||||||
| 36 | #2. or | ||||||||||
| 37 | use VKontakte::API; | ||||||||||
| 38 | $vk = VKontakte::API->new( | ||||||||||
| 39 | $api_id, | ||||||||||
| 40 | $cgi_query->param('session[secret]'), | ||||||||||
| 41 | $cgi_query->param('session[mid]'), | ||||||||||
| 42 | $cgi_query->param('session[sid]') | ||||||||||
| 43 | ); | ||||||||||
| 44 | my $data=$vk->sendRequest('getProfiles', {'domains'=>'deevaas'}); | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | #3. or new one, use OAuth 2.0 | ||||||||||
| 48 | use VKontakte::API::OAuth; | ||||||||||
| 49 | $vk = VKontakte::API::OAuth->new( | ||||||||||
| 50 | $api_id, | ||||||||||
| 51 | $secret | ||||||||||
| 52 | ); | ||||||||||
| 53 | my $data=$vk->sendRequest('getProfiles', {'domains'=>'deevaas'}); | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | =head1 SUBROUTINES/METHODS | ||||||||||
| 57 | |||||||||||
| 58 | =head2 new | ||||||||||
| 59 | |||||||||||
| 60 | Create new object. Two parameters of registered application: | ||||||||||
| 61 | |||||||||||
| 62 | =over 4 | ||||||||||
| 63 | |||||||||||
| 64 | =item api_id | ||||||||||
| 65 | |||||||||||
| 66 | =item secret_key | ||||||||||
| 67 | |||||||||||
| 68 | =back | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 73 | 0 | my $class = shift; | |||||||||
| 74 | 0 | my $self = {}; | |||||||||
| 75 | 0 | bless( $self, $class ); | |||||||||
| 76 | |||||||||||
| 77 | 0 | $self->{api_id} = $_[0]; | |||||||||
| 78 | 0 | $self->{secret} = $_[1]; | |||||||||
| 79 | 0 | $self->{mid} = $_[2]; | |||||||||
| 80 | 0 | $self->{sid} = $_[3]; | |||||||||
| 81 | |||||||||||
| 82 | 0 | $self->{api_url} = "http://api.vk.com/api.php"; | |||||||||
| 83 | |||||||||||
| 84 | 0 | return $self; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =head2 sendRequest | ||||||||||
| 88 | |||||||||||
| 89 | Send requests described at http://vkontakte.ru/developers.php?o=-1&p=%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5+%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2+API | ||||||||||
| 90 | |||||||||||
| 91 | $resp = $auth->sendRequest('getProfiles', {'uids'=>'123123'}); | ||||||||||
| 92 | |||||||||||
| 93 | =over 4 | ||||||||||
| 94 | |||||||||||
| 95 | =item method | ||||||||||
| 96 | |||||||||||
| 97 | Name of methods listed at http://vkontakte.ru/developers.php?o=-1&p=%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5+%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2+API | ||||||||||
| 98 | |||||||||||
| 99 | =item params | ||||||||||
| 100 | |||||||||||
| 101 | Parameters for method | ||||||||||
| 102 | |||||||||||
| 103 | =back | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub sendRequest { | ||||||||
|
Subroutine
sendRequest: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 108 | 0 | my $self = shift; | |||||||||
| 109 | 0 | my $method = $_[0]; | |||||||||
| 110 | 0 | my $params = $_[1]; | |||||||||
| 111 | |||||||||||
| 112 | 0 | $params->{'api_id'} = $self->{'api_id'}; | |||||||||
| 113 | 0 | $params->{'v'} = '3.0'; | |||||||||
| 114 | 0 | $params->{'method'} = $method; | |||||||||
| 115 | 0 | $params->{'timestamp'} = time(); | |||||||||
| 116 | 0 | $params->{'format'} = 'json'; | |||||||||
| 117 | 0 | $params->{'rnd'} = int(rand()*10000); | |||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | my $sig = defined $self->{'mid'} ? $self->{'mid'} : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | foreach my $k (sort keys %$params){ | |||||||||
| 121 | 0 | $sig .= $k . '=' . $params->{$k}; | |||||||||
| 122 | } | ||||||||||
| 123 | 0 | $sig .= $self->{secret}; | |||||||||
| 124 | |||||||||||
| 125 | 0 | $params->{'sig'} = md5_hex($sig); | |||||||||
| 126 | 0 | ▶ | $params->{'sid'} = $self->{sid} if $self->{sid}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | my $query = $self->{api_url} . '?' . $self->_params($params); | |||||||||
| 128 | |||||||||||
| 129 | 0 | my $mech = WWW::Mechanize->new( agent => 'VKontakte::API', ); | |||||||||
| 130 | 0 | my $r = $mech->get($query); | |||||||||
| 131 | |||||||||||
| 132 | # my $res = file_get_contents($query); | ||||||||||
| 133 | 0 | my $response = $mech->content(); | |||||||||
| 134 | 0 | utf8::encode($response); | |||||||||
| 135 | 0 | return decode_json($response); | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | =head2 _params | ||||||||||
| 139 | |||||||||||
| 140 | prepares parameters for request | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | 0 | ▶ | sub _params { | ||||||||
|
Subroutine
_params: not called CC=3 SCAR=24.8
| |||||||||||
| 145 | 0 | my $self = shift; | |||||||||
| 146 | 0 | my $params = shift; | |||||||||
| 147 | |||||||||||
| 148 | 0 | ▶ | return unless ( ref $params eq "HASH" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 149 | |||||||||||
| 150 | 0 | my @pice; | |||||||||
| 151 | 0 | ▶ | while ( my ( $k, $v ) = each %$params ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 0 | push @pice, $k . '=' . $v;# _encurl($v); | |||||||||
| 153 | } | ||||||||||
| 154 | 0 | return join( '&', @pice ); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | =head2 _encurl | ||||||||||
| 159 | |||||||||||
| 160 | encodes data for url | ||||||||||
| 161 | |||||||||||
| 162 | =cut | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | sub _encurl { | ||||||||
|
Subroutine
_encurl: not called CC=2 SCAR=17.9
| |||||||||||
| 165 | 0 | my ($url) = @_; | |||||||||
| 166 | 0 | ▶ | ( defined $url ) || ( $url = "" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | |||||||||||
| 168 | 0 | $url=~s/([^a-z0-9])/sprintf("%%%02x",ord($1))/egsi; | |||||||||
| 169 | #$url =~ s/([^a-z0-9])/sprintf("%%%x",ord($1))/egsi; | ||||||||||
| 170 | 0 | $url =~ s/ /\+/go; | |||||||||
| 171 | 0 | return $url; | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | |||||||||||
| 175 | =head1 AUTHOR | ||||||||||
| 176 | |||||||||||
| 177 | Anastasiya Deeva, C<< <nastya at creograf.ru> >> | ||||||||||
| 178 | |||||||||||
| 179 | =head1 BUGS | ||||||||||
| 180 | |||||||||||
| 181 | Please report any bugs or feature requests to C<bug-vkontakte-api at rt.cpan.org>, or through | ||||||||||
| 182 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=VKontakte-API>. I will be notified, and then you'll | ||||||||||
| 183 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 184 | |||||||||||
| 185 | |||||||||||
| 186 | |||||||||||
| 187 | |||||||||||
| 188 | =head1 SUPPORT | ||||||||||
| 189 | |||||||||||
| 190 | You can find documentation for this module with the perldoc command. | ||||||||||
| 191 | |||||||||||
| 192 | perldoc VKontakte::API | ||||||||||
| 193 | |||||||||||
| 194 | |||||||||||
| 195 | You can also look for information at: | ||||||||||
| 196 | |||||||||||
| 197 | =over 4 | ||||||||||
| 198 | |||||||||||
| 199 | =item * RT: CPAN's request tracker | ||||||||||
| 200 | |||||||||||
| 201 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=VKontakte-API> | ||||||||||
| 202 | |||||||||||
| 203 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 204 | |||||||||||
| 205 | L<http://annocpan.org/dist/VKontakte-API> | ||||||||||
| 206 | |||||||||||
| 207 | =item * CPAN Ratings | ||||||||||
| 208 | |||||||||||
| 209 | L<http://cpanratings.perl.org/d/VKontakte-API> | ||||||||||
| 210 | |||||||||||
| 211 | =item * Search CPAN | ||||||||||
| 212 | |||||||||||
| 213 | L<http://search.cpan.org/dist/VKontakte-API/> | ||||||||||
| 214 | |||||||||||
| 215 | =back | ||||||||||
| 216 | |||||||||||
| 217 | |||||||||||
| 218 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 219 | |||||||||||
| 220 | |||||||||||
| 221 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 222 | |||||||||||
| 223 | Copyright 2011 Anastasiya Deeva. | ||||||||||
| 224 | |||||||||||
| 225 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 226 | under the terms of either: the GNU General Public License as published | ||||||||||
| 227 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 228 | |||||||||||
| 229 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 230 | |||||||||||
| 231 | |||||||||||
| 232 | =cut | ||||||||||
| 233 | |||||||||||
| 234 | 2 | 1; | |||||||||