Skip to content

Commit 09ef200

Browse files
committed
Fix authorization
add too_many_rps_handler (too many requests per second)
1 parent b942281 commit 09ef200

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

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)