Repository navigation
Expand file tree
/
Copy pathexceptions.py
More file actions
185 lines (121 loc) · 4.27 KB
/
Copy pathexceptions.py
File metadata and controls
185 lines (121 loc) · 4.27 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
# -*- coding: utf-8 -*-
"""
:authors: python273
:license: Apache License, Version 2.0, see LICENSE file
:copyright: (c) 2019 python273
"""
import typing as t
TWOFACTOR_CODE = -2
HTTP_ERROR_CODE = -1
TOO_MANY_RPS_CODE = 6
CAPTCHA_ERROR_CODE = 14
NEED_VALIDATION_CODE = 17
CONFIRMATION_ERROR_CODE = 1110
class VkApiError(Exception):
pass
class AccessDenied(VkApiError):
pass
class AuthError(VkApiError):
pass
class LoginRequired(AuthError):
pass
class PasswordRequired(AuthError):
pass
class BadPassword(AuthError):
pass
class AccountBlocked(AuthError):
pass
class TwoFactorError(AuthError):
pass
class SecurityCheck(AuthError):
def __init__(self, phone_prefix=None, phone_postfix=None, response=None):
super(SecurityCheck, self).__init__()
self.phone_prefix = phone_prefix
self.phone_postfix = phone_postfix
self.response = response
def __str__(self):
if self.phone_prefix and self.phone_postfix:
return f'Security check. Enter number: {self.phone_prefix} ... {self.phone_postfix}'
else:
return ('Security check. Phone prefix and postfix are not detected.'
' Please send bugreport (response in self.response)')
class ApiError(VkApiError):
def __init__(self, vk, method, values, raw, error):
super(ApiError, self).__init__()
self.vk = vk
self.method = method
self.values = values
self.raw = raw
self.code = error['error_code']
self.error = error
def try_method(self):
""" Отправить запрос заново """
return self.vk.method(self.method, self.values, raw=self.raw)
def __str__(self):
return f"[{self.error['error_code']}] {self.error['error_msg']}"
class ApiHttpError(VkApiError):
def __init__(self, vk, method, values, raw, response):
super(ApiHttpError, self).__init__()
self.vk = vk
self.method = method
self.values = values
self.raw = raw
self.response = response
def try_method(self):
""" Отправить запрос заново """
return self.vk.method(self.method, self.values, raw=self.raw)
def __str__(self):
return f'Response code {self.response.status_code}'
class Captcha(VkApiError):
def __init__(self, vk, captcha_sid, func, args=None, kwargs=None, url=None, redirect_uri=None):
super(Captcha, self).__init__()
self.vk = vk
self.sid = captcha_sid
self.func = func
self.args = args or ()
self.kwargs = kwargs or {}
self.code = CAPTCHA_ERROR_CODE
self.key = None
self.url = url
self.redirect_uri = redirect_uri
self.image = None
def get_url(self):
""" Получить ссылку на изображение капчи """
if not self.url:
self.url = f'https://api.vk.ru/captcha.php?sid={self.sid}'
return self.url
def get_image(self):
""" Получить изображение капчи (jpg) """
if not self.image:
self.image = self.vk.http.get(self.get_url()).content
return self.image
def try_again(self, key=None, success_token=None):
""" Отправить запрос заново с ответом капчи
:param key: ответ капчи
:param success_token: токен капчи с redirect_uri
"""
if key:
self.key = key
self.kwargs.update({
'captcha_sid': self.sid,
'captcha_key': self.key
})
if success_token:
self.kwargs.update({
'success_token': success_token,
})
return self.func(*self.args, **self.kwargs)
def __str__(self):
return 'Captcha needed'
class VkAudioException(Exception):
pass
class VkAudioUrlDecodeError(VkAudioException):
pass
class VkToolsException(VkApiError):
def __init__(self, *args, response=None):
super().__init__(*args)
self.response = response
class VkRequestsPoolException(Exception):
def __init__(self, error, *args, **kwargs):
self.error = error
super(VkRequestsPoolException, self).__init__(*args, **kwargs)