Skip to content

Commit 719d580

Browse files
committed
Merge branch 'dev'
2 parents 157048a + c261349 commit 719d580

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
build/
22
dist/
3-
test/
43
*.pyc
5-
upload_to_PyPi.py
64
.project
75
.pydevproject
86
.settings/
9-
.idea/
107
/testing

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
setup(
1616
name='vk_api',
17-
version='6.0',
17+
version='6.1',
1818
author='Kirill Python',
1919
author_email='python273@ya.ru',
2020
url='https://github.com/python273/vk_api',

vk_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010

1111
__author__ = 'Kirill Python'
12-
__version__ = '6.0'
12+
__version__ = '6.1'
1313
__email__ = 'python273@ya.ru'
1414
__contact__ = 'https://vk.com/python273'
1515

vk_api/vk_api.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import time
1515

1616
DELAY = 0.36 # 3 requests per second
17+
TOO_MANY_RPS_CODE = 6
1718
CAPTCHA_ERROR_CODE = 14
1819
NEED_VALIDATION_CODE = 17
1920
HTTP_ERROR_CODE = -1
2021

22+
RE_LOGIN_HASH = re.compile(r'name="lg_h" value="([a-z0-9]+)"')
2123
RE_CAPTCHAID = re.compile(r'sid=(\d+)')
2224
RE_NUMBER_HASH = re.compile(r"al_page: '3', hash: '([a-z0-9]+)'")
2325
RE_TOKEN_URL = re.compile(r'location\.href = "(.*?)"\+addr;')
@@ -83,7 +85,8 @@ def __init__(self, login=None, password=None, number=None, sec_number=None,
8385

8486
self.error_handlers = {
8587
NEED_VALIDATION_CODE: self.need_validation_handler,
86-
CAPTCHA_ERROR_CODE: captcha_handler or self.captcha_handler
88+
CAPTCHA_ERROR_CODE: captcha_handler or self.captcha_handler,
89+
TOO_MANY_RPS_CODE: self.too_many_rps_handler
8790
}
8891

8992
def authorization(self):
@@ -103,12 +106,16 @@ def authorization(self):
103106
def vk_login(self, captcha_sid=None, captcha_key=None):
104107
""" Авторизация ВКонтакте с получением cookies remixsid """
105108

106-
url = 'https://login.vk.com/'
109+
self.http.cookies.clear()
110+
111+
# Get cookies
112+
response = self.http.get('https://vk.com/')
113+
107114
values = {
108115
'act': 'login',
109-
'utf8': '1',
110116
'email': self.login,
111-
'pass': self.password
117+
'pass': self.password,
118+
'lg_h': search_re(RE_LOGIN_HASH, response.text)
112119
}
113120

114121
if captcha_sid and captcha_key:
@@ -117,8 +124,7 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
117124
'captcha_key': captcha_key
118125
})
119126

120-
self.http.cookies.clear()
121-
response = self.http.post(url, values)
127+
response = self.http.post('https://login.vk.com/', values)
122128

123129
remixsid = None
124130

@@ -270,6 +276,10 @@ def http_handler(self, error):
270276
""" Handle connection errors """
271277
pass
272278

279+
def too_many_rps_handler(self, error):
280+
time.sleep(0.5)
281+
error.try_method()
282+
273283
def method(self, method, values=None, captcha_sid=None, captcha_key=None):
274284
""" Использование методов API
275285
@@ -320,10 +330,9 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None):
320330

321331
if 'error' in response:
322332
error = ApiError(self, method, values, response['error'])
323-
error_code = error.code
324333

325-
if error_code in self.error_handlers:
326-
if error_code == CAPTCHA_ERROR_CODE:
334+
if error.code in self.error_handlers:
335+
if error.code == CAPTCHA_ERROR_CODE:
327336

328337
error = Captcha(
329338
self,
@@ -334,7 +343,7 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None):
334343
error.error['captcha_img']
335344
)
336345

337-
response = self.error_handlers[error_code](error)
346+
response = self.error_handlers[error.code](error)
338347

339348
if response is not None:
340349
return response

0 commit comments

Comments
 (0)