From 545f83dcc81f43bc297d226f72a0001ec8eeaa5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Bart=C3=ADk?= <63553146+sambartik@users.noreply.github.com> Date: Wed, 27 Nov 2024 01:16:35 +0100 Subject: [PATCH] Handle losing titulky VIP status during an active login session --- .../subliminal_patch/providers/titulky.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/custom_libs/subliminal_patch/providers/titulky.py b/custom_libs/subliminal_patch/providers/titulky.py index 0e8a6b9a7..54dd7c8c5 100644 --- a/custom_libs/subliminal_patch/providers/titulky.py +++ b/custom_libs/subliminal_patch/providers/titulky.py @@ -191,7 +191,7 @@ def login(self, bypass_cache=False): location_qs = parse_qs(urlparse(res.headers['Location']).query) # If the response is a redirect and doesnt point to an error message page, then we are logged in - if res.status_code == 302 and location_qs['msg_type'][0] == 'i': + if res.status_code == 302 and location_qs['msg_type'][0].lower() == 'i': if 'omezené' in location_qs['msg'][0].lower(): raise AuthenticationError("V.I.P. account is required for this provider to work!") else: @@ -245,16 +245,28 @@ def get_request(self, url, ref=server_url, allow_redirects=False, _recursion=0): return res location_qs = parse_qs(urlparse(res.headers['Location']).query) - # If the msg_type query parameter does NOT equal to 'e' or is absent, follow the URL in the Location header. - if allow_redirects is True and ('msg_type' not in location_qs or ('msg_type' in location_qs and location_qs['msg_type'][0] != 'e')): + + # If the redirect url does not contain an error message, we follow the redirect right away + if 'msg_type' not in location_qs or ('msg_type' in location_qs and (location_qs['msg_type'][0]).lower() != 'e'): return self.get_request(urljoin(res.headers['Origin'] or self.server_url, res.headers['Location']), ref=url, allow_redirects=True, _recursion=(_recursion + 1)) - # Check if we got redirected because login cookies expired. - if "přihlašte" in location_qs['msg'][0].lower(): + # We got redirected to a page with an error message: + error_message = location_qs['msg'][0].lower() + + # Check if we got redirected because login cookies expired and try to relogin + if "přihlašte" in error_message: logger.info(f"Titulky.com: Login cookies expired.") self.login(True) return self.get_request(url, ref=ref, allow_redirects=True, _recursion=(_recursion + 1)) + # Check if we got redirected because our VIP expired + if "omezené" in error_message: + raise AuthenticationError("V.I.P. status expired."); + + # TODO: We don't know why we got redirected to an error page. + # What should we do? I am not aware if there is a use case where we want to return such response anway. + raise ProviderError(f"Got redirected from {url} to an error page with message: \"{location_qs['msg'][0]}\""); + return res def fetch_page(self, url, ref=server_url, allow_redirects=False):