Skip to content

Commit a146e23

Browse files
VarnaX 279python273
andauthored
Add Flood Wait Exception (#42)
Co-authored-by: Kirill <iam@python273.pw>
1 parent df18a3d commit a146e23

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

telegraph/api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import requests
55

6-
from .exceptions import TelegraphException
6+
from .exceptions import TelegraphException, RetryAfterError
77
from .utils import html_to_nodes, nodes_to_html
88

99

@@ -34,7 +34,12 @@ def method(self, method, values=None, path=''):
3434
if response.get('ok'):
3535
return response['result']
3636

37-
raise TelegraphException(response.get('error'))
37+
error = response.get('error')
38+
if isinstance(error, str) and error.startswith('FLOOD_WAIT_'):
39+
retry_after = int(error.rsplit('_', 1)[-1])
40+
raise RetryAfterError(retry_after)
41+
else:
42+
raise TelegraphException(error)
3843

3944

4045
class Telegraph(object):

telegraph/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ class NotAllowedTag(ParsingException):
1414

1515
class InvalidHTML(ParsingException):
1616
pass
17+
18+
19+
class RetryAfterError(TelegraphException):
20+
21+
def __init__(self, retry_after: int):
22+
self.retry_after = retry_after
23+
super().__init__(f'Flood control exceeded. Retry in {retry_after} seconds')
24+

telegraph/upload.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import requests
44

5-
from .exceptions import TelegraphException
5+
from .exceptions import TelegraphException, RetryAfterError
66

77

88
def upload_file(f):
@@ -24,7 +24,11 @@ def upload_file(f):
2424
error = response.get('error')
2525

2626
if error:
27-
raise TelegraphException(error)
27+
if isinstance(error, str) and error.startswith('FLOOD_WAIT_'):
28+
retry_after = int(error.rsplit('_',1)[-1])
29+
raise RetryAfterError(retry_after)
30+
else:
31+
raise TelegraphException(error)
2832

2933
return [i['src'] for i in response]
3034

0 commit comments

Comments
 (0)