|
|
@ -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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.token = r.json()['token']
|
|
|
|
self.token = r.json()['token']
|
|
|
|
|
|
|
|
except (ValueError, JSONDecodeError):
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
region.set("oscom_token", self.token)
|
|
|
|
region.set("oscom_token", self.token)
|
|
|
|
return
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def sanitize_external_ids(external_id):
|
|
|
|
def sanitize_external_ids(external_id):
|
|
|
@ -223,22 +227,7 @@ 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),
|
|
|
|
|
|
|
|
validate_token=True,
|
|
|
|
|
|
|
|
validate_json=True,
|
|
|
|
|
|
|
|
json_key_name='data'
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
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),
|
|
|
|
lambda: self.session.get(self.server_url + 'features', params=parameters, timeout=30),
|
|
|
|
validate_json=True,
|
|
|
|
validate_json=True,
|
|
|
|
json_key_name='data'
|
|
|
|
json_key_name='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,14 +442,16 @@ 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).
|
|
|
@ -475,7 +466,7 @@ def checked(fn, raise_api_limit=False, validate_token=False, validate_json=False
|
|
|
|
if not raise_api_limit:
|
|
|
|
if not raise_api_limit:
|
|
|
|
logger.info("API request limit hit, waiting and trying again once.")
|
|
|
|
logger.info("API request limit hit, waiting and trying again once.")
|
|
|
|
time.sleep(2)
|
|
|
|
time.sleep(2)
|
|
|
|
return checked(fn, raise_api_limit=True)
|
|
|
|
return self.checked(fn, raise_api_limit=True)
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
except (ConnectionError, Timeout, ReadTimeout):
|
|
|
|
except (ConnectionError, Timeout, ReadTimeout):
|
|
|
|
raise ServiceUnavailable(f'Unknown Error, empty response: {response.status_code}: {response}')
|
|
|
|
raise ServiceUnavailable(f'Unknown Error, empty response: {response.status_code}: {response}')
|
|
|
@ -491,11 +482,13 @@ def checked(fn, raise_api_limit=False, validate_token=False, validate_json=False
|
|
|
|
raise ConfigurationError('Do not use email but username')
|
|
|
|
raise ConfigurationError('Do not use email but username')
|
|
|
|
elif status_code == 401:
|
|
|
|
elif status_code == 401:
|
|
|
|
time.sleep(1)
|
|
|
|
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()
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
raise AuthenticationError('Login failed')
|
|
|
|
elif status_code == 403:
|
|
|
|
elif status_code == 403:
|
|
|
|
log_request_response(response)
|
|
|
|
log_request_response(response)
|
|
|
|
raise ProviderError("Bazarr API key seems to be in problem")
|
|
|
|
raise ProviderError("Bazarr API key seems to be in problem")
|
|
|
|