Skip to content

Commit

Permalink
Merge development into master
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jul 22, 2023
2 parents 3c2f940 + 6e7858f commit 64af56c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 43 deletions.
17 changes: 17 additions & 0 deletions bazarr/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def save_settings(settings_items):
undefined_audio_track_default_changed = False
undefined_subtitles_track_default_changed = False
audio_tracks_parsing_changed = False
reset_providers = False

# Subzero Mods
update_subzero = False
Expand Down Expand Up @@ -491,46 +492,62 @@ def save_settings(settings_items):

if key == 'settings-addic7ed-username':
if key != settings.addic7ed.username:
reset_providers = True
region.delete('addic7ed_data')
elif key == 'settings-addic7ed-password':
if key != settings.addic7ed.password:
reset_providers = True
region.delete('addic7ed_data')

if key == 'settings-legendasdivx-username':
if key != settings.legendasdivx.username:
reset_providers = True
region.delete('legendasdivx_cookies2')
elif key == 'settings-legendasdivx-password':
if key != settings.legendasdivx.password:
reset_providers = True
region.delete('legendasdivx_cookies2')

if key == 'settings-opensubtitles-username':
if key != settings.opensubtitles.username:
reset_providers = True
region.delete('os_token')
elif key == 'settings-opensubtitles-password':
if key != settings.opensubtitles.password:
reset_providers = True
region.delete('os_token')

if key == 'settings-opensubtitlescom-username':
if key != settings.opensubtitlescom.username:
reset_providers = True
region.delete('oscom_token')
elif key == 'settings-opensubtitlescom-password':
if key != settings.opensubtitlescom.password:
reset_providers = True
region.delete('oscom_token')

if key == 'settings-subscene-username':
if key != settings.subscene.username:
reset_providers = True
region.delete('subscene_cookies2')
elif key == 'settings-subscene-password':
if key != settings.subscene.password:
reset_providers = True
region.delete('subscene_cookies2')

if key == 'settings-titlovi-username':
if key != settings.titlovi.username:
reset_providers = True
region.delete('titlovi_token')
elif key == 'settings-titlovi-password':
if key != settings.titlovi.password:
reset_providers = True
region.delete('titlovi_token')

if reset_providers:
from .get_providers import reset_throttled_providers
reset_throttled_providers(only_auth_or_conf_error=True)

if settings_keys[0] == 'settings':
settings[settings_keys[1]][settings_keys[2]] = str(value)

Expand Down
13 changes: 10 additions & 3 deletions bazarr/app/get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from subliminal_patch.exceptions import TooManyRequests, APIThrottled, ParseResponseError, IPAddressBlocked, \
MustGetBlacklisted, SearchLimitReached
from subliminal.providers.opensubtitles import DownloadLimitReached
from subliminal.exceptions import DownloadLimitExceeded, ServiceUnavailable
from subliminal.exceptions import DownloadLimitExceeded, ServiceUnavailable, AuthenticationError, ConfigurationError
from subliminal import region as subliminal_cache_region
from subliminal_patch.extensions import provider_registry

Expand Down Expand Up @@ -76,6 +76,8 @@ def provider_throttle_map():
APIThrottled: (datetime.timedelta(seconds=15), "15 seconds"),
},
"opensubtitlescom": {
AuthenticationError: (datetime.timedelta(hours=12), "12 hours"),
ConfigurationError: (datetime.timedelta(hours=12), "12 hours"),
TooManyRequests: (datetime.timedelta(minutes=1), "1 minute"),
DownloadLimitExceeded: (datetime.timedelta(hours=24), "24 hours"),
},
Expand Down Expand Up @@ -413,12 +415,17 @@ def list_throttled_providers():
return throttled_providers


def reset_throttled_providers():
def reset_throttled_providers(only_auth_or_conf_error=False):
for provider in list(tp):
if only_auth_or_conf_error and tp[provider][0] not in ['AuthenticationError', 'ConfigurationError']:
continue
del tp[provider]
set_throttled_providers(str(tp))
update_throttled_provider()
logging.info('BAZARR throttled providers have been reset.')
if only_auth_or_conf_error:
logging.info('BAZARR throttled providers have been reset (only AuthenticationError and ConfigurationError).')
else:
logging.info('BAZARR throttled providers have been reset.')


def get_throttled_providers():
Expand Down
74 changes: 34 additions & 40 deletions libs/subliminal_patch/providers/opensubtitlescom.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,34 @@ def __init__(self, username=None, password=None, use_hash=True, api_key=None):

def initialize(self):
self._started = time.time()
self.login()

if region.get("oscom_token", expiration_time=TOKEN_EXPIRATION_TIME) is NO_VALUE:
logger.debug("No cached token, we'll try to login again.")
self.login()
else:
self.token = region.get("oscom_token", expiration_time=TOKEN_EXPIRATION_TIME)

def terminate(self):
self.session.close()

