sok.file_operations.mixins.parsing
Parsing mixin for file operations.
Provides utilities to extract information from filenames:
- Year extraction
- Quality metadata extraction (codec, resolution, source...)
- Title and filename cleaning
FileParsingMixin
class FileParsingMixinMixin providing filename parsing utilities.
Used by all specific operations classes to avoid parsing code duplication.
Sourceclean_filenamestaticmethod
def clean_filename(filename: str, replace_char: str = '_') -> str:Clean a filename by removing invalid characters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
filename | str | Filename to clean. | |
replace_char | str | '_' | Replacement character. |
Returns
strCleaned filename.
clean_titlestaticmethod
def clean_title( title: str, replace_dots: bool = True, replace_underscores: bool = True,) -> str:Clean a title by removing common formatting characters.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
title | str | Title to clean. | |
replace_dots | bool | True | Replace dots with spaces. |
replace_underscores | bool | True | Replace underscores with spaces. |
Returns
strCleaned title.
extract_number_from_textstaticmethod
def extract_number_from_text(text: str, pattern: str = '\\d+') -> Optional[int]:Extract the first number from text.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
text | str | Text to analyze. | |
pattern | str | '\\d+' | Regex pattern to find the number. |
Returns
Optional[int]Found number or None.
extract_quality_metadatastaticmethod
def extract_quality_metadata(text: str) -> Dict[str, Optional[str]]:Extract quality metadata from text (filename).
Parameters
| Name | Type | Description |
|---|---|---|
text | str | Text to analyze. |
Returns
Dict[str, Optional[str]]Extracted metadata (quality, codec, audio, source, language).
extract_yearstaticmethod
def extract_year(text: str) -> Optional[int]:Extract a year from text.
A year in parentheses wins; otherwise the last one, since a title can hold a number that looks like a year: "Blade.Runner.2049.2017" is 2017, "1917.2019" is 2019. A year after next year is not one.
Parameters
| Name | Type | Description |
|---|---|---|
text | str | Text potentially containing a year. |
Returns
Optional[int]Extracted year or None.
normalize_path_for_comparisonstaticmethod
def normalize_path_for_comparison(path: str) -> str:Normalize a path for comparison (lowercase, unified separators).
Parameters
| Name | Type | Description |
|---|---|---|
path | str | Path to normalize. |
Returns
strNormalized path.
parse_track_disc_numberstaticmethod
def parse_track_disc_number(value: str) -> int:Parse a track/disc number that may be in '1/12' format.
Parameters
| Name | Type | Description |
|---|---|---|
value | str | Value to parse (e.g., '5' or '5/12'). |
Returns
intExtracted number.
Attributes
| Name | Type | Value |
|---|---|---|
AUDIO_CODECS | List[str] | ['AAC', 'AC3', 'DTS', 'TrueHD', 'Atmos', 'DD5.1', 'DD+', 'EAC3', 'FLAC', 'MP3'] |
CONTENT_SOURCES | List[str] | ['BluRay', 'BRRip', 'WEB-DL', 'WEBRip', 'HDTV', 'DVDRip', 'DVD', 'REMUX'] |
INVALID_FILENAME_CHARS | str | '<>:"/\\|?*' |
LANGUAGE_PATTERNS | Dict[str, str] | {'FRENCH': 'fr', 'VOSTFR': 'fr-sub', 'MULTI': 'multi', 'TRUEFRENCH': 'fr', 'VFF': 'fr', 'VFQ': 'fr-qc', 'ENGLISH': 'en', 'GERMAN': 'de', 'SPANISH': 'es'} |
VIDEO_CODECS | List[str] | ['x264', 'x265', 'H264', 'H265', 'HEVC', 'XviD', 'DivX', 'VP9', 'AV1'] |
VIDEO_QUALITY_PATTERNS | Dict[str, str] | {'2160p': '4K', '1080p': '1080p', '720p': '720p', '480p': '480p', '4K': '4K', 'UHD': '4K', 'HD': 'HD'} |