Skip to content

Commit 2fd6d72

Browse files
committed
Some refactoring
1 parent a3b5096 commit 2fd6d72

2 files changed

Lines changed: 20 additions & 22 deletions

File tree

vk_api/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class AuthorizationError(AuthError): # todo: delete
2727
pass
2828

2929

30+
class LoginRequired(AuthError):
31+
pass
32+
33+
3034
class PasswordRequired(AuthError):
3135
pass
3236

vk_api/vk_api.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ def auth(self, reauth=False, token_only=False):
131131
"""
132132

133133
if not self.login:
134-
self.logger.info('No login to auth')
135-
return
134+
raise LoginRequired('Login is required to auth')
136135

137136
self.logger.info('Auth with login: {}'.format(self.login))
138137

@@ -198,24 +197,12 @@ def _auth_token(self, reauth=False):
198197
self.vk_login()
199198
self.api_login()
200199

201-
def authorization(self, *args, **kwargs):
202-
import warnings
203-
warnings.simplefilter('always', DeprecationWarning)
204-
warnings.warn(
205-
'Please replace `VkApi.authorization` with `VkApi.auth` and '
206-
'`AuthorizationError` with `AuthError`',
207-
DeprecationWarning
208-
)
209-
210-
return self.auth(*args, **kwargs)
211-
212200
def vk_login(self, captcha_sid=None, captcha_key=None):
213201
""" Авторизация ВКонтакте с получением cookies remixsid """
214202

215203
self.logger.info('Logging in...')
216204

217205
if not self.password:
218-
self.logger.info('No password')
219206
raise PasswordRequired('Password is required to login')
220207

221208
self.http.cookies.clear()
@@ -257,7 +244,6 @@ def vk_login(self, captcha_sid=None, captcha_key=None):
257244
return self.error_handlers[CAPTCHA_ERROR_CODE](captcha)
258245

259246
if 'onLoginFailed(4' in response.text:
260-
self.logger.info('Bad password')
261247
raise BadPassword('Bad password')
262248

263249
if 'act=authcheck' in response.text:
@@ -316,8 +302,10 @@ def security_check(self, response=None):
316302

317303
if response is None:
318304
response = self.http.get('https://vk.com/settings')
305+
319306
if 'security_check' not in response.url:
320307
self.logger.info('Security check is not required')
308+
321309
return response
322310

323311
phone_prefix = clear_string(search_re(RE_PHONE_PREFIX, response.text))
@@ -426,7 +414,8 @@ def server_auth(self):
426414
}
427415

428416
response = self.http.post(
429-
'https://oauth.vk.com/access_token', values).json()
417+
'https://oauth.vk.com/access_token', values
418+
).json()
430419

431420
if 'error' in response:
432421
raise AuthError(response['error_description'])
@@ -467,18 +456,20 @@ def auth_handler(self):
467456
def get_api(self):
468457
return VkApiMethod(self)
469458

470-
def method(self, method, values=None, captcha_sid=None, captcha_key=None, raw=False):
459+
def method(self, method, values=None, captcha_sid=None, captcha_key=None,
460+
raw=False):
471461
""" Использование методов API
472462
473463
:param method: метод
474464
:param values: параметры
475465
:param captcha_sid:
476466
:param captcha_key:
477-
:param raw: при False возвращает response['response'], при True возвращает response
478-
e.g. может понадобиться для метода execute для получения execute_errors
467+
:param raw: при False возвращает response['response']
468+
при True возвращает response
469+
(может понадобиться для метода execute
470+
для получения execute_errors)
479471
"""
480472

481-
url = 'https://api.vk.com/method/%s' % method
482473
values = values.copy() if values else {}
483474

484475
if 'v' not in values:
@@ -498,7 +489,10 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None, raw=Fa
498489
if delay > 0:
499490
time.sleep(delay)
500491

501-
response = self.http.post(url, values)
492+
response = self.http.post(
493+
'https://api.vk.com/method/' + method,
494+
values
495+
)
502496
self.last_request = time.time()
503497

504498
if response.ok:
@@ -522,7 +516,7 @@ def method(self, method, values=None, captcha_sid=None, captcha_key=None, raw=Fa
522516
error.error['captcha_sid'],
523517
self.method,
524518
(method,),
525-
{'values': values},
519+
{'values': values, 'raw': raw},
526520
error.error['captcha_img']
527521
)
528522

0 commit comments

Comments
 (0)