Skip to content

Commit 2c582cc

Browse files
committed
3 request per second limit
API version 5.4 Get number from login for security check vk_tools
1 parent 2dc5eb7 commit 2c582cc

5 files changed

Lines changed: 120 additions & 33 deletions

File tree

MANIFEST

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# file GENERATED by distutils, do NOT edit
2-
setup.py
3-
jconfig\__init__.py
4-
jconfig\jconfig.py
5-
test\test.py
6-
vk_api\__init__.py
7-
vk_api\vk_api.py
8-
vk_api\vk_upload.py
1+
# file GENERATED by distutils, do NOT edit
2+
setup.py
3+
jconfig\__init__.py
4+
jconfig\jconfig.py
5+
test\test.py
6+
vk_api\__init__.py
7+
vk_api\vk_api.py
8+
vk_api\vk_upload.py

setup.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
23

34
'''
@@ -8,13 +9,10 @@
89
Copyright (C) 2013
910
'''
1011

11-
#!/usr/bin/env python
12-
""" Setup file for vk_api package """
13-
1412
from distutils.core import setup
1513
setup(name='vk_api',
16-
version='4.6',
17-
description='Module to use API VKontakte vk.com',
14+
version='4.6.3',
15+
description='Module to use API vk.com',
1816
author='Kirill Python',
1917
author_email='mikeking568@gmail.com',
2018
url='https://github.com/python273/vk_api',

vk_api/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'''
1010

1111
__author__ = "Kirill Python"
12-
__version__ = "4.6"
12+
__version__ = "4.6.3"
1313
__email__ = "mikeking568@gmail.com"
1414
__contact__ = "https://vk.com/python273"
1515

@@ -18,6 +18,8 @@
1818
if sys.version_info[0] == 2:
1919
from vk_api import *
2020
from vk_upload import *
21+
from vk_tools import *
2122
else:
2223
from .vk_api import *
2324
from .vk_upload import *
25+
from .vk_tools import *

vk_api/vk_api.py

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
'''
24
@author: Kirill Python
35
@contact: http://vk.com/python273
@@ -6,26 +8,26 @@
68
Copyright (C) 2013
79
'''
810

9-
# -*- coding: utf-8 -*-
1011
import jconfig
1112
import re
1213
import requests
13-
# import time
14+
import time
15+
16+
DELAY = 1.0 / 3 # 3 requests per second
1417

1518

1619
class VkApi(object):
1720
def __init__(self,
1821
login=None, password=None, number=None,
19-
sid=None, token=None,
22+
token=None,
2023
proxies=None,
21-
version='5.0', app_id=2895443, scope=2097151):
24+
version='5.4', app_id=2895443, scope=2097151):
2225
'''
2326
:param login: Логин ВКонтакте
2427
:param password: Пароль ВКонтакте
2528
:param number: Номер при проверке безопасности
2629
Номер: +7 12345678 90
2730
number = 12345678
28-
:param sid: cookie remixsid
2931
:param token: access_token
3032
:param proxies: proxy server
3133
{'http': 'http://127.0.0.1:8888/',
@@ -39,7 +41,7 @@ def __init__(self,
3941
self.password = password
4042
self.number = number
4143

42-
self.sid = sid
44+
self.sid = None
4345
self.token = {'access_token': token}
4446

4547
self.version = version
@@ -56,6 +58,8 @@ def __init__(self,
5658
}
5759
self.http.verify = False
5860

61+
self.last_request = 0.0
62+
5963
if login and password:
6064
self.sid = self.settings['remixsid']
6165
self.token = self.settings['access_token']
@@ -106,7 +110,24 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
106110
else:
107111
raise authorization_error('Authorization error (bad password)')
108112

109-
if 'security_check' in response.url and self.number:
113+
if 'security_check' in response.url:
114+
self.security_check(response)
115+
116+
def security_check(self, url=None, response=None):
117+
if url:
118+
response = self.http.get(url)
119+
120+
# Проверяем, является ли логин номером
121+
if not self.number:
122+
phone_postfix = regexp(r'class="phone_postfix">(.*?)</span>',
123+
response.text)
124+
125+
phone_postfix = phone_postfix[0].strip()
126+
127+
if self.login[-len(phone_postfix):] == phone_postfix:
128+
self.number = self.login
129+
130+
if self.number:
110131
number_hash = regexp(r'security_check.*?hash: \'(.*?)\'\};',
111132
response.text)[0]
112133

@@ -122,9 +143,10 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
122143
response = self.http.post('https://vk.com/login.php', values)
123144

124145
if response.text.split('<!>')[4] == '4':
125-
return
146+
return True
147+
148+
raise authorization_error('Security check (enter number)')
126149

127-
raise authorization_error('Authorization error (enter number)')
128150

129151
def check_sid(self):
130152
''' Прверка Cookies remixsid на валидность '''
@@ -180,37 +202,54 @@ def check_token(self):
180202
if self.token:
181203
try:
182204
self.method('isAppUser')
183-
except api_error:
205+
except apiError:
184206
return False
185207

186208
return True
187209

188210
def method(self, method, values=None):
189211
''' Использование методов API
190212
191-
method - название метода
213+
param: method - название метода
192214
'users.get'
193215
194-
values - параметры
216+
param: values - параметры
195217
{'uids': 1}
196218
'''
197-
198219
url = 'https://api.vk.com/method/%s' % method
199-
values = values or {}
220+
221+
if values:
222+
values = values.copy()
223+
else:
224+
values = {}
200225

201226
if not 'v' in values:
202227
values.update({'v': self.version})
203228

204229
if self.token:
205230
values.update({'access_token': self.token['access_token']})
206231

232+
# Ограничение 3 запроса в секунду
233+
sleep = DELAY - (time.time() - self.last_request)
234+
235+
if sleep > 0:
236+
time.sleep(sleep)
237+
207238
response = self.http.post(url, values).json()
239+
self.last_request = time.time()
240+
208241
if 'error' in response:
209242
# TODO: write me
210243
# Capcha handler
211244
# Capcha object
212-
# Error #17 handler
213-
raise api_error(response['error'])
245+
246+
error = apiError(response['error'], self, method, values)
247+
248+
if error.code == 17:
249+
print 'HANDLER 17'
250+
# TODO: number handler
251+
252+
raise error
214253
else:
215254
return response['response']
216255

@@ -231,10 +270,21 @@ class authorization_error(vk_api_error):
231270
pass
232271

233272

234-
class api_error(vk_api_error):
235-
pass
273+
class apiError(Exception):
274+
def __init__(self, error, vk, method, values):
275+
self.error = error
276+
self.vk = vk
277+
self.method = method
278+
self.values = values
279+
self.code = error['error_code']
280+
281+
def try_method(self):
282+
return self.vk.method(self.method, self.values)
283+
284+
def __str__(self):
285+
return self.error['error_msg']
236286

237287

238-
class capcha():
288+
class Capcha():
239289
pass
240290

vk_api/vk_tools.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# encoding: utf-8
2+
3+
4+
class VkTools(object):
5+
def __init__(self, vk):
6+
self.vk = vk
7+
8+
def get_all_items(self, method, values=None, max_count=200):
9+
''' Получить все элементы
10+
Работает в методах, где в ответе есть count и items
11+
12+
:param method: метод
13+
:param values: параметры
14+
:param max_count: максимальное количество,
15+
которое можно получить за один раз
16+
'''
17+
18+
if not values:
19+
values = {}
20+
else:
21+
values = values.copy()
22+
23+
values.update({'count': max_count})
24+
25+
response = self.vk.method(method, values)
26+
count = response['count']
27+
items = response['items']
28+
29+
for i in xrange(max_count, count + 1, max_count):
30+
values.update({
31+
'offset': i
32+
})
33+
34+
response = self.vk.method(method, values)
35+
items += response['items']
36+
37+
return {'count': len(items), 'items': items}

0 commit comments

Comments
 (0)