Skip to content

Commit

Permalink
Added the option to skip mismatched fps to Titulky
Browse files Browse the repository at this point in the history
  • Loading branch information
sambartik authored Oct 8, 2024
1 parent a4873fc commit f296ba5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions bazarr/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def check_parser_binary(value):
Validator('titulky.username', must_exist=True, default='', is_type_of=str, cast=str),
Validator('titulky.password', must_exist=True, default='', is_type_of=str, cast=str),
Validator('titulky.approved_only', must_exist=True, default=False, is_type_of=bool),
Validator('titulky.skip_wrong_fps', must_exist=True, default=False, is_type_of=bool),

# embeddedsubtitles section
Validator('embeddedsubtitles.included_codecs', must_exist=True, default=[], is_type_of=list),
Expand Down
1 change: 1 addition & 0 deletions bazarr/app/get_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def get_providers_auth():
'username': settings.titulky.username,
'password': settings.titulky.password,
'approved_only': settings.titulky.approved_only,
'skip_wrong_fps': settings.titulky.skip_wrong_fps,
},
'titlovi': {
'username': settings.titlovi.username,
Expand Down
66 changes: 62 additions & 4 deletions custom_libs/subliminal_patch/providers/titulky.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

from subliminal_patch.subtitle import Subtitle, guess_matches

from subliminal_patch.score import framerate_equal

from dogpile.cache.api import NO_VALUE
from subzero.language import Language

Expand Down Expand Up @@ -53,6 +55,8 @@ def __init__(self,
approved,
page_link,
download_link,
fps,
skip_wrong_fps,
asked_for_episode=None):
super().__init__(language, page_link=page_link)

Expand All @@ -67,6 +71,8 @@ def __init__(self,
self.page_link = page_link
self.uploader = uploader
self.download_link = download_link
self.fps = fps if skip_wrong_fps else None # This attribute should be ignored if skip_wrong_fps is false
self.skip_wrong_fps = skip_wrong_fps
self.asked_for_episode = asked_for_episode
self.matches = None

Expand All @@ -78,6 +84,10 @@ def get_matches(self, video):
matches = set()
media_type = 'movie' if isinstance(video, Movie) else 'episode'

if self.skip_wrong_fps and video.fps and self.fps and not framerate_equal(video.fps, self.fps):
logger.debug(f"Titulky.com: Wrong FPS (expected: {video.fps}, got: {self.fps}, lowering score massively)")
return set()

if media_type == 'episode':
# match imdb_id of a series
if video.series_imdb_id and video.series_imdb_id == self.imdb_id:
Expand Down Expand Up @@ -120,16 +130,19 @@ class TitulkyProvider(Provider, ProviderSubtitleArchiveMixin):
def __init__(self,
username=None,
password=None,
approved_only=None):
approved_only=None,
skip_wrong_fps=None):
if not all([username, password]):
raise ConfigurationError("Username and password must be specified!")

if type(approved_only) is not bool:
raise ConfigurationError(f"Approved_only {approved_only} must be a boolean!")
if type(skip_wrong_fps) is not bool:
raise ConfigurationError(f"Skip_wrong_fps {skip_wrong_fps} must be a boolean!")

self.username = username
self.password = password
self.approved_only = approved_only
self.skip_wrong_fps = skip_wrong_fps

self.session = None

Expand Down Expand Up @@ -268,6 +281,48 @@ def build_url(self, params):

return result

# Retrieves the fps value given subtitles id from the details page and caches it.
def retrieve_subtitles_fps(self, subtitles_id):
cache_key = f"titulky_subs-{subtitles_id}_fps"
cached_fps_value = cache.get(cache_key)

if(cached_fps_value != NO_VALUE):
logger.debug(f"Titulky.com: Reusing cached fps value {cached_fps_value} for subtitles with id {subtitles_id}")
return cached_fps_value

params = {
'action': 'detail',
'id': subtitles_id
}
browse_url = self.build_url(params)
html_src = self.fetch_page(browse_url, allow_redirects=True)
browse_page_soup = ParserBeautifulSoup(html_src, ['lxml', 'html.parser'])

fps_container = browse_page_soup.select_one("div.ulozil:has(> img[src='img/ico/Movieroll.png'])")
if(fps_container is None):
logger.debug("Titulky.com: Could not manage to find the FPS container in the details page")
cache.set(cache_key, None)
return None

fps_text_components = fps_container.get_text(strip=True).split()
# Check if the container contains valid fps data
if(len(fps_text_components) < 2 or fps_text_components[1].lower() != "fps"):
logger.debug(f"Titulky.com: Could not determine FPS value for subtitles with id {subtitles_id}")
cache.set(cache_key, None)
return None

fps_text = fps_text_components[0].replace(",", ".") # Fix decimal comma to decimal point
try:
fps = float(fps_text)
logger.debug(f"Titulky.com: Retrieved FPS value {fps} from details page for subtitles with id {subtitles_id}")
cache.set(cache_key, fps)
return fps
except:
logger.debug(f"Titulky.com: There was an error parsing FPS value string for subtitles with id {subtitles_id}")
cache.set(cache_key, None)
return None


"""
There are multiple ways to find substitles on Titulky.com, however we are
going to utilize a page that lists all available subtitles for all episodes in a season
Expand Down Expand Up @@ -377,7 +432,8 @@ def query(self, languages,
'language': sub_language,
'uploader': uploader,
'details_link': details_link,
'download_link': download_link
'download_link': download_link,
'fps': self.retrieve_subtitles_fps(sub_id) if skip_wrong_fps else None,
}

# If this row contains the first subtitles to an episode number,
Expand Down Expand Up @@ -413,7 +469,9 @@ def query(self, languages,
sub_info['approved'],
sub_info['details_link'],
sub_info['download_link'],
asked_for_episode=(media_type is SubtitlesType.EPISODE)
sub_info['fps'],
self.skip_wrong_fps,
asked_for_episode=(media_type is SubtitlesType.EPISODE),
)
subtitles.append(subtitle_instance)

Expand Down
5 changes: 5 additions & 0 deletions frontend/src/pages/Settings/Providers/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ export const ProviderList: Readonly<ProviderInfo[]> = [
key: "approved_only",
name: "Skip unapproved subtitles",
},
{
type: "switch",
key: "skip_wrong_fps",
name: "Skip subtitles with mismatched fps to video's",
},
],
},
{
Expand Down

0 comments on commit f296ba5

Please sign in to comment.