Skip to content

Commit dca85cd

Browse files
authored
Merge pull request #16 from ScrapingAnt/feature/issue14-handle-423-status-code
feature/issue14-handle-423-status-code: done
2 parents 04b9f20 + 3486300 commit dca85cd

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Class defining response from API.
8686
| ScrapingantInvalidInputException | Invalid value provided. Please, look into error message for more info |
8787
| ScrapingantInternalException | Something went wrong with the server side code. Try again later or contact ScrapingAnt support |
8888
| ScrapingantSiteNotReachableException | The requested URL is not reachable. Please, check it locally |
89+
| ScrapingantDetectedException | The anti-bot detection system has detected the request. Please, retry or change the request settings. |
8990

9091
* * *
9192

scrapingant_client/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.4"
1+
__version__ = "0.3.5"
22

33
from scrapingant_client.client import ScrapingAntClient
44
from scrapingant_client.cookie import Cookie
@@ -8,6 +8,7 @@
88
ScrapingantInvalidInputException,
99
ScrapingantInternalException,
1010
ScrapingantSiteNotReachableException,
11+
ScrapingantDetectedException,
1112
)
1213
from scrapingant_client.response import Response
1314

@@ -19,5 +20,6 @@
1920
'ScrapingantInvalidInputException',
2021
'ScrapingantInternalException',
2122
'ScrapingantSiteNotReachableException',
23+
'ScrapingantDetectedException',
2224
'Response',
2325
]

scrapingant_client/client.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
ScrapingantInvalidInputException,
1313
ScrapingantInternalException,
1414
ScrapingantSiteNotReachableException,
15+
ScrapingantDetectedException,
1516
)
1617
from scrapingant_client.response import Response
1718
from scrapingant_client.utils import base64_encode_string
@@ -53,10 +54,12 @@ def general_request(
5354
)
5455
if response.status_code == 403:
5556
raise ScrapingantInvalidTokenException()
56-
elif response.status_code == 422:
57-
raise ScrapingantInvalidInputException(response.text)
5857
elif response.status_code == 404:
5958
raise ScrapingantSiteNotReachableException(url)
59+
elif response.status_code == 422:
60+
raise ScrapingantInvalidInputException(response.text)
61+
elif response.status_code == 423:
62+
raise ScrapingantDetectedException()
6063
elif response.status_code == 500:
6164
raise ScrapingantInternalException()
6265
json_response = response.json()

scrapingant_client/errors.py

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def __init__(self, url):
1919
super().__init__(message)
2020

2121

22+
class ScrapingantDetectedException(ScrapingantClientException):
23+
def __init__(self):
24+
message = 'The anti-bot detection system has detected the request. ' \
25+
'Please, retry or change the request settings.'
26+
super().__init__(message)
27+
28+
2229
class ScrapingantInternalException(ScrapingantClientException):
2330
def __init__(self):
2431
message = 'Something went wrong with the server side. Please try later or contact support'

tests/test_exceptions.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ScrapingantInvalidInputException,
88
ScrapingantInternalException,
99
ScrapingantSiteNotReachableException,
10+
ScrapingantDetectedException,
1011
)
1112
from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL
1213

@@ -47,3 +48,13 @@ def test_not_reachable():
4748
with pytest.raises(ScrapingantSiteNotReachableException) as e:
4849
client.general_request('example.com')
4950
assert 'The requested URL is not reachable (example.com)' in str(e)
51+
52+
53+
@responses.activate
54+
def test_detected():
55+
responses.add(responses.POST, SCRAPINGANT_API_BASE_URL + '/general',
56+
json={}, status=423)
57+
client = ScrapingAntClient(token='some_token')
58+
with pytest.raises(ScrapingantDetectedException) as e:
59+
client.general_request('example.com')
60+
assert 'The anti-bot detection system has detected the request' in str(e)

0 commit comments

Comments
 (0)