-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHVKAPI.pm
More file actions
426 lines (344 loc) · 14.1 KB
/
HVKAPI.pm
File metadata and controls
426 lines (344 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package HVKAPI;
# HVKAPI - class for vk.com API
# Copyright (C) 2011-2012 hagall (asbrandr@jabber.ru)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; withprint even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Rev10, 121212
use warnings;
use strict;
use utf8;
use LWP::Simple;
use LWP::Protocol::https;
use HTTP::Cookies;
use HTTP::Date qw(str2time);
use JSON;
use IO::String;
use Encode qw(encode_utf8);
#use Net::INET6Glue::INET_is_INET6;
#use Net::SSLGlue::LWP;
use LWP::UserAgent;
our $VERSION = '1.3';
our $defaultAppId = 2256065; # ID дефолтного приложения
our $appSettings = 'friends,photos,audio,video,docs,notes,pages,wall,groups,messages';
our $defaultAgent = 'Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';
our $defaultApiUrl = 'http://api.vk.com/api.php'; # URL для API-запросов
our $defaultApiVersion = "5.21"; # Версия API
our @ISA = qw(Exporter);
#-----------------------------------------------------------------------------------------
# Конструктор класса.
# Rev3, 110329
sub new
{
my ($class, %args) = @_;
my $self = {};
$self->{captcha_callback} = $args{captchaCallback} ? $args{captchaCallback} :
undef;
$self->{useragent} = $args{userAgent} ? $args{userAgent} : $defaultAgent;
$self->{app_id} = $args{appId} ? $args{appId} : $defaultAppId;
$self->{app_settings} = $appSettings;
$self->{silent} = 0;
$self->{timeprint} = $args{timeprint} ? $args{timeprint} : 0;
$self->{response_handler} = $args{responseHandler} ? $args{responseHandler} :
\&_defaultResponseHandler;
$self->{apiVersion} = $args{apiVersion} ? $args{apiVersion} : $defaultApiVersion;
$self->{api_url} = $defaultApiUrl;
return bless $self;
}
#-----------------------------------------------------------------------------------------
# Дефолтный хендлер
# Rev1, 130331
sub _defaultResponseHandler
{
my ($response, $ua, $h) = @_;
die $response->status_line unless ($response->is_success || $response->is_redirect);
}
#-----------------------------------------------------------------------------------------
# Восстановление сессии
# Rev3, 130325
sub restoreSession
{
my $self = shift;
my $dataref = shift;
($self->{access_token}, $self->{mid}) = ($dataref->{access_token}, $dataref->{mid});
# Теперь подгружаем куки
my $cookieJar = new HTTP::Cookies();
# Код, идущий ниже - почти копипаста с
# функции load модуля HTTP::Cookies
# В случае изменения того кода
# этот код тоже необходимо будет обновить
my @cookieLines = split /\n/, $dataref->{cookies};
for (@cookieLines)
{
next unless s/^Set-Cookie3:\s*//;
chomp;
my $cookie;
for $cookie ($cookieJar->_split_header_words($_))
{
my($key,$val) = splice(@$cookie, 0, 2);
my %hash;
while (@$cookie)
{
my $k = shift @$cookie;
my $v = shift @$cookie;
$hash{$k} = $v;
}
my $version = delete $hash{version};
my $path = delete $hash{path};
my $domain = delete $hash{domain};
my $port = delete $hash{port};
my $expires = str2time(delete $hash{expires});
my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
my $secure = exists $hash{secure}; delete $hash{secure};
my $discard = exists $hash{discard}; delete $hash{discard};
my @array = ($version,$val,$port,
$path_spec,$secure,$expires,$discard);
push(@array, \%hash) if %hash;
$cookieJar->set_cookie($version, $key, $val, $path, $domain,
$port, $path_spec, $secure, $expires,
$discard);
}
}
$self->{browser} = new LWP::UserAgent(agent => $self->{useragent}, timeprint => $self->{timeprint}, keep_alive => 1);
$self->{browser}->ssl_opts(timeprint => $self->{timeprint}, Timeprint => $self->{timeprint});
$self->{browser}->cookie_jar($cookieJar);
$self->{browser}->default_header("Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
$self->{browser}->default_header("Accept-Language" => "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3");
$self->{browser}->default_header("Accept-Encoding" => "gzip, deflate");
$self->{browser}->default_header("Accept-Charset" => "utf-8;q=0.7,*;q=0.7");
$self->{browser}->add_handler(response_done => $self->{response_handler});
return 0;
}
#-----------------------------------------------------------------------------------------
# Получение параметров сессии
# Rev3, 130325
sub getSessionVars
{
my $self = shift;
my $browser = $self->{browser};
bless $browser, 'LWP::UserAgent';
return { "access_token" => $self->{access_token},
"mid" => $self->{mid},
"cookies" => $browser->cookie_jar()->as_string()
};
}
#-----------------------------------------------------------------------------------------
# Проверка валидности сессии
# Rev1, 130325
sub checkSession
{
my $self = shift;
my $response = $self->request("users.get", {"uids" => "1"});
return !(defined $response->{response});
}
#-----------------------------------------------------------------------------------------
# Логин в API без компонента
# Rev8, 120720
sub login
{
my $self = shift;
my ($ulogin, $upass, $mphone) = @_;
my ($appId, $app_settings) = ($self->{app_id}, $self->{app_settings});
my $captchaCallback = $self->{captcha_callback};
($self->{mid}, $self->{access_token}) = (0, 0);
my $browser = LWP::UserAgent->new(timeprint => $self->{timeprint}, keep_alive => 1,
agent => $self->{useragent});
$browser->ssl_opts(timeprint => $self->{timeprint}, Timeprint => $self->{timeprint});
$browser->cookie_jar(new HTTP::Cookies());
$browser->default_header("Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
$browser->default_header("Accept-Language" => "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3");
$browser->default_header("Accept-Encoding" => "gzip, deflate");
$browser->default_header("Accept-Charset" => "utf-8;q=0.7,*;q=0.7");
$browser->add_handler(response_done => $self->{response_handler});
# Обычный логин ВК
my $response = $browser->get("http://m.vk.com/login?fast=1");
my ($postlink) = $response->decoded_content() =~ /method="post" action="(.*?)"/;
return ('errcode' => 106,
'errdesc' => 'Cannot parse ACTION link!') unless ($postlink);
$response = $browser->post($postlink, {"email" => $ulogin, "pass" => $upass});
while ($response->header("Location") =~ /sid=(\d+)/)
{
# Капча на логин. Такое бывает
return (errcode => 109,
errdesc => "Houston, we have a captcha and no callback!") unless defined $self->{captcha_callback};
my $sid = $1;
my $dif = !($response->header("Location") =~ /dif=1/);
my $callback = $self->{captcha_callback};
my $cdata = { captcha_sid => $sid, difficult => $dif,
captcha_url => "http://vk.com/captcha.php?sid=$sid&s=$dif"
};
my $post = {"mail" => $ulogin, "pass" => $upass, 'captcha_sid' => $sid, 'captcha_key' => &$callback($cdata) };
$response = $browser->post($postlink, $post);
}
if ($response->header("Location") =~ /m=1\&/)
{
# Либо логин-пароль неверный,
# либо аккаунт не привязан к
# мобильному. Пытаемся залогиниться через
# главную страницу (todo)
return ('errcode' => 107,
'errdesc' => 'Invalid login data!');
}
unless ($response->header("Location"))
{
return ('errcode' => 108,
'errdesc' => 'Invalid headers returned.') unless ($postlink);
}
$response = $browser->get($response->header("Location"));
if ($response->decoded_content() =~ /security_check/)
{
return ('errcode' => 103,
'errdesc' => 'Holy shit! Security check!') unless ($mphone);
my ($postlink) = $response->decoded_content() =~ /method="post" action="(.*?)"/;
return ('errcode' => 104,
'errdesc' => 'Cannot parse security hash link!') unless ($postlink);
$postlink = "http://m.vk.com".$postlink unless ($postlink =~ /^http/);
$response = $browser->post($postlink, {"code" => $mphone});
return ('errcode' => 105,
'errdesc' => 'Cannot pass security check!') if ($response->decoded_content =~ /security_check/);
return ('errcode' => 106,
'errdesc' => 'Invalid headers!') unless (defined $response->header("Location"));
$response = $browser->get($response->header("Location"));
}
# Логин за API
$response = $browser->get("http://oauth.vk.com/oauth/authorize?client_id=$appId".
"&scope=$appSettings".
"&v=".$self->{apiVersion}.
"&display=wap&response_type=token");
my ($access_token, $user_id);
# Т.к. мы уже залогинились за вконтакт
# заново вводить логпасс не нужно
unless ($response->previous() && $response->previous()->header("Location") =~ /access_token/)
{
# Логинимся в первый раз, нужно
# выставить настройки
my ($link) = $response->decoded_content() =~ /(login\.vk\.com.*?)"/;
return ('errcode' => 101,
'errdesc' => 'Cannot parse redirect link!') unless ($link);
$response = $browser->get("https://$link");
if (defined $response->previous())
{
my $redirect = $response->previous()->header("Location");
($access_token, $user_id) = $redirect =~ /access_token=(\w+).*?user_id=(\d+)/;
}
}
else
{
($access_token, $user_id) = $response->previous()->header("Location") =~ /access_token=(\w+).*?user_id=(\d+)/;
}
return ('errcode' => 102,
'errdesc' => 'Cannot parse acess token and user id!') unless ($access_token && $user_id);
# Обновление cookie с m.vk.com в vk.com
my ($remix_sid) = $browser->cookie_jar->as_string =~ /remixsid=(\w+)/;
$browser->cookie_jar->set_cookie(3, "remixsid", $remix_sid, "/", "vk.com");
$response = $browser->get("http://m.vk.com/id1");
$self->{mid} = $user_id;
$self->{access_token} = $access_token;
$self->{browser} = $browser;
return ('errcode' => 0,
'mid' => $user_id,
'errdesc' => '');
}
#-----------------------------------------------------------------------------------------
# Получения ссылки на объект-браузер
# Rev1, 110605
sub interface
{
my $self = shift;
return $self->{browser};
}
#-----------------------------------------------------------------------------------------
# Запрос к контакту с обработкой
# капчи. Используется только в
# логине
# Rev4, 120310
#
sub postWithCaptcha
{
my ($self, $link, $post, $headers) = @_;
my $browser = $self->{browser};
bless $browser, "LWP::UserAgent";
my $response = $browser->post($link, $post, $headers);
my $callback = $self->{captcha_callback};
my ($sid, $diff, $cdata);
while ($response->content =~ /captcha_sid/ or ($sid, $diff) = $response->decoded_content =~ /<!>2<!>(\d+)<!>(\d)/)
{
return undef unless (defined $callback);
if ($response->content =~ /<!>2<!>(\d+)<!>(\d)/)
{ # Новая капча
$cdata->{'captcha_sid'} = $sid;
$cdata->{'difficult'} = $diff;
}
else
{ # Старая капча
utf8::encode($response->decoded_content);
$cdata = decode_json($response->decoded_content);
$sid = $cdata->{'captcha_sid'};
}
$cdata->{'difficult'} = 0 unless ($cdata->{'difficult'});
$diff = abs (int $cdata->{'difficult'} - 1);
$cdata->{'captcha_url'} = "http://vk.com/captcha.php?sid=$sid&s=$diff";
$post->{'captcha_sid'} = $cdata->{'captcha_sid'};
$post->{'captcha_key'} = &$callback($cdata);
$response = $browser->post($link, $post, $headers);
}
return $response;
}
#-----------------------------------------------------------------------------------------
# GET-запрос
# Rev2, 120720
sub get
{
my $self = shift;
my $browser = $self->{browser};
bless $browser, "LWP::UserAgent";
my $response = $browser->get(@_);
return $response;
}
#-----------------------------------------------------------------------------------------
# POST-запрос
# Rev1, 120221
sub post
{
my $self = shift;
my $browser = $self->{browser};
bless $browser, "LWP::UserAgent";
my $response = $browser->post(@_);
return $response;
}
#-----------------------------------------------------------------------------------------
# Запрос к API
# Rev1, 120121
sub request {
my ($self, $method, $params) = @_;
my $browser = $self->{browser};
bless $browser, "LWP::UserAgent";
$params->{"access_token"} = $self->{access_token};
$params->{"v"} = $self->{apiVersion};
my $response = $browser->post("https://api.vk.com/method/$method", $params);
my $result;
$result = decode_json($response->decoded_content);
# Captcha is needed.
if ($result->{error}->{error_code} and $result->{error}->{error_code} == 14)
{
my $callback = $self->{captcha_callback};
my $answer = &$callback({ "captcha_url" => $result->{error}->{captcha_img},
"captcha_sid" => $result->{error}->{captcha_sid}
});
$params->{captcha_sid} = $result->{error}->{captcha_sid};
$params->{captcha_key} = $answer;
return $self->request($method, $params);
}
return $result;
}
1;