From 762cd61b215e9f6aa8f0dfee9855f80bc7753f04 Mon Sep 17 00:00:00 2001 From: morpheus65535 Date: Mon, 22 Mar 2021 10:16:51 -0400 Subject: [PATCH] Fixed OpenSubtitles.com token expiration --- libs/subliminal_patch/providers/opensubtitlescom.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/subliminal_patch/providers/opensubtitlescom.py b/libs/subliminal_patch/providers/opensubtitlescom.py index c3d6a5b8d..716eef6e1 100644 --- a/libs/subliminal_patch/providers/opensubtitlescom.py +++ b/libs/subliminal_patch/providers/opensubtitlescom.py @@ -24,6 +24,7 @@ from guessit import guessit logger = logging.getLogger(__name__) SHOW_EXPIRATION_TIME = datetime.timedelta(weeks=1).total_seconds() +TOKEN_EXPIRATION_TIME = datetime.timedelta(hours=12).total_seconds() def fix_tv_naming(title): @@ -143,16 +144,13 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider): self.use_hash = use_hash def initialize(self): + self.login() self.token = region.get("oscom_token") - if self.token: - self.session.headers.update({'Authorization': 'Beaker ' + self.token}) - return True - else: - self.login() def terminate(self): self.session.close() + @region.cache_on_arguments(expiration_time=TOKEN_EXPIRATION_TIME) def login(self): try: r = self.session.post(self.server_url + 'login', @@ -168,7 +166,6 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider): except ValueError: raise ProviderError('Invalid JSON returned by provider') else: - self.session.headers.update({'Authorization': 'Beaker ' + self.token}) region.set("oscom_token", self.token) return True elif r.status_code == 401: @@ -324,7 +321,8 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider): def download_subtitle(self, subtitle): logger.info('Downloading subtitle %r', subtitle) - headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} + headers = {'Accept': 'application/json', 'Content-Type': 'application/json', + 'Authorization': 'Beaker ' + self.token} res = self.session.post(self.server_url + 'download', json={'file_id': subtitle.file_id, 'sub_format': 'srt'}, headers=headers,