diff --git a/libs/subliminal_patch/converters/titlovi.py b/libs/subliminal_patch/converters/titlovi.py index 940507d4f..761cf79a6 100644 --- a/libs/subliminal_patch/converters/titlovi.py +++ b/libs/subliminal_patch/converters/titlovi.py @@ -27,16 +27,6 @@ class TitloviConverter(LanguageReverseConverter): } self.codes = set(self.from_titlovi.keys()) - # temporary fix, should be removed as soon as API is used - self.lang_from_countrycode = {'ba': ('bos',), - 'en': ('eng',), - 'hr': ('hrv',), - 'mk': ('mkd',), - 'rs': ('srp',), - 'rsc': ('srp', None, 'Cyrl'), - 'si': ('slv',) - } - def convert(self, alpha3, country=None, script=None): if (alpha3, country, script) in self.to_titlovi: return self.to_titlovi[(alpha3, country, script)] @@ -49,9 +39,5 @@ class TitloviConverter(LanguageReverseConverter): if titlovi in self.from_titlovi: return self.from_titlovi[titlovi] - # temporary fix, should be removed as soon as API is used - if titlovi in self.lang_from_countrycode: - return self.lang_from_countrycode[titlovi] - raise ConfigurationError('Unsupported language number for titlovi: %s' % titlovi) diff --git a/libs/subliminal_patch/core.py b/libs/subliminal_patch/core.py index bb9b3752b..9e96754dd 100644 --- a/libs/subliminal_patch/core.py +++ b/libs/subliminal_patch/core.py @@ -30,7 +30,7 @@ from subliminal.core import guessit, ProviderPool, io, is_windows_special_path, ThreadPoolExecutor, check_video from subliminal_patch.exceptions import TooManyRequests, APIThrottled, ParseResponseError -from subzero.language import Language +from subzero.language import Language, ENDSWITH_LANGUAGECODE_RE from scandir import scandir, scandir_generic as _scandir_generic logger = logging.getLogger(__name__) @@ -571,12 +571,14 @@ def scan_video(path, dont_use_actual_file=False, hints=None, providers=None, ski return video -def _search_external_subtitles(path, languages=None, only_one=False, scandir_generic=False): +def _search_external_subtitles(path, languages=None, only_one=False, scandir_generic=False, match_strictness="strict"): dirpath, filename = os.path.split(path) dirpath = dirpath or '.' - fileroot, fileext = os.path.splitext(filename) + fn_no_ext, fileext = os.path.splitext(filename) + fn_no_ext_lower = fn_no_ext.lower() subtitles = {} _scandir = _scandir_generic if scandir_generic else scandir + for entry in _scandir(dirpath): if (not entry.name or entry.name in ('\x0c', '$', ',', '\x7f')) and not scandir_generic: logger.debug('Could not determine the name of the file, retrying with scandir_generic') @@ -587,9 +589,11 @@ def _search_external_subtitles(path, languages=None, only_one=False, scandir_gen p = entry.name # keep only valid subtitle filenames - if not p.lower().startswith(fileroot.lower()) or not p.lower().endswith(SUBTITLE_EXTENSIONS): + if not p.lower().endswith(SUBTITLE_EXTENSIONS): continue + # not p.lower().startswith(fileroot.lower()) or not + p_root, p_ext = os.path.splitext(p) if not INCLUDE_EXOTIC_SUBS and p_ext not in (".srt", ".ass", ".ssa", ".vtt"): continue @@ -608,7 +612,19 @@ def _search_external_subtitles(path, languages=None, only_one=False, scandir_gen forced = "forced" in adv_tag # extract the potential language code - language_code = p_root[len(fileroot):].replace('_', '-')[1:] + language_code = p_root.rsplit(".", 1)[1].replace('_', '-') + + # remove possible language code for matching + p_root_bare = ENDSWITH_LANGUAGECODE_RE.sub("", p_root) + + p_root_lower = p_root_bare.lower() + + filename_matches = p_root_lower == fn_no_ext_lower + filename_contains = p_root_lower in fn_no_ext_lower + + if not filename_matches: + if match_strictness == "strict" or (match_strictness == "loose" and not filename_contains): + continue # default language is undefined language = Language('und') @@ -632,7 +648,7 @@ def _search_external_subtitles(path, languages=None, only_one=False, scandir_gen return subtitles -def search_external_subtitles(path, languages=None, only_one=False): +def search_external_subtitles(path, languages=None, only_one=False, match_strictness="strict"): """ wrap original search_external_subtitles function to search multiple paths for one given video # todo: cleanup and merge with _search_external_subtitles @@ -653,10 +669,11 @@ def search_external_subtitles(path, languages=None, only_one=False): if os.path.isdir(os.path.dirname(abspath)): try: subtitles.update(_search_external_subtitles(abspath, languages=languages, - only_one=only_one)) + only_one=only_one, match_strictness=match_strictness)) except OSError: subtitles.update(_search_external_subtitles(abspath, languages=languages, - only_one=only_one, scandir_generic=True)) + only_one=only_one, match_strictness=match_strictness, + scandir_generic=True)) logger.debug("external subs: found %s", subtitles) return subtitles diff --git a/libs/subliminal_patch/providers/titlovi.py b/libs/subliminal_patch/providers/titlovi.py index c0a1ffa11..9be0a92f6 100644 --- a/libs/subliminal_patch/providers/titlovi.py +++ b/libs/subliminal_patch/providers/titlovi.py @@ -2,42 +2,35 @@ import io import logging -import math import re -import time +from datetime import datetime +import dateutil.parser import rarfile -from bs4 import BeautifulSoup from zipfile import ZipFile, is_zipfile from rarfile import RarFile, is_rarfile from babelfish import language_converters, Script -from requests import RequestException +from requests import RequestException, codes as request_codes from guessit import guessit from subliminal_patch.http import RetryingCFSession from subliminal_patch.providers import Provider from subliminal_patch.providers.mixins import ProviderSubtitleArchiveMixin from subliminal_patch.subtitle import Subtitle from subliminal_patch.utils import sanitize, fix_inconsistent_naming as _fix_inconsistent_naming -from subliminal.exceptions import ProviderError +from subliminal.exceptions import ProviderError, AuthenticationError, ConfigurationError from subliminal.score import get_equivalent_release_groups from subliminal.utils import sanitize_release_group from subliminal.subtitle import guess_matches from subliminal.video import Episode, Movie from subliminal.subtitle import fix_line_ending -from subliminal_patch.pitcher import pitchers, load_verification, store_verification -from subzero.language import Language -from random import randint -from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST +from subzero.language import Language +from dogpile.cache.api import NO_VALUE +from subliminal.cache import region # parsing regex definitions title_re = re.compile(r'(?P