def ping(self):
return self._started and (time.time() - self._started) < TOKEN_EXPIRATION_TIME

def login(self):
r = self.retry(
lambda: self.checked(
lambda: self.session.post(self.server_url + 'login',
json={"username": self.username, "password": self.password},
allow_redirects=False,
timeout=30),
validate_json=True,
json_key_name='token'
),
amount=retry_amount
)
def login(self, is_retry=False):
r = self.checked(
lambda: self.session.post(self.server_url + 'login',
json={"username": self.username, "password": self.password},
allow_redirects=False,
timeout=30),
is_retry=is_retry)

try:
self.token = r.json()['token']
except (ValueError, JSONDecodeError):
return False
log_request_response(r)
raise ProviderError("Cannot get token from provider login response")
else:
region.set("oscom_token", self.token)
return True

@staticmethod
def sanitize_external_ids(external_id):
Expand Down Expand Up @@ -262,9 +262,6 @@ def search_titles(self, title):
logger.debug(f'No match found for {title}')

def query(self, languages, video):
if region.get("oscom_token", expiration_time=TOKEN_EXPIRATION_TIME) is NO_VALUE:
logger.debug("No cached token, we'll try to login again.")
self.login()
self.video = video
if self.use_hash:
file_hash = self.video.hashes.get('opensubtitlescom')
Expand All @@ -289,9 +286,11 @@ def query(self, languages, video):
if not title_id:
return []

lang_strings = [to_opensubtitlescom(lang.basename) for lang in languages]
# be sure to remove duplicates
lang_strings = list(set([to_opensubtitlescom(lang.basename) for lang in languages]))

langs = ','.join(lang_strings)
logging.debug(f'Searching for this languages: {lang_strings}')
logging.debug(f'Searching for those languages: {lang_strings}')

# query the server
if isinstance(self.video, Episode):
Expand Down Expand Up @@ -400,13 +399,6 @@ def list_subtitles(self, video, languages):
return self.query(languages, video)

def download_subtitle(self, subtitle):
if region.get("oscom_token", expiration_time=TOKEN_EXPIRATION_TIME) is NO_VALUE:
logger.debug("No cached token, we'll try to login again.")
self.login()
if self.token is NO_VALUE:
logger.debug("Unable to obtain an authentication token right now, we'll try again later.")
raise ProviderError("Unable to obtain an authentication token")

logger.info('Downloading subtitle %r', subtitle)

headers = {'Accept': 'application/json', 'Content-Type': 'application/json',
Expand Down Expand Up @@ -442,19 +434,22 @@ def download_subtitle(self, subtitle):
subtitle_content = r.content
subtitle.content = fix_line_ending(subtitle_content)

def reset_token(self):
@staticmethod
def reset_token():
logging.debug('Authentication failed: clearing cache and attempting to login.')
region.delete("oscom_token")
return self.login()
return

def checked(self, fn, raise_api_limit=False, validate_json=False, json_key_name=None, validate_content=False):
def checked(self, fn, raise_api_limit=False, validate_json=False, json_key_name=None, validate_content=False,
is_retry=False):
"""Run :fn: and check the response status before returning it.
:param fn: the function to make an API call to OpenSubtitles.com.
:param raise_api_limit: if True we wait a little bit longer before running the call again.
:param validate_json: test if response is valid json.
:param json_key_name: test if returned json contain a specific key.
:param validate_content: test if response have a content (used with download).
:param is_retry: prevent additional retries with login endpoint.
:return: the response.
"""
Expand All @@ -465,7 +460,7 @@ def checked(self, fn, raise_api_limit=False, validate_json=False, json_key_name=
except APIThrottled:
if not raise_api_limit:
logger.info("API request limit hit, waiting and trying again once.")
time.sleep(2)
time.sleep(15)
return self.checked(fn, raise_api_limit=True)
raise
except (ConnectionError, Timeout, ReadTimeout):
Expand All @@ -478,17 +473,16 @@ def checked(self, fn, raise_api_limit=False, validate_json=False, json_key_name=
except Exception:
status_code = None
else:
if status_code == 400:
raise ConfigurationError('Do not use email but username')
elif status_code == 401:
time.sleep(1)
if status_code == 401:
log_request_response(response)
logged_in = self.reset_token()
if logged_in:
return self.checked(fn, raise_api_limit=raise_api_limit, validate_json=validate_json,
json_key_name=json_key_name, validate_content=validate_content)
else:
self.reset_token()
if is_retry:
raise AuthenticationError('Login failed')
else:
time.sleep(1)
self.login(is_retry=True)
self.checked(fn, raise_api_limit=raise_api_limit, validate_json=validate_json,
json_key_name=json_key_name, validate_content=validate_content, is_retry=True)
elif status_code == 403:
log_request_response(response)
raise ProviderError("Bazarr API key seems to be in problem")
Expand Down

0 comments on commit 64af56c

Please sign in to comment.