"""Video title normalization — clean the noisy YouTube titles for display + storage. Applied where a video's title is written (enrichment) and where a download's title comes from yt-dlp, so the feed, search, channel pages, and the download center all show tidy titles. The raw title is preserved in `Video.original_title` so this is reversible / re-derivable. Rules (user-approved "option b"): 0. Strip a leading social engagement prefix ("1.6M views · 66K reactions | …") some sources (Facebook) prepend, up to and including the pipe. 1. Drop emoji / pictographs / symbols / control chars (keep accents — HU/DE/…). 2. Strip trailing SEO hashtag clusters (word hashtags at the end); a numeric "#3" episode marker survives. 3. De-shout ALL-CAPS words, context-aware: - If the title is *mostly shouting* (≥ half the multi-letter words are all-caps), every all-caps word is Title-cased (short shouted words like CAR/WAR/ÉN get fixed too), and the first letter is capitalized. Known acronyms (PS, AI, USA, PC…) and words with digits (PS5, 3D) are kept; function words (to/the/és/az…) are lowercased. - Otherwise only long all-caps words (≥4 letters) are Title-cased, so a lone acronym in an otherwise normal title is left alone. 4. Collapse repeated punctuation (!!! → !) and whitespace. """ import re import unicodedata _DROP_CATEGORIES = {"So", "Sk", "Cc", "Cf", "Cs", "Co", "Cn"} # Social engagement prefix some sources (Facebook) prepend, e.g. "1.6M views · 66K reactions | ". # Match one-or-more "