diff options
-rw-r--r-- | dev-requirements.txt | 1 | ||||
-rw-r--r-- | libs/subliminal_patch/providers/gestdown.py | 24 | ||||
-rw-r--r-- | tests/subliminal_patch/test_gestdown.py | 16 |
3 files changed, 38 insertions, 3 deletions
diff --git a/dev-requirements.txt b/dev-requirements.txt index cbea12f0a..550ab0d0e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -6,3 +6,4 @@ pytest-flakes pytest-cov pytest-vcr pytest-mock +requests-mock diff --git a/libs/subliminal_patch/providers/gestdown.py b/libs/subliminal_patch/providers/gestdown.py index 56b922b6d..f66809864 100644 --- a/libs/subliminal_patch/providers/gestdown.py +++ b/libs/subliminal_patch/providers/gestdown.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import logging +import time from requests import HTTPError from requests import Session @@ -37,6 +38,28 @@ class GestdownSubtitle(Subtitle): return self._id +def _retry_on_423(method): + def retry(self, *args, **kwargs): + retries = 0 + while 5 > retries: + try: + yield from method(self, *args, **kwargs) + except HTTPError as error: + if error.response.status_code != 423: + raise + + retries += 1 + + logger.debug("423 returned. Retrying in 30 seconds") + time.sleep(30) + else: + break + + logger.debug("Retries limit exceeded. Ignoring query") + + return retry + + class GestdownProvider(Provider): provider_name = "gestdown" @@ -61,6 +84,7 @@ class GestdownProvider(Provider): def terminate(self): self._session.close() + @_retry_on_423 def _subtitles_search(self, video, language: Language): json_data = { "search": f"{video.series} S{video.season:02}E{video.episode:02}", diff --git a/tests/subliminal_patch/test_gestdown.py b/tests/subliminal_patch/test_gestdown.py index 4a67304d9..59310774f 100644 --- a/tests/subliminal_patch/test_gestdown.py +++ b/tests/subliminal_patch/test_gestdown.py @@ -1,11 +1,10 @@ import pytest - +from subliminal_patch.language import PatchedAddic7edConverter +from subliminal_patch.providers.gestdown import _BASE_URL from subliminal_patch.providers.gestdown import GestdownProvider from subliminal_patch.providers.gestdown import GestdownSubtitle from subzero.language import Language -from subliminal_patch.language import PatchedAddic7edConverter - def test_language_list_is_convertible(): converter = PatchedAddic7edConverter() @@ -75,3 +74,14 @@ def test_subtitle_download(subtitle): provider.download_subtitle(subtitle) assert subtitle.content is not None assert subtitle.is_valid() + + +def test_subtitles_search_423(episodes, requests_mock, mocker): + mocker.patch("time.sleep") + requests_mock.post(f"{_BASE_URL}/subtitles/search", status_code=423) + + with GestdownProvider() as provider: + gen = provider._subtitles_search( + episodes["breaking_bad_s01e01"], Language.fromietf("en") + ) + assert not list(gen) |