From 3486300b90e6dfc8ec73700c285604a3edeb393c Mon Sep 17 00:00:00 2001 From: "andrii.kovalenko" Date: Tue, 24 Aug 2021 16:37:37 +0300 Subject: [PATCH] feature/issue14-handle-423-status-code: done --- README.md | 1 + scrapingant_client/__init__.py | 4 +++- scrapingant_client/client.py | 7 +++++-- scrapingant_client/errors.py | 7 +++++++ tests/test_exceptions.py | 11 +++++++++++ 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3066eb3..88870c3 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Class defining response from API. | ScrapingantInvalidInputException | Invalid value provided. Please, look into error message for more info | | ScrapingantInternalException | Something went wrong with the server side code. Try again later or contact ScrapingAnt support | | ScrapingantSiteNotReachableException | The requested URL is not reachable. Please, check it locally | +| ScrapingantDetectedException | The anti-bot detection system has detected the request. Please, retry or change the request settings. | * * * diff --git a/scrapingant_client/__init__.py b/scrapingant_client/__init__.py index 345b0f9..07a7dff 100644 --- a/scrapingant_client/__init__.py +++ b/scrapingant_client/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.3.4" +__version__ = "0.3.5" from scrapingant_client.client import ScrapingAntClient from scrapingant_client.cookie import Cookie @@ -8,6 +8,7 @@ ScrapingantInvalidInputException, ScrapingantInternalException, ScrapingantSiteNotReachableException, + ScrapingantDetectedException, ) from scrapingant_client.response import Response @@ -19,5 +20,6 @@ 'ScrapingantInvalidInputException', 'ScrapingantInternalException', 'ScrapingantSiteNotReachableException', + 'ScrapingantDetectedException', 'Response', ] diff --git a/scrapingant_client/client.py b/scrapingant_client/client.py index 2e4f0c8..f11c390 100644 --- a/scrapingant_client/client.py +++ b/scrapingant_client/client.py @@ -12,6 +12,7 @@ ScrapingantInvalidInputException, ScrapingantInternalException, ScrapingantSiteNotReachableException, + ScrapingantDetectedException, ) from scrapingant_client.response import Response from scrapingant_client.utils import base64_encode_string @@ -53,10 +54,12 @@ def general_request( ) if response.status_code == 403: raise ScrapingantInvalidTokenException() - elif response.status_code == 422: - raise ScrapingantInvalidInputException(response.text) elif response.status_code == 404: raise ScrapingantSiteNotReachableException(url) + elif response.status_code == 422: + raise ScrapingantInvalidInputException(response.text) + elif response.status_code == 423: + raise ScrapingantDetectedException() elif response.status_code == 500: raise ScrapingantInternalException() json_response = response.json() diff --git a/scrapingant_client/errors.py b/scrapingant_client/errors.py index 0d70971..a431ab3 100644 --- a/scrapingant_client/errors.py +++ b/scrapingant_client/errors.py @@ -19,6 +19,13 @@ def __init__(self, url): super().__init__(message) +class ScrapingantDetectedException(ScrapingantClientException): + def __init__(self): + message = 'The anti-bot detection system has detected the request. ' \ + 'Please, retry or change the request settings.' + super().__init__(message) + + class ScrapingantInternalException(ScrapingantClientException): def __init__(self): message = 'Something went wrong with the server side. Please try later or contact support' diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index dbc6887..26d886f 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -7,6 +7,7 @@ ScrapingantInvalidInputException, ScrapingantInternalException, ScrapingantSiteNotReachableException, + ScrapingantDetectedException, ) from scrapingant_client.constants import SCRAPINGANT_API_BASE_URL @@ -47,3 +48,13 @@ def test_not_reachable(): with pytest.raises(ScrapingantSiteNotReachableException) as e: client.general_request('example.com') assert 'The requested URL is not reachable (example.com)' in str(e) + + +@responses.activate +def test_detected(): + responses.add(responses.POST, SCRAPINGANT_API_BASE_URL + '/general', + json={}, status=423) + client = ScrapingAntClient(token='some_token') + with pytest.raises(ScrapingantDetectedException) as e: + client.general_request('example.com') + assert 'The anti-bot detection system has detected the request' in str(e)