From 7f1d3e1adf0d87f230ee32b7cf39d90675d173d3 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Sun, 2 Oct 2022 13:17:00 -0400 Subject: [PATCH 1/3] Gestdown: Check for show before checking for subtitle (#1962) Will reduce the number of different calls to the provider and only ask for subtitle when we know the show exists. --- libs/subliminal_patch/providers/gestdown.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libs/subliminal_patch/providers/gestdown.py b/libs/subliminal_patch/providers/gestdown.py index 386d8f404..6af9920ea 100644 --- a/libs/subliminal_patch/providers/gestdown.py +++ b/libs/subliminal_patch/providers/gestdown.py @@ -104,9 +104,24 @@ class GestdownProvider(Provider): logger.debug("Found subtitle: %s", sub) yield sub + def _show_exists(self, video): + try: + response = self._session.get(f"{_BASE_URL}/shows/search/{video.series}") + response.raise_for_status() + return True + except HTTPError as error: + if error.response.status_code == 404: + return False + raise + + @_retry_on_423 def list_subtitles(self, video, languages): subtitles = [] + if not self._show_exists(video): + logger.debug("Couldn't find the show") + return subtitles + for language in languages: try: subtitles += self._subtitles_search(video, language) From 0cd89ea0f01746e0a86835fc6400aa9ca2e8db71 Mon Sep 17 00:00:00 2001 From: Vitiko Date: Sun, 2 Oct 2022 13:23:40 -0400 Subject: [PATCH 2/3] no log: update Gestdown tests --- tests/subliminal_patch/test_gestdown.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/subliminal_patch/test_gestdown.py b/tests/subliminal_patch/test_gestdown.py index cc79d1475..edef2e5fa 100644 --- a/tests/subliminal_patch/test_gestdown.py +++ b/tests/subliminal_patch/test_gestdown.py @@ -78,6 +78,7 @@ def test_subtitle_download(subtitle): def test_list_subtitles_423(episodes, requests_mock, mocker): mocker.patch("time.sleep") + requests_mock.get("https://api.gestdown.info/shows/search/Breaking%20Bad", status_code=200) requests_mock.get( f"{_BASE_URL}/subtitles/find/English/Breaking%20Bad/1/1", status_code=423 ) From 571ffbccf8544224bc63c9e51688be6310fb05e3 Mon Sep 17 00:00:00 2001 From: Vitiko Date: Mon, 3 Oct 2022 20:44:31 -0400 Subject: [PATCH 3/3] Fix provider pool updates --- libs/subliminal_patch/core.py | 23 +++++++------ tests/subliminal_patch/test_core.py | 53 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/libs/subliminal_patch/core.py b/libs/subliminal_patch/core.py index 88b16db1f..7b8625383 100644 --- a/libs/subliminal_patch/core.py +++ b/libs/subliminal_patch/core.py @@ -193,11 +193,22 @@ class SZProviderPool(ProviderPool): # Check if any new provider has been added updated = providers != self.providers or ban_list != self.ban_list - removed_providers = list(sorted(self.providers - providers)) - new_providers = list(sorted(providers - self.providers)) + removed_providers = set(sorted(self.providers - providers)) + + logger.debug("Discarded providers: %s | New providers: %s", self.discarded_providers, providers) + self.discarded_providers.difference_update(providers) + logger.debug("Updated discarded providers: %s", self.discarded_providers) + + removed_providers.update(self.discarded_providers) + + logger.debug("Removed providers: %s", removed_providers) + + self.providers.difference_update(removed_providers) + self.providers.update(list(providers)) # Terminate and delete removed providers from instance for removed in removed_providers: + logger.debug("Removing provider: %s", removed) try: del self[removed] # If the user has updated the providers but hasn't made any @@ -206,14 +217,6 @@ class SZProviderPool(ProviderPool): except KeyError: pass - if updated: - logger.debug("Removed providers: %s", removed_providers) - logger.debug("New providers: %s", new_providers) - - self.discarded_providers.difference_update(new_providers) - self.providers.difference_update(removed_providers) - self.providers.update(list(providers)) - # self.provider_configs = provider_configs self.provider_configs.update(provider_configs) diff --git a/tests/subliminal_patch/test_core.py b/tests/subliminal_patch/test_core.py index 17451ccdf..d6481ee16 100644 --- a/tests/subliminal_patch/test_core.py +++ b/tests/subliminal_patch/test_core.py @@ -1,5 +1,7 @@ from pathlib import Path +import pytest + from subliminal_patch import core @@ -17,3 +19,54 @@ def test_scan_video_episode(tmpdir): result = core.scan_video(str(video_path)) assert isinstance(result, core.Episode) + + +@pytest.fixture +def pool_instance(): + yield core.SZProviderPool({"argenteam"}, {}) + + +def test_pool_update_w_nothing(pool_instance): + pool_instance.update({}, {}, [], {}) + assert pool_instance.providers == set() + assert pool_instance.discarded_providers == set() + + +def test_pool_update_w_multiple_providers(pool_instance): + assert pool_instance.providers == {"argenteam"} + pool_instance.update({"argenteam", "subdivx", "subf2m"}, {}, [], {}) + assert pool_instance.providers == {"argenteam", "subdivx", "subf2m"} + + +def test_pool_update_discarded_providers(pool_instance): + assert pool_instance.providers == {"argenteam"} + + # Provider was discarded internally + pool_instance.discarded_providers = {"argenteam"} + + assert pool_instance.discarded_providers == {"argenteam"} + + # Provider is set to be used again + pool_instance.update({"subdivx", "argenteam"}, {}, [], {}) + + assert pool_instance.providers == {"argenteam", "subdivx"} + + # Provider should disappear from discarded providers + assert pool_instance.discarded_providers == set() + + +def test_pool_update_discarded_providers_2(pool_instance): + assert pool_instance.providers == {"argenteam"} + + # Provider was discarded internally + pool_instance.discarded_providers = {"argenteam"} + + assert pool_instance.discarded_providers == {"argenteam"} + + # Provider is not set to be used again + pool_instance.update({"subdivx"}, {}, [], {}) + + assert pool_instance.providers == {"subdivx"} + + # Provider should not disappear from discarded providers + assert pool_instance.discarded_providers == {"argenteam"}