Skip to content

Commit

Permalink
Added some failsafe to RegieLive provider to try to prevent getting r…
Browse files Browse the repository at this point in the history
…edirected to captcha validation or being completely blocked for a while. #2165
  • Loading branch information
morpheus65535 committed Jan 16, 2025
1 parent dbcbf4e commit e17bad6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
7 changes: 6 additions & 1 deletion bazarr/app/get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from requests import ConnectionError
from subzero.language import Language
from subliminal_patch.exceptions import TooManyRequests, APIThrottled, ParseResponseError, IPAddressBlocked, \
MustGetBlacklisted, SearchLimitReached
MustGetBlacklisted, SearchLimitReached, ProviderError
from subliminal.providers.opensubtitles import DownloadLimitReached, PaymentRequired, Unauthorized
from subliminal.exceptions import DownloadLimitExceeded, ServiceUnavailable, AuthenticationError, ConfigurationError
from subliminal import region as subliminal_cache_region
Expand Down Expand Up @@ -123,6 +123,11 @@ def provider_throttle_map():
"whisperai": {
ConnectionError: (datetime.timedelta(hours=24), "24 hours"),
},
"regielive": {
APIThrottled: (datetime.timedelta(hours=1), "1 hour"),
TooManyRequests: (datetime.timedelta(minutes=5), "5 minutes"),
ProviderError: (datetime.timedelta(minutes=10), "10 minutes"),
},
}


Expand Down
51 changes: 43 additions & 8 deletions custom_libs/subliminal_patch/providers/regielive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import io
import os

from requests import Session
from requests import Session, JSONDecodeError
from guessit import guessit
from subliminal_patch.exceptions import TooManyRequests, APIThrottled, ProviderError
from subliminal_patch.providers import Provider
from subliminal_patch.subtitle import Subtitle, guess_matches
from subliminal.subtitle import SUBTITLE_EXTENSIONS, fix_line_ending
Expand Down Expand Up @@ -87,13 +88,18 @@ def query(self, video, language):
payload['nume'] = video.title
payload['an'] = video.year

response = self.session.get(
self.url + "?" + urllib.parse.urlencode(payload),
data=payload, headers=self.headers)
response = self.checked(
lambda: self.session.get(
self.url + "?" + urllib.parse.urlencode(payload),
data=payload, headers=self.headers)
)

subtitles = []
if response.status_code == 200:
results = response.json()
try:
results = response.json()
except JSONDecodeError:
raise ProviderError('Unable to parse JSON response')
if len(results) > 0:
results_subs = results['rezultate']
for film in results_subs:
Expand Down Expand Up @@ -122,9 +128,13 @@ def download_subtitle(self, subtitle):
'Cache-Control': 'no-cache'
}
session.headers.update(_addheaders)
res = session.get('https://subtitrari.regielive.ro')
res = self.checked(
lambda: session.get('https://subtitrari.regielive.ro')
)
cookies = res.cookies
_zipped = session.get(subtitle.page_link, cookies=cookies)
_zipped = self.checked(
lambda: session.get(subtitle.page_link, cookies=cookies, allow_redirects=False)
)
if _zipped:
if _zipped.text == '500':
raise ValueError('Error 500 on server')
Expand All @@ -135,7 +145,8 @@ def download_subtitle(self, subtitle):
return subtitle
raise ValueError('Problems conecting to the server')

def _get_subtitle_from_archive(self, archive):
@staticmethod
def _get_subtitle_from_archive(archive):
# some files have a non subtitle with .txt extension
_tmp = list(SUBTITLE_EXTENSIONS)
_tmp.remove('.txt')
Expand All @@ -153,3 +164,27 @@ def _get_subtitle_from_archive(self, archive):
return archive.read(name)

raise APIThrottled('Can not find the subtitle in the compressed file')

@staticmethod
def checked(fn):
"""Run :fn: and check the response status before returning it.
:param fn: the function to make an API call to provider.
:return: the response.
"""
response = None
try:
response = fn()
except Exception:
logger.exception('Unhandled exception raised.')
raise ProviderError('Unhandled exception raised. Check log.')
else:
status_code = response.status_code

if status_code == 301:
raise APIThrottled()
elif status_code == 429:
raise TooManyRequests()

return response

0 comments on commit e17bad6

Please sign in to comment.