|
|
@ -193,7 +193,7 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
|
|
|
|
|
|
|
|
def login(self):
|
|
|
|
def login(self):
|
|
|
|
r = self.retry(
|
|
|
|
r = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.post(self.server_url + 'login',
|
|
|
|
lambda: self.session.post(self.server_url + 'login',
|
|
|
|
json={"username": self.username, "password": self.password},
|
|
|
|
json={"username": self.username, "password": self.password},
|
|
|
|
allow_redirects=False,
|
|
|
|
allow_redirects=False,
|
|
|
@ -204,9 +204,13 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
amount=retry_amount
|
|
|
|
amount=retry_amount
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
self.token = r.json()['token']
|
|
|
|
try:
|
|
|
|
region.set("oscom_token", self.token)
|
|
|
|
self.token = r.json()['token']
|
|
|
|
return
|
|
|
|
except (ValueError, JSONDecodeError):
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
region.set("oscom_token", self.token)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def sanitize_external_ids(external_id):
|
|
|
|
def sanitize_external_ids(external_id):
|
|
|
@ -223,29 +227,14 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
logging.debug(f'Searching using this title: {title}')
|
|
|
|
logging.debug(f'Searching using this title: {title}')
|
|
|
|
|
|
|
|
|
|
|
|
results = self.retry(
|
|
|
|
results = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.get(self.server_url + 'features', params=parameters, timeout=30),
|
|
|
|
lambda: self.session.get(self.server_url + 'features', params=parameters, timeout=30),
|
|
|
|
validate_token=True,
|
|
|
|
|
|
|
|
validate_json=True,
|
|
|
|
validate_json=True,
|
|
|
|
json_key_name='data'
|
|
|
|
json_key_name='data'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
amount=retry_amount
|
|
|
|
amount=retry_amount
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if results == 401:
|
|
|
|
|
|
|
|
logging.debug('Authentication failed: clearing cache and attempting to login.')
|
|
|
|
|
|
|
|
region.delete("oscom_token")
|
|
|
|
|
|
|
|
self.login()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = self.retry(
|
|
|
|
|
|
|
|
lambda: checked(
|
|
|
|
|
|
|
|
lambda: self.session.get(self.server_url + 'features', params=parameters, timeout=30),
|
|
|
|
|
|
|
|
validate_json=True,
|
|
|
|
|
|
|
|
json_key_name='data'
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
amount=retry_amount
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# deserialize results
|
|
|
|
# deserialize results
|
|
|
|
results_dict = results.json()['data']
|
|
|
|
results_dict = results.json()['data']
|
|
|
|
|
|
|
|
|
|
|
@ -307,7 +296,7 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
# query the server
|
|
|
|
# query the server
|
|
|
|
if isinstance(self.video, Episode):
|
|
|
|
if isinstance(self.video, Episode):
|
|
|
|
res = self.retry(
|
|
|
|
res = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.get(self.server_url + 'subtitles',
|
|
|
|
lambda: self.session.get(self.server_url + 'subtitles',
|
|
|
|
params=(('ai_translated', 'exclude'),
|
|
|
|
params=(('ai_translated', 'exclude'),
|
|
|
|
('episode_number', self.video.episode),
|
|
|
|
('episode_number', self.video.episode),
|
|
|
@ -325,7 +314,7 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
res = self.retry(
|
|
|
|
res = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.get(self.server_url + 'subtitles',
|
|
|
|
lambda: self.session.get(self.server_url + 'subtitles',
|
|
|
|
params=(('ai_translated', 'exclude'),
|
|
|
|
params=(('ai_translated', 'exclude'),
|
|
|
|
('id', title_id if title_id else None),
|
|
|
|
('id', title_id if title_id else None),
|
|
|
@ -423,7 +412,7 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
headers = {'Accept': 'application/json', 'Content-Type': 'application/json',
|
|
|
|
headers = {'Accept': 'application/json', 'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'Bearer ' + self.token}
|
|
|
|
'Authorization': 'Bearer ' + self.token}
|
|
|
|
res = self.retry(
|
|
|
|
res = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.post(self.server_url + 'download',
|
|
|
|
lambda: self.session.post(self.server_url + 'download',
|
|
|
|
json={'file_id': subtitle.file_id, 'sub_format': 'srt'},
|
|
|
|
json={'file_id': subtitle.file_id, 'sub_format': 'srt'},
|
|
|
|
headers=headers,
|
|
|
|
headers=headers,
|
|
|
@ -438,7 +427,7 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
subtitle.download_link = download_data['link']
|
|
|
|
subtitle.download_link = download_data['link']
|
|
|
|
|
|
|
|
|
|
|
|
r = self.retry(
|
|
|
|
r = self.retry(
|
|
|
|
lambda: checked(
|
|
|
|
lambda: self.checked(
|
|
|
|
lambda: self.session.get(subtitle.download_link, timeout=30),
|
|
|
|
lambda: self.session.get(subtitle.download_link, timeout=30),
|
|
|
|
validate_content=True
|
|
|
|
validate_content=True
|
|
|
|
),
|
|
|
|
),
|
|
|
@ -453,103 +442,107 @@ class OpenSubtitlesComProvider(ProviderRetryMixin, Provider):
|
|
|
|
subtitle_content = r.content
|
|
|
|
subtitle_content = r.content
|
|
|
|
subtitle.content = fix_line_ending(subtitle_content)
|
|
|
|
subtitle.content = fix_line_ending(subtitle_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_token(self):
|
|
|
|
|
|
|
|
logging.debug('Authentication failed: clearing cache and attempting to login.')
|
|
|
|
|
|
|
|
region.delete("oscom_token")
|
|
|
|
|
|
|
|
return self.login()
|
|
|
|
|
|
|
|
|
|
|
|
def checked(fn, raise_api_limit=False, validate_token=False, validate_json=False, json_key_name=None,
|
|
|
|
def checked(self, fn, raise_api_limit=False, validate_json=False, json_key_name=None, validate_content=False):
|
|
|
|
validate_content=False):
|
|
|
|
"""Run :fn: and check the response status before returning it.
|
|
|
|
"""Run :fn: and check the response status before returning it.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
:param fn: the function to make an API call to OpenSubtitles.com.
|
|
|
|
: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 raise_api_limit: if True we wait a little bit longer before running the call again.
|
|
|
|
:param validate_token: test if token is valid and return 401 if not.
|
|
|
|
:param validate_json: test if response is valid json.
|
|
|
|
:param validate_json: test if response is valid json.
|
|
|
|
:param json_key_name: test if returned json contain a specific key.
|
|
|
|
: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 validate_content: test if response have a content (used with download).
|
|
|
|
:return: the response.
|
|
|
|
:return: the response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
response = None
|
|
|
|
response = None
|
|
|
|
try:
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = fn()
|
|
|
|
try:
|
|
|
|
except APIThrottled:
|
|
|
|
response = fn()
|
|
|
|
if not raise_api_limit:
|
|
|
|
except APIThrottled:
|
|
|
|
logger.info("API request limit hit, waiting and trying again once.")
|
|
|
|
if not raise_api_limit:
|
|
|
|
time.sleep(2)
|
|
|
|
logger.info("API request limit hit, waiting and trying again once.")
|
|
|
|
return checked(fn, raise_api_limit=True)
|
|
|
|
time.sleep(2)
|
|
|
|
raise
|
|
|
|
return self.checked(fn, raise_api_limit=True)
|
|
|
|
except (ConnectionError, Timeout, ReadTimeout):
|
|
|
|
raise
|
|
|
|
raise ServiceUnavailable(f'Unknown Error, empty response: {response.status_code}: {response}')
|
|
|
|
except (ConnectionError, Timeout, ReadTimeout):
|
|
|
|
|
|
|
|
raise ServiceUnavailable(f'Unknown Error, empty response: {response.status_code}: {response}')
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
logging.exception('Unhandled exception raised.')
|
|
|
|
|
|
|
|
raise ProviderError('Unhandled exception raised. Check log.')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
status_code = response.status_code
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|
logging.exception('Unhandled exception raised.')
|
|
|
|
status_code = None
|
|
|
|
raise ProviderError('Unhandled exception raised. Check log.')
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
status_code = response.status_code
|
|
|
|
if status_code == 400:
|
|
|
|
except Exception:
|
|
|
|
raise ConfigurationError('Do not use email but username')
|
|
|
|
status_code = None
|
|
|
|
elif status_code == 401:
|
|
|
|
else:
|
|
|
|
time.sleep(1)
|
|
|
|
if status_code == 400:
|
|
|
|
|
|
|
|
raise ConfigurationError('Do not use email but username')
|
|
|
|
|
|
|
|
elif status_code == 401:
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if validate_token:
|
|
|
|
|
|
|
|
return 401
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
log_request_response(response)
|
|
|
|
log_request_response(response)
|
|
|
|
raise AuthenticationError(f'Login failed: {response.reason}')
|
|
|
|
logged_in = self.reset_token()
|
|
|
|
elif status_code == 403:
|
|
|
|
if logged_in:
|
|
|
|
log_request_response(response)
|
|
|
|
return self.checked(fn, raise_api_limit=raise_api_limit, validate_json=validate_json,
|
|
|
|
raise ProviderError("Bazarr API key seems to be in problem")
|
|
|
|
json_key_name=json_key_name, validate_content=validate_content)
|
|
|
|
elif status_code == 406:
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
raise AuthenticationError('Login failed')
|
|
|
|
json_response = response.json()
|
|
|
|
elif status_code == 403:
|
|
|
|
download_count = json_response['requests']
|
|
|
|
|
|
|
|
remaining_download = json_response['remaining']
|
|
|
|
|
|
|
|
quota_reset_time = json_response['reset_time']
|
|
|
|
|
|
|
|
except JSONDecodeError:
|
|
|
|
|
|
|
|
raise ProviderError('Invalid JSON returned by provider')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
log_request_response(response)
|
|
|
|
log_request_response(response)
|
|
|
|
raise DownloadLimitExceeded(f"Daily download limit reached. {download_count} subtitles have been "
|
|
|
|
raise ProviderError("Bazarr API key seems to be in problem")
|
|
|
|
f"downloaded and {remaining_download} remaining subtitles can be "
|
|
|
|
elif status_code == 406:
|
|
|
|
f"downloaded. Quota will be reset in {quota_reset_time}.")
|
|
|
|
try:
|
|
|
|
elif status_code == 410:
|
|
|
|
json_response = response.json()
|
|
|
|
log_request_response(response)
|
|
|
|
download_count = json_response['requests']
|
|
|
|
raise ProviderError("Download as expired")
|
|
|
|
remaining_download = json_response['remaining']
|
|
|
|
elif status_code == 429:
|
|
|
|
quota_reset_time = json_response['reset_time']
|
|
|
|
log_request_response(response)
|
|
|
|
except JSONDecodeError:
|
|
|
|
raise TooManyRequests()
|
|
|
|
raise ProviderError('Invalid JSON returned by provider')
|
|
|
|
elif status_code == 500:
|
|
|
|
else:
|
|
|
|
logging.debug("Server side exception raised while downloading from opensubtitles.com website. They "
|
|
|
|
log_request_response(response)
|
|
|
|
"should mitigate this soon.")
|
|
|
|
raise DownloadLimitExceeded(f"Daily download limit reached. {download_count} subtitles have been "
|
|
|
|
return None
|
|
|
|
f"downloaded and {remaining_download} remaining subtitles can be "
|
|
|
|
elif status_code == 502:
|
|
|
|
f"downloaded. Quota will be reset in {quota_reset_time}.")
|
|
|
|
# this one should deal with Bad Gateway issue on their side.
|
|
|
|
elif status_code == 410:
|
|
|
|
raise APIThrottled()
|
|
|
|
log_request_response(response)
|
|
|
|
elif 500 <= status_code <= 599:
|
|
|
|
raise ProviderError("Download as expired")
|
|
|
|
raise ProviderError(response.reason)
|
|
|
|
elif status_code == 429:
|
|
|
|
|
|
|
|
log_request_response(response)
|
|
|
|
if status_code != 200:
|
|
|
|
raise TooManyRequests()
|
|
|
|
log_request_response(response)
|
|
|
|
elif status_code == 500:
|
|
|
|
raise ProviderError(f'Bad status code: {response.status_code}')
|
|
|
|
logging.debug("Server side exception raised while downloading from opensubtitles.com website. They "
|
|
|
|
|
|
|
|
"should mitigate this soon.")
|
|
|
|
if validate_json:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
elif status_code == 502:
|
|
|
|
json_test = response.json()
|
|
|
|
# this one should deal with Bad Gateway issue on their side.
|
|
|
|
except JSONDecodeError:
|
|
|
|
raise APIThrottled()
|
|
|
|
raise ProviderError('Invalid JSON returned by provider')
|
|
|
|
elif 500 <= status_code <= 599:
|
|
|
|
else:
|
|
|
|
raise ProviderError(response.reason)
|
|
|
|
if json_key_name not in json_test:
|
|
|
|
|
|
|
|
raise ProviderError(f'Invalid JSON returned by provider: no {json_key_name} key in returned json.')
|
|
|
|
if status_code != 200:
|
|
|
|
|
|
|
|
log_request_response(response)
|
|
|
|
if validate_content:
|
|
|
|
raise ProviderError(f'Bad status code: {response.status_code}')
|
|
|
|
if not hasattr(response, 'content'):
|
|
|
|
|
|
|
|
logging.error('Download link returned no content attribute.')
|
|
|
|
if validate_json:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
elif not response.content:
|
|
|
|
json_test = response.json()
|
|
|
|
logging.error(f'This download link returned empty content: {response.url}')
|
|
|
|
except JSONDecodeError:
|
|
|
|
return False
|
|
|
|
raise ProviderError('Invalid JSON returned by provider')
|
|
|
|
|
|
|
|
else:
|
|
|
|
return response
|
|
|
|
if json_key_name not in json_test:
|
|
|
|
|
|
|
|
raise ProviderError(f'Invalid JSON returned by provider: no {json_key_name} key in returned json.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if validate_content:
|
|
|
|
|
|
|
|
if not hasattr(response, 'content'):
|
|
|
|
|
|
|
|
logging.error('Download link returned no content attribute.')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
elif not response.content:
|
|
|
|
|
|
|
|
logging.error(f'This download link returned empty content: {response.url}')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log_request_response(response):
|
|
|
|
def log_request_response(response):
|
|
|
|