mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 09:56:17 -05:00
Subtitle language-region suffixes are checked against the codes, not a 336,000-entry set (#947)
Every API process built a set of every language-region and language-auto .srt/.vtt suffix at import: about 1 s and 73 MiB per process on a Pi 4. A suffix is now split into its language and region, and each is checked against its own code set. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ba9b47edf3
commit
20439437f8
2 changed files with 29 additions and 10 deletions
|
|
@ -566,17 +566,22 @@ SUFFIXES = {
|
|||
}
|
||||
SUFFIXES |= {f'.{i}.srt' for i in ISO_639_CODES}
|
||||
SUFFIXES |= {f'.{i}.vtt' for i in ISO_639_CODES}
|
||||
EXTRA_SUFFIXES = set()
|
||||
for six_ in ISO_639_CODES.keys():
|
||||
EXTRA_SUFFIXES |= {f'.{six_}-{i}.srt' for i in ISO_3166_CODES}
|
||||
EXTRA_SUFFIXES |= {f'.{six_}-{i}.vtt' for i in ISO_3166_CODES}
|
||||
# Add auto suffixes once (these don't depend on the loop variable)
|
||||
EXTRA_SUFFIXES |= {f'.{i}-auto.srt' for i in ISO_639_CODES}
|
||||
EXTRA_SUFFIXES |= {f'.{i}-auto.vtt' for i in ISO_639_CODES}
|
||||
|
||||
# Convert to lowercase frozensets for O(1) case-insensitive lookup
|
||||
_SUFFIXES_LOWER = frozenset(s.lower() for s in SUFFIXES)
|
||||
_EXTRA_SUFFIXES_LOWER = frozenset(s.lower() for s in EXTRA_SUFFIXES)
|
||||
_LANGUAGES_LOWER = frozenset(i.lower() for i in ISO_639_CODES)
|
||||
_REGIONS_LOWER = frozenset(i.lower() for i in ISO_3166_CODES)
|
||||
|
||||
|
||||
def _is_language_region_suffix(suffix_lower: str) -> bool:
|
||||
"""Is this lowercase .srt/.vtt suffix a language with a region or "auto", like ".en-us.srt" or ".en-auto.vtt"?
|
||||
|
||||
Never build the set of every combination: 336,000 strings cost ~2s and ~65MiB in each process on a Pi 4. A
|
||||
region code never contains "-"; a language code may ("en-gb")."""
|
||||
language, _, region = suffix_lower[1:-4].rpartition('-')
|
||||
return language in _LANGUAGES_LOWER and (region == 'auto' or region in _REGIONS_LOWER)
|
||||
|
||||
|
||||
_HARDCODED_SUFFIXES = frozenset(
|
||||
{'.info.json', '.ffprobe.json', '.live_chat.json', '.readability.html', '.readability.json', '.readability.txt'})
|
||||
|
||||
|
|
@ -630,11 +635,11 @@ def _get_suffix_length(name_lower: str) -> int:
|
|||
if suffix in _SUFFIXES_LOWER:
|
||||
return len(suffix)
|
||||
|
||||
# Only check EXTRA_SUFFIXES for .srt/.vtt files
|
||||
# Only .srt/.vtt files have language-region suffixes
|
||||
simple_suffix = candidates[0] if candidates else ''
|
||||
if simple_suffix in {'.srt', '.vtt'}:
|
||||
for suffix in reversed(candidates):
|
||||
if suffix in _EXTRA_SUFFIXES_LOWER:
|
||||
if _is_language_region_suffix(suffix):
|
||||
return len(suffix)
|
||||
|
||||
return len(simple_suffix)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ from sqlalchemy.exc import IntegrityError
|
|||
from wrolpi.errors import InvalidFile, UnknownDirectory, FileGroupIsTagged, NoPrimaryFile
|
||||
from wrolpi.files import lib, indexers
|
||||
from wrolpi.files.models import FileGroup, Directory
|
||||
from wrolpi.lang import ISO_639_CODES, ISO_3166_CODES
|
||||
from wrolpi.tags import TagFile
|
||||
from wrolpi.test.common import only_macos, skip_ci
|
||||
from wrolpi.vars import PROJECT_DIR, IS_MACOS
|
||||
|
|
@ -256,6 +257,19 @@ def test_split_path_stem_and_suffix(path, full, expected):
|
|||
assert lib.split_path_stem_and_suffix(Path(path), full) == expected
|
||||
|
||||
|
||||
def test_is_language_region_suffix():
|
||||
"""Every language-region and language-auto subtitle suffix is recognized, without listing them all."""
|
||||
for language in ISO_639_CODES:
|
||||
for ext in ('.srt', '.vtt'):
|
||||
assert lib._is_language_region_suffix(f'.{language}-auto{ext}'.lower())
|
||||
for region in ISO_3166_CODES:
|
||||
assert lib._is_language_region_suffix(f'.{language}-{region}{ext}'.lower())
|
||||
|
||||
for suffix in ('.srt', '.en.srt', '.en-.srt', '.-us.srt', '.en-usa.srt', '.zz-us.srt', '.en-zz.vtt',
|
||||
'.foo.en-us.srt', '.en.us.srt', '.en_us.srt'):
|
||||
assert not lib._is_language_region_suffix(suffix), suffix
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_refresh_files(async_client, test_session, make_files_structure, assert_file_groups, refresh_files):
|
||||
"""All files in the media directory should be found when calling `refresh_files`"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue