Fix for #1104. Searching for one language after the other when more then one language is requested. Previously, PT was being prioritized over others.

pull/1173/head
Louis Vézina 4 years ago
parent 99318bbde1
commit 7a2e6ab24f

@ -290,6 +290,8 @@ class LegendasdivxProvider(Provider):
_searchurl = self.searchurl _searchurl = self.searchurl
subtitles = []
if isinstance(video, Movie): if isinstance(video, Movie):
querytext = video.imdb_id if video.imdb_id else video.title querytext = video.imdb_id if video.imdb_id else video.title
@ -298,79 +300,83 @@ class LegendasdivxProvider(Provider):
querytext = quote(quote(querytext)) querytext = quote(quote(querytext))
# language query filter # language query filter
if isinstance(languages, (tuple, list, set)): if not isinstance(languages, (tuple, list, set)):
language_ids = ','.join(sorted(l.opensubtitles for l in languages)) languages = [languages]
if 'por' in language_ids: # prioritize portuguese subtitles
for language in languages:
logger.debug("Legendasdivx.pt :: searching for %s subtitles.", language)
language_id = language.opensubtitles
if 'por' in language_id:
lang_filter = '&form_cat=28' lang_filter = '&form_cat=28'
elif 'pob' in language_ids: elif 'pob' in language_id:
lang_filter = '&form_cat=29' lang_filter = '&form_cat=29'
else: else:
lang_filter = '' lang_filter = ''
querytext = querytext + lang_filter if lang_filter else querytext querytext = querytext + lang_filter if lang_filter else querytext
try: try:
# sleep for a 1 second before another request # sleep for a 1 second before another request
sleep(1) sleep(1)
self.headers['Referer'] = self.site + '/index.php' self.headers['Referer'] = self.site + '/index.php'
self.session.headers.update(self.headers) self.session.headers.update(self.headers)
res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False) res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False)
res.raise_for_status()
if (res.status_code == 200 and "A legenda não foi encontrada" in res.text):
logger.warning('Legendasdivx.pt :: query %s return no results!', querytext)
# for series, if no results found, try again just with series and season (subtitle packs)
if isinstance(video, Episode):
logger.debug("Legendasdivx.pt :: trying again with just series and season on query.")
querytext = re.sub("(e|E)(\d{2})", "", querytext)
res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False)
res.raise_for_status()
if (res.status_code == 200 and "A legenda não foi encontrada" in res.text):
logger.warning('Legendasdivx.pt :: query %s return no results (for series and season only).', querytext)
return []
if res.status_code == 302: # got redirected to login page.
# seems that our session cookies are no longer valid... clean them from cache
region.delete("legendasdivx_cookies2")
logger.debug("Legendasdivx.pt :: Logging in again. Cookies have expired!")
# login and try again
self.login()
res = self.session.get(_searchurl.format(query=querytext))
res.raise_for_status() res.raise_for_status()
except HTTPError as e: if (res.status_code == 200 and "A legenda não foi encontrada" in res.text):
if "bloqueado" in res.text.lower(): logger.warning('Legendasdivx.pt :: query %s return no results!', querytext)
logger.error("LegendasDivx.pt :: Your IP is blocked on this server.") # for series, if no results found, try again just with series and season (subtitle packs)
raise IPAddressBlocked("LegendasDivx.pt :: Your IP is blocked on this server.") if isinstance(video, Episode):
logger.error("Legendasdivx.pt :: HTTP Error %s", e) logger.debug("Legendasdivx.pt :: trying again with just series and season on query.")
raise TooManyRequests("Legendasdivx.pt :: HTTP Error %s", e) querytext = re.sub("(e|E)(\d{2})", "", querytext)
except Exception as e: res = self.session.get(_searchurl.format(query=querytext), allow_redirects=False)
logger.error("LegendasDivx.pt :: Uncaught error: %r", e) res.raise_for_status()
raise ServiceUnavailable("LegendasDivx.pt :: Uncaught error: %r", e) if (res.status_code == 200 and "A legenda não foi encontrada" in res.text):
logger.warning('Legendasdivx.pt :: query %s return no results (for series and season only).', querytext)
bsoup = ParserBeautifulSoup(res.content, ['html.parser']) return []
if res.status_code == 302: # got redirected to login page.
# search for more than 10 results (legendasdivx uses pagination) # seems that our session cookies are no longer valid... clean them from cache
# don't throttle - maximum results = 6 * 10 region.delete("legendasdivx_cookies2")
MAX_PAGES = 6 logger.debug("Legendasdivx.pt :: Logging in again. Cookies have expired!")
# login and try again
# get number of pages bases on results found self.login()
page_header = bsoup.find("div", {"class": "pager_bar"}) res = self.session.get(_searchurl.format(query=querytext))
results_found = re.search(r'\((.*?) encontradas\)', page_header.text).group(1) if page_header else 0 res.raise_for_status()
logger.debug("Legendasdivx.pt :: Found %s subtitles", str(results_found)) except HTTPError as e:
num_pages = (int(results_found) // 10) + 1 if "bloqueado" in res.text.lower():
num_pages = min(MAX_PAGES, num_pages) logger.error("LegendasDivx.pt :: Your IP is blocked on this server.")
raise IPAddressBlocked("LegendasDivx.pt :: Your IP is blocked on this server.")
# process first page logger.error("Legendasdivx.pt :: HTTP Error %s", e)
subtitles = self._process_page(video, bsoup) raise TooManyRequests("Legendasdivx.pt :: HTTP Error %s", e)
except Exception as e:
# more pages? logger.error("LegendasDivx.pt :: Uncaught error: %r", e)
if num_pages > 1: raise ServiceUnavailable("LegendasDivx.pt :: Uncaught error: %r", e)
for num_page in range(2, num_pages+1):
sleep(1) # another 1 sec before requesting... bsoup = ParserBeautifulSoup(res.content, ['html.parser'])
_search_next = self.searchurl.format(query=querytext) + "&page={0}".format(str(num_page))
logger.debug("Legendasdivx.pt :: Moving on to next page: %s", _search_next) # search for more than 10 results (legendasdivx uses pagination)
res = self.session.get(_search_next) # don't throttle - maximum results = 6 * 10
next_page = ParserBeautifulSoup(res.content, ['html.parser']) MAX_PAGES = 6
subs = self._process_page(video, next_page)
subtitles.extend(subs) # get number of pages bases on results found
page_header = bsoup.find("div", {"class": "pager_bar"})
results_found = re.search(r'\((.*?) encontradas\)', page_header.text).group(1) if page_header else 0
logger.debug("Legendasdivx.pt :: Found %s subtitles", str(results_found))
num_pages = (int(results_found) // 10) + 1
num_pages = min(MAX_PAGES, num_pages)
# process first page
subtitles += self._process_page(video, bsoup)
# more pages?
if num_pages > 1:
for num_page in range(2, num_pages+1):
sleep(1) # another 1 sec before requesting...
_search_next = self.searchurl.format(query=querytext) + "&page={0}".format(str(num_page))
logger.debug("Legendasdivx.pt :: Moving on to next page: %s", _search_next)
res = self.session.get(_search_next)
next_page = ParserBeautifulSoup(res.content, ['html.parser'])
subs = self._process_page(video, next_page)
subtitles.extend(subs)
return subtitles return subtitles

Loading…
Cancel
Save