From 3e57cf5bf80a15606cc4109f652585d699f36c25 Mon Sep 17 00:00:00 2001 From: "naps62-yolo (agent)" Date: Sat, 22 Aug 2026 19:49:05 +0100 Subject: [PATCH] Release name parser (#50) --- Cargo.lock | 3 + crates/arr-parse/Cargo.toml | 1 + crates/arr-parse/src/lib.rs | 482 +++++++++++++++++++++++++++++++- crates/arr-parse/src/markers.rs | 204 ++++++++++++++ crates/arr-parse/tests/parse.rs | 355 +++++++++++++++++++++++ 5 files changed, 1044 insertions(+), 1 deletion(-) create mode 100644 crates/arr-parse/src/markers.rs create mode 100644 crates/arr-parse/tests/parse.rs diff --git a/Cargo.lock b/Cargo.lock index 34fb6b5..5040cca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,9 @@ version = "0.1.0" [[package]] name = "arr-parse" version = "0.1.0" +dependencies = [ + "serde", +] [[package]] name = "arr-probe" diff --git a/crates/arr-parse/Cargo.toml b/crates/arr-parse/Cargo.toml index 959c20f..77855d2 100644 --- a/crates/arr-parse/Cargo.toml +++ b/crates/arr-parse/Cargo.toml @@ -7,6 +7,7 @@ repository.workspace = true publish = false [dependencies] +serde = { workspace = true } [lints] workspace = true diff --git a/crates/arr-parse/src/lib.rs b/crates/arr-parse/src/lib.rs index eaf4b4b..4fead85 100644 --- a/crates/arr-parse/src/lib.rs +++ b/crates/arr-parse/src/lib.rs @@ -1 +1,481 @@ -//! arr-parse — see DESIGN.md. +//! Release name parsing. +//! +//! Everything extracted here is a *claim*, not a fact (`DESIGN.md` §5.6): +//! release names lie or omit. Claims drive pre-grab filtering and scoring +//! only. Post-download truth comes from `ffprobe` and lives in different +//! types downstream — never mix the two in one struct. +//! +//! Parsing never fails: a malformed name yields a partial [`NameClaims`], +//! worst case an empty one. + +mod markers; + +use serde::{Deserialize, Serialize}; + +use crate::markers::{Marker, Strength}; + +/// Claimed video resolution. Ordered worst to best. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub enum Resolution { + /// 480p or 480i. + #[serde(rename = "480p")] + P480, + /// 576p or 576i. + #[serde(rename = "576p")] + P576, + /// 720p. + #[serde(rename = "720p")] + P720, + /// 1080p or 1080i. + #[serde(rename = "1080p")] + P1080, + /// 2160p, also claimed by `4K` and `UHD` markers. + #[serde(rename = "2160p")] + P2160, +} + +/// Claimed source tier. Ordered worst to best per `DESIGN.md` §5.5; the +/// bottom four are hard filters there, not low scores. +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub enum Source { + /// Camcorder in a cinema. + Cam, + /// Telesync. + Telesync, + /// Telecine. + Telecine, + /// Screener copy. + Screener, + /// DVD or a DVD rip. + Dvd, + /// Over-the-air capture. + Hdtv, + /// Re-encoded streaming capture. + WebRip, + /// Untouched streaming download. + WebDl, + /// `BluRay` encode. + BluRay, + /// Untouched `BluRay` streams. + Remux, +} + +/// Claimed video codec. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Codec { + /// H.264 / AVC. + X264, + /// H.265 / HEVC. + X265, + /// AV1. + Av1, + /// `XviD` / `DivX`. + Xvid, +} + +/// Claimed HDR format markers. A name may carry several (`DV HDR10`), and a +/// `DV` claim says nothing about the profile — that is `ffprobe`'s job +/// (`DESIGN.md` §5.3). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum HdrMarker { + /// `DV`, `DoVi`, `Dolby Vision`. + DolbyVision, + /// HDR10+. + Hdr10Plus, + /// HDR10. + Hdr10, + /// Generic `HDR` with no format named. + Hdr, + /// Hybrid log-gamma. + Hlg, + /// Explicitly SDR. + Sdr, +} + +/// Claimed language markers. `PtBr` and `Dual` are the first of the three +/// pt-BR detection signals in `DESIGN.md` §5.2; interpretation belongs to +/// the policy engine, this crate only reports what the name says. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum LanguageMarker { + /// `PT-BR`, `Dublado`, `Nacional`. + PtBr, + /// `PT-PT`. + PtPt, + /// `Portuguese`/`POR`/`PT` — flavour unknown. + Portuguese, + /// English. + English, + /// French. + French, + /// German. + German, + /// Spanish, including `Latino` and `Castellano`. + Spanish, + /// Italian. + Italian, + /// `MULTi` — several audio languages advertised. + Multi, + /// `DUAL` / `Dual Áudio` — two audio tracks, a common pt-BR signal. + Dual, +} + +/// Claimed edition markers. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Edition { + /// Extended cut or edition. + Extended, + /// Director's cut. + DirectorsCut, + /// Theatrical cut. + Theatrical, + /// Unrated. + Unrated, + /// Uncut. + Uncut, + /// IMAX. + Imax, + /// Remastered. + Remastered, + /// Criterion release. + Criterion, + /// Special edition. + SpecialEdition, + /// `LIMITED` theatrical run. + Limited, +} + +/// What a release name claims about itself. Every field is unverified +/// (`DESIGN.md` §5.6); absence of a marker means nothing. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(default)] +pub struct NameClaims { + /// Title words before the year / first quality marker, if any survived. + pub title: Option, + /// Claimed release year. + pub year: Option, + /// Claimed resolution. + pub resolution: Option, + /// Claimed source tier. `Remux` wins when both it and `BluRay` appear. + pub source: Option, + /// Claimed video codec. + pub codec: Option, + /// Claimed HDR markers, in order of appearance, deduplicated. + pub hdr: Vec, + /// Claimed language markers, in order of appearance, deduplicated. + pub languages: Vec, + /// Claimed editions, in order of appearance, deduplicated. + pub editions: Vec, + /// Claimed release group. + pub group: Option, +} + +#[derive(Debug, Clone)] +struct Token { + orig: String, + low: String, +} + +/// Parse a release name into [`NameClaims`]. Never panics; unrecognised +/// input degrades to partial or empty claims. +#[must_use] +pub fn parse(name: &str) -> NameClaims { + let trimmed = name.trim(); + let stripped = strip_extension(trimmed); + let (cleaned, bracket_group) = take_trailing_bracket_group(stripped); + let mut toks = tokenize(&cleaned); + strip_site_prefix(&mut toks); + + let has_strong = first_strong_index(&toks).is_some(); + let dash_group = take_dash_group(&mut toks, has_strong); + let first_strong = first_strong_index(&toks); + + let limit = first_strong.unwrap_or(toks.len()); + let year_positions: Vec = toks + .iter() + .enumerate() + .skip(1) + .filter(|(_, t)| markers::year_of(&t.low).is_some()) + .map(|(i, _)| i) + .collect(); + let year_idx = year_positions + .iter() + .rev() + .find(|&&i| i < limit) + .or_else(|| year_positions.first()) + .copied(); + let boundary = year_idx.map_or(limit, |y| y.min(limit)); + + let mut claims = NameClaims { + year: year_idx + .and_then(|i| toks.get(i)) + .and_then(|t| markers::year_of(&t.low)), + group: dash_group.or(bracket_group), + ..NameClaims::default() + }; + + let mut title_toks: Vec = toks.get(..boundary).unwrap_or_default().to_vec(); + strip_title_editions(&mut title_toks, &mut claims.editions); + if !title_toks.is_empty() { + let words: Vec<&str> = title_toks.iter().map(|t| t.orig.as_str()).collect(); + claims.title = Some(words.join(" ")); + } + + let mut sources: Vec = Vec::new(); + let mut i = boundary; + while i < toks.len() { + if Some(i) == year_idx || markers::is_season_episode(&toks[i].low) { + i += 1; + continue; + } + if let Some(next) = toks.get(i + 1) { + if Some(i + 1) != year_idx { + if let Some((m, _)) = markers::classify_pair(&toks[i].low, &next.low) { + apply_marker(&mut claims, &mut sources, m); + i += 2; + continue; + } + } + } + if let Some((m, _)) = markers::classify(&toks[i].low) { + apply_marker(&mut claims, &mut sources, m); + } + i += 1; + } + + claims.source = if sources.contains(&Source::Remux) { + Some(Source::Remux) + } else { + sources.first().copied() + }; + claims +} + +fn apply_marker(claims: &mut NameClaims, sources: &mut Vec, m: Marker) { + match m { + Marker::Resolution(r) => { + if claims.resolution.is_none() { + claims.resolution = Some(r); + } + } + Marker::Source(s) => { + if !sources.contains(&s) { + sources.push(s); + } + } + Marker::Codec(c) => { + if claims.codec.is_none() { + claims.codec = Some(c); + } + } + Marker::Hdr(h) => { + if !claims.hdr.contains(&h) { + claims.hdr.push(h); + } + } + Marker::Language(l) => { + if !claims.languages.contains(&l) { + claims.languages.push(l); + } + } + Marker::Edition(e) => { + if !claims.editions.contains(&e) { + claims.editions.push(e); + } + } + Marker::Audio | Marker::Junk => {} + } +} + +/// Pop edition markers off the tail of the title region ("Movie Extended +/// 2024" carries an edition, not a longer title). Always leaves at least +/// one title token. +fn strip_title_editions(title_toks: &mut Vec, editions: &mut Vec) { + loop { + let n = title_toks.len(); + if n >= 3 { + if let (Some(a), Some(b)) = (title_toks.get(n - 2), title_toks.get(n - 1)) { + if let Some((Marker::Edition(e), _)) = markers::classify_pair(&a.low, &b.low) { + if !editions.contains(&e) { + editions.insert(0, e); + } + title_toks.truncate(n - 2); + continue; + } + } + } + if n >= 2 { + if let Some(last) = title_toks.get(n - 1) { + if let Some((Marker::Edition(e), _)) = markers::classify(&last.low) { + if !editions.contains(&e) { + editions.insert(0, e); + } + title_toks.truncate(n - 1); + continue; + } + } + } + break; + } +} + +/// Index of the first token that unambiguously marks the end of the title: +/// a strong marker, a strong pair, or a season/episode tag. +fn first_strong_index(toks: &[Token]) -> Option { + for (i, tok) in toks.iter().enumerate() { + if markers::is_season_episode(&tok.low) { + return Some(i); + } + if let Some(next) = toks.get(i + 1) { + if let Some((_, Strength::Strong)) = markers::classify_pair(&tok.low, &next.low) { + return Some(i); + } + } + if let Some((_, Strength::Strong)) = markers::classify(&tok.low) { + return Some(i); + } + } + None +} + +fn tokenize(s: &str) -> Vec { + s.split(|c: char| { + matches!( + c, + '.' | '_' | '[' | ']' | '(' | ')' | '{' | '}' | ',' | ';' | '!' | '?' + ) || c.is_whitespace() + }) + .filter(|t| !t.is_empty()) + .map(|t| Token { + orig: t.to_string(), + low: t.to_lowercase(), + }) + .collect() +} + +/// Drop a leading `www..` tag. +fn strip_site_prefix(toks: &mut Vec) { + if toks.first().is_some_and(|t| t.low == "www") { + let tld = toks.iter().take(4).position(|t| { + matches!( + t.low.as_str(), + "com" | "net" | "org" | "to" | "io" | "me" | "cc" + ) + }); + if let Some(i) = tld { + toks.drain(..=i); + } + } + while toks + .first() + .is_some_and(|t| !t.orig.is_empty() && t.orig.chars().all(|c| c == '-')) + { + toks.remove(0); + } +} + +/// Group from a trailing dash: `x265-GROUP`, `TrueHD.7.1-FGT`, or the +/// spaced form `... - GROUP`. Requires either a known marker left of the +/// dash or a strong marker elsewhere, so `Spider-Man` alone keeps its dash. +fn take_dash_group(toks: &mut Vec, has_strong: bool) -> Option { + if toks.len() >= 2 && has_strong { + let sep = &toks[toks.len() - 2].orig; + let last = &toks[toks.len() - 1]; + if !sep.is_empty() && sep.chars().all(|c| c == '-') && group_ok(&last.low) { + let g = last.orig.clone(); + toks.truncate(toks.len() - 2); + return Some(g); + } + } + let last = toks.last()?; + if !last.orig.contains('-') + || markers::classify(&last.low).is_some() + || markers::is_season_episode(&last.low) + { + return None; + } + let (left, right) = last.orig.rsplit_once('-')?; + let left_low = left.to_lowercase(); + let left_known = markers::classify(&left_low).is_some(); + if !left.is_empty() && group_ok(&right.to_lowercase()) && (left_known || has_strong) { + let group = right.to_string(); + let n = toks.len(); + toks[n - 1] = Token { + orig: left.to_string(), + low: left_low, + }; + return Some(group); + } + None +} + +fn group_ok(low: &str) -> bool { + !low.is_empty() + && low.len() <= 24 + && low.chars().any(|c| c.is_ascii_alphabetic()) + && markers::classify(low).is_none() + && markers::year_of(low).is_none() + && !markers::is_season_episode(low) +} + +/// Group from a trailing bracket tag, YTS style: `Movie (2024) [1080p] +/// [YTS.MX]`. Bracketed chunks holding known markers are left in place for +/// the token scan. +fn take_trailing_bracket_group(s: &str) -> (String, Option) { + const DENY: [&str; 4] = ["rartv", "eztv", "ettv", "tgx"]; + let mut end = s.len(); + loop { + let head = s[..end].trim_end_matches(|c: char| c.is_whitespace() || c == '.' || c == '-'); + if !head.ends_with(']') { + break; + } + let Some(open) = head.rfind('[') else { break }; + let content = &head[open + 1..head.len() - 1]; + let low = content.to_lowercase(); + let tokens_clean = low + .split(['.', '_', ' ', '-']) + .filter(|t| !t.is_empty()) + .all(|t| { + markers::classify(t).is_none() + && markers::year_of(t).is_none() + && !markers::is_season_episode(t) + }); + if !content.is_empty() + && !content.contains(' ') + && content.chars().any(|c| c.is_ascii_alphabetic()) + && !DENY.contains(&low.as_str()) + && tokens_clean + { + let mut out = String::with_capacity(s.len()); + out.push_str(&s[..open]); + out.push_str(&s[head.len()..]); + return (out, Some(content.to_string())); + } + end = open; + } + (s.to_string(), None) +} + +fn strip_extension(s: &str) -> &str { + if let Some((stem, ext)) = s.rsplit_once('.') { + let known = matches!( + ext.to_lowercase().as_str(), + "mkv" + | "mp4" + | "avi" + | "m4v" + | "mov" + | "wmv" + | "webm" + | "m2ts" + | "flv" + | "mpg" + | "mpeg" + | "iso" + | "divx" + ); + if known && !stem.is_empty() { + return stem; + } + } + s +} diff --git a/crates/arr-parse/src/markers.rs b/crates/arr-parse/src/markers.rs new file mode 100644 index 0000000..e553e9a --- /dev/null +++ b/crates/arr-parse/src/markers.rs @@ -0,0 +1,204 @@ +//! Token classification tables. Input tokens are lowercase. + +use crate::{Codec, Edition, HdrMarker, LanguageMarker, Resolution, Source}; + +/// What a recognised token claims. `Audio` and `Junk` are recognised so they +/// anchor the title boundary and group detection, but are not surfaced. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Marker { + Resolution(Resolution), + Source(Source), + Codec(Codec), + Hdr(HdrMarker), + Language(LanguageMarker), + Edition(Edition), + Audio, + Junk, +} + +/// Strong markers set the title boundary. Weak markers are words that also +/// occur in real titles ("Web", "Cam", "Dual") and only count once the +/// boundary is already known. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Strength { + Strong, + Weak, +} + +pub(crate) fn classify(token: &str) -> Option<(Marker, Strength)> { + if let Some(m) = classify_exact(token) { + return Some(m); + } + if is_audio_family(token) { + return Some((Marker::Audio, Strength::Strong)); + } + None +} + +/// Two-token markers: `WEB DL`, `Dolby Vision`, `Dual Áudio`, `Directors Cut`. +pub(crate) fn classify_pair(a: &str, b: &str) -> Option<(Marker, Strength)> { + let m = match (a, b) { + ("dolby", "vision") => (Marker::Hdr(HdrMarker::DolbyVision), Strength::Strong), + ("web", "dl") => (Marker::Source(Source::WebDl), Strength::Strong), + ("web", "rip") => (Marker::Source(Source::WebRip), Strength::Strong), + ("blu", "ray") => (Marker::Source(Source::BluRay), Strength::Strong), + ("dual", "audio" | "áudio") => (Marker::Language(LanguageMarker::Dual), Strength::Strong), + ("multi", "audio") => (Marker::Language(LanguageMarker::Multi), Strength::Strong), + ("h" | "x", "264") => (Marker::Codec(Codec::X264), Strength::Strong), + ("h" | "x", "265") => (Marker::Codec(Codec::X265), Strength::Strong), + ("directors" | "director's", "cut") => { + (Marker::Edition(Edition::DirectorsCut), Strength::Weak) + } + ("extended", "cut" | "edition") => (Marker::Edition(Edition::Extended), Strength::Weak), + ("special", "edition") => (Marker::Edition(Edition::SpecialEdition), Strength::Weak), + ("imax", "enhanced") => (Marker::Edition(Edition::Imax), Strength::Weak), + ("season", n) if !n.is_empty() && n.bytes().all(|c| c.is_ascii_digit()) => { + (Marker::Junk, Strength::Strong) + } + _ => return None, + }; + Some(m) +} + +#[allow(clippy::too_many_lines)] +fn classify_exact(t: &str) -> Option<(Marker, Strength)> { + use Strength::{Strong, Weak}; + let m = match t { + "2160p" | "4k" | "uhd" => (Marker::Resolution(Resolution::P2160), Strong), + "1080p" | "1080i" => (Marker::Resolution(Resolution::P1080), Strong), + "720p" => (Marker::Resolution(Resolution::P720), Strong), + "576p" | "576i" => (Marker::Resolution(Resolution::P576), Strong), + "480p" | "480i" => (Marker::Resolution(Resolution::P480), Strong), + + "remux" | "bdremux" => (Marker::Source(Source::Remux), Strong), + "bluray" | "blu-ray" | "bdrip" | "brrip" => (Marker::Source(Source::BluRay), Strong), + "web-dl" | "webdl" => (Marker::Source(Source::WebDl), Strong), + "webrip" | "web-rip" => (Marker::Source(Source::WebRip), Strong), + "web" => (Marker::Source(Source::WebDl), Weak), + "hdtv" | "pdtv" => (Marker::Source(Source::Hdtv), Strong), + "dvdrip" | "dvd" => (Marker::Source(Source::Dvd), Strong), + "telesync" | "hdts" => (Marker::Source(Source::Telesync), Strong), + "ts" => (Marker::Source(Source::Telesync), Weak), + "telecine" | "hdtc" => (Marker::Source(Source::Telecine), Strong), + "tc" => (Marker::Source(Source::Telecine), Weak), + "screener" | "dvdscr" | "bdscr" => (Marker::Source(Source::Screener), Strong), + "scr" => (Marker::Source(Source::Screener), Weak), + "camrip" | "hdcam" => (Marker::Source(Source::Cam), Strong), + "cam" => (Marker::Source(Source::Cam), Weak), + + "x264" | "h264" | "avc" => (Marker::Codec(Codec::X264), Strong), + "x265" | "h265" | "hevc" => (Marker::Codec(Codec::X265), Strong), + "av1" => (Marker::Codec(Codec::Av1), Strong), + "xvid" | "divx" => (Marker::Codec(Codec::Xvid), Strong), + + "dovi" => (Marker::Hdr(HdrMarker::DolbyVision), Strong), + "dv" => (Marker::Hdr(HdrMarker::DolbyVision), Weak), + "hdr10+" | "hdr10plus" => (Marker::Hdr(HdrMarker::Hdr10Plus), Strong), + "hdr10" => (Marker::Hdr(HdrMarker::Hdr10), Strong), + "hdr" => (Marker::Hdr(HdrMarker::Hdr), Strong), + "hlg" => (Marker::Hdr(HdrMarker::Hlg), Strong), + "sdr" => (Marker::Hdr(HdrMarker::Sdr), Weak), + + // pt-BR self-identification (`DESIGN.md` §5.2): loud and distinctive. + "pt-br" | "ptbr" | "dublado" | "nacional" => { + (Marker::Language(LanguageMarker::PtBr), Strong) + } + "pt-pt" | "ptpt" => (Marker::Language(LanguageMarker::PtPt), Strong), + "portuguese" | "português" | "portugues" => { + (Marker::Language(LanguageMarker::Portuguese), Strong) + } + "por" | "pt" => (Marker::Language(LanguageMarker::Portuguese), Weak), + "multi" => (Marker::Language(LanguageMarker::Multi), Strong), + "dual" => (Marker::Language(LanguageMarker::Dual), Weak), + "english" => (Marker::Language(LanguageMarker::English), Strong), + "eng" => (Marker::Language(LanguageMarker::English), Weak), + "french" | "truefrench" | "vostfr" => (Marker::Language(LanguageMarker::French), Strong), + "fre" | "fra" | "vf" | "vff" => (Marker::Language(LanguageMarker::French), Weak), + "german" | "deutsch" => (Marker::Language(LanguageMarker::German), Strong), + "ger" => (Marker::Language(LanguageMarker::German), Weak), + "spanish" | "castellano" | "latino" => (Marker::Language(LanguageMarker::Spanish), Strong), + "spa" | "esp" => (Marker::Language(LanguageMarker::Spanish), Weak), + "italian" | "italiano" => (Marker::Language(LanguageMarker::Italian), Strong), + "ita" => (Marker::Language(LanguageMarker::Italian), Weak), + + "extended" => (Marker::Edition(Edition::Extended), Weak), + "unrated" => (Marker::Edition(Edition::Unrated), Weak), + "uncut" => (Marker::Edition(Edition::Uncut), Weak), + "theatrical" => (Marker::Edition(Edition::Theatrical), Weak), + "imax" => (Marker::Edition(Edition::Imax), Weak), + "remastered" => (Marker::Edition(Edition::Remastered), Weak), + "criterion" => (Marker::Edition(Edition::Criterion), Weak), + "limited" => (Marker::Edition(Edition::Limited), Weak), + + "truehd" | "atmos" | "flac" | "opus" | "mp3" | "dd+" | "ma" => (Marker::Audio, Strong), + + // "Legendado" claims pt-BR *subtitles* over original audio — it must + // NOT become a PtBr audio claim or main-root originals get rejected. + "proper" | "repack" | "internal" | "hybrid" | "10bit" | "8bit" | "hi10p" | "amzn" + | "dsnp" | "atvp" | "hmax" | "pcok" | "legendado" => (Marker::Junk, Strong), + "nf" | "hulu" | "max" | "itunes" | "cr" | "complete" | "sample" | "subbed" | "subs" + | "sub" | "mkv" | "mp4" | "avi" | "www" | "com" | "net" | "org" | "retail" | "readnfo" => { + (Marker::Junk, Weak) + } + _ => return None, + }; + Some(m) +} + +fn is_audio_family(t: &str) -> bool { + const PREFIXES: [&str; 9] = [ + "ddp", "dd5", "dd7", "dd2", "dts", "eac3", "ac3", "aac", "truehd", + ]; + PREFIXES.iter().any(|p| { + t.strip_prefix(p).is_some_and(|rest| { + matches!(rest, "-hd" | "hd" | "-x" | "x" | "-ma" | "ma" | "-hd-ma") + || rest + .chars() + .all(|c| c.is_ascii_digit() || matches!(c, '.' | '+' | '-')) + }) + }) +} + +/// `S01`, `S01E02`, `S01E01E02`, `S01-E02`, `1x02`. +pub(crate) fn is_season_episode(t: &str) -> bool { + fn digits(s: &str) -> (usize, &str) { + let n = s.bytes().take_while(u8::is_ascii_digit).count(); + (n, &s[n..]) + } + if let Some(rest) = t.strip_prefix('s') { + let (n, mut rest) = digits(rest); + if !(1..=2).contains(&n) { + return false; + } + while !rest.is_empty() { + let Some(r) = rest.strip_prefix('e').or_else(|| rest.strip_prefix("-e")) else { + return false; + }; + let (m, r2) = digits(r); + if !(1..=3).contains(&m) { + return false; + } + rest = r2; + } + return true; + } + let (n, rest) = digits(t); + if (1..=2).contains(&n) { + if let Some(r) = rest.strip_prefix('x') { + let (m, r2) = digits(r); + return (2..=3).contains(&m) && r2.is_empty(); + } + } + false +} + +/// A plausible release year: four digits, 1900–2099. +pub(crate) fn year_of(t: &str) -> Option { + if t.len() == 4 && t.bytes().all(|b| b.is_ascii_digit()) { + let y: u16 = t.parse().ok()?; + if (1900..=2099).contains(&y) { + return Some(y); + } + } + None +} diff --git a/crates/arr-parse/tests/parse.rs b/crates/arr-parse/tests/parse.rs new file mode 100644 index 0000000..5a69403 --- /dev/null +++ b/crates/arr-parse/tests/parse.rs @@ -0,0 +1,355 @@ +//! Table-driven checks over representative release names. The full fixture +//! corpus pulled from real indexers is issue #9; these rows pin the parser's +//! behaviour per shape of name. + +use arr_parse::{parse, Codec, Edition, HdrMarker, LanguageMarker, NameClaims, Resolution, Source}; +use serde as _; + +struct Case { + name: &'static str, + want: NameClaims, +} + +#[allow(clippy::unnecessary_wraps)] +fn s(v: &str) -> Option { + Some(v.to_string()) +} + +#[allow(clippy::too_many_lines)] +fn cases() -> Vec { + vec![ + Case { + name: "Movie.Title.2024.2160p.UHD.BluRay.DV.HDR10.x265.TrueHD-GROUP", + want: NameClaims { + title: s("Movie Title"), + year: Some(2024), + resolution: Some(Resolution::P2160), + source: Some(Source::BluRay), + codec: Some(Codec::X265), + hdr: vec![HdrMarker::DolbyVision, HdrMarker::Hdr10], + group: s("GROUP"), + ..NameClaims::default() + }, + }, + Case { + // "Web" before the year is title, not a source claim. + name: "Charlottes.Web.2006.1080p.BluRay.x264-GRP", + want: NameClaims { + title: s("Charlottes Web"), + year: Some(2006), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + codec: Some(Codec::X264), + group: s("GRP"), + ..NameClaims::default() + }, + }, + Case { + name: "Dune Part Two (2024) [2160p] [WEB-DL] [HDR10+] [YTS.MX]", + want: NameClaims { + title: s("Dune Part Two"), + year: Some(2024), + resolution: Some(Resolution::P2160), + source: Some(Source::WebDl), + hdr: vec![HdrMarker::Hdr10Plus], + group: s("YTS.MX"), + ..NameClaims::default() + }, + }, + Case { + name: "O.Auto.da.Compadecida.2000.NACIONAL.1080p.WEB-DL.x264-BRA", + want: NameClaims { + title: s("O Auto da Compadecida"), + year: Some(2000), + resolution: Some(Resolution::P1080), + source: Some(Source::WebDl), + codec: Some(Codec::X264), + languages: vec![LanguageMarker::PtBr], + group: s("BRA"), + ..NameClaims::default() + }, + }, + Case { + // Repeated pt-BR markers dedupe to one claim. + name: "Filme.2023.Dublado.PT-BR.1080p.WEBRip.x264", + want: NameClaims { + title: s("Filme"), + year: Some(2023), + resolution: Some(Resolution::P1080), + source: Some(Source::WebRip), + codec: Some(Codec::X264), + languages: vec![LanguageMarker::PtBr], + ..NameClaims::default() + }, + }, + Case { + name: "Filme.Infantil.2023.1080p.WEB-DL.Dual.Áudio.x264", + want: NameClaims { + title: s("Filme Infantil"), + year: Some(2023), + resolution: Some(Resolution::P1080), + source: Some(Source::WebDl), + codec: Some(Codec::X264), + languages: vec![LanguageMarker::Dual], + ..NameClaims::default() + }, + }, + Case { + name: "Movie.2024.MULTi.1080p.WEB.x265-GRP", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::WebDl), + codec: Some(Codec::X265), + languages: vec![LanguageMarker::Multi], + group: s("GRP"), + ..NameClaims::default() + }, + }, + Case { + // Season/episode tags bound the title; no year needed. + name: "Serie.S01E02.720p.HDTV.x264-LOL", + want: NameClaims { + title: s("Serie"), + resolution: Some(Resolution::P720), + source: Some(Source::Hdtv), + codec: Some(Codec::X264), + group: s("LOL"), + ..NameClaims::default() + }, + }, + Case { + // Remux beats the BluRay marker riding along. + name: "Movie.2024.2160p.BluRay.REMUX.HEVC.TrueHD.7.1.Atmos-FGT", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P2160), + source: Some(Source::Remux), + codec: Some(Codec::X265), + group: s("FGT"), + ..NameClaims::default() + }, + }, + Case { + name: "Movie.2024.Extended.1080p.BluRay.x264", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + codec: Some(Codec::X264), + editions: vec![Edition::Extended], + ..NameClaims::default() + }, + }, + Case { + // Edition between title and year is stripped off the title tail. + name: "Movie.Directors.Cut.2024.1080p.BluRay", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + editions: vec![Edition::DirectorsCut], + ..NameClaims::default() + }, + }, + Case { + // First year-like token is the title, second is the year. + name: "2012.2009.1080p.BluRay.x264", + want: NameClaims { + title: s("2012"), + year: Some(2009), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + codec: Some(Codec::X264), + ..NameClaims::default() + }, + }, + Case { + name: "Blade.Runner.2049.2017.2160p.WEB-DL.DDP5.1.HDR.HEVC-XEBEC", + want: NameClaims { + title: s("Blade Runner 2049"), + year: Some(2017), + resolution: Some(Resolution::P2160), + source: Some(Source::WebDl), + codec: Some(Codec::X265), + hdr: vec![HdrMarker::Hdr], + group: s("XEBEC"), + ..NameClaims::default() + }, + }, + Case { + name: "Movie.2024.HDTS.x264-ETRG", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + source: Some(Source::Telesync), + codec: Some(Codec::X264), + group: s("ETRG"), + ..NameClaims::default() + }, + }, + Case { + name: "Movie.2024.1080p.HMAX.WEB-DL.DD5.1.H.264-GROUP", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::WebDl), + codec: Some(Codec::X264), + group: s("GROUP"), + ..NameClaims::default() + }, + }, + Case { + // Spaced dash before the group. + name: "Movie Title 2024 1080p BluRay x265 - VXT", + want: NameClaims { + title: s("Movie Title"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + codec: Some(Codec::X265), + group: s("VXT"), + ..NameClaims::default() + }, + }, + Case { + // Filename with a container extension. + name: "Movie.Title.2024.720p.WEBRip.x264.mkv", + want: NameClaims { + title: s("Movie Title"), + year: Some(2024), + resolution: Some(Resolution::P720), + source: Some(Source::WebRip), + codec: Some(Codec::X264), + ..NameClaims::default() + }, + }, + Case { + // No markers at all: the whole thing is a title claim. + name: "Some Random Words", + want: NameClaims { + title: s("Some Random Words"), + ..NameClaims::default() + }, + }, + Case { + // Marker-only garbage parses partially, title absent. + name: "1080p", + want: NameClaims { + resolution: Some(Resolution::P1080), + ..NameClaims::default() + }, + }, + Case { + name: "", + want: NameClaims::default(), + }, + Case { + // Dolby Vision spelled out, dotted. + name: "Movie.2024.2160p.WEB-DL.Dolby.Vision.HDR10.HEVC-GRP", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P2160), + source: Some(Source::WebDl), + codec: Some(Codec::X265), + hdr: vec![HdrMarker::DolbyVision, HdrMarker::Hdr10], + group: s("GRP"), + ..NameClaims::default() + }, + }, + Case { + // Hyphenated title survives when nothing marks a group. + name: "Spider-Man.2002.1080p.BluRay.x264-SECTOR7", + want: NameClaims { + title: s("Spider-Man"), + year: Some(2002), + resolution: Some(Resolution::P1080), + source: Some(Source::BluRay), + codec: Some(Codec::X264), + group: s("SECTOR7"), + ..NameClaims::default() + }, + }, + Case { + name: "www.Torrenting.com - Movie.2024.1080p.WEB-DL.x264", + want: NameClaims { + title: s("Movie"), + year: Some(2024), + resolution: Some(Resolution::P1080), + source: Some(Source::WebDl), + codec: Some(Codec::X264), + ..NameClaims::default() + }, + }, + Case { + // Season pack, no episode. + name: "Show.Name.S02.2160p.WEB-DL.DDP5.1.DV.HDR10.H.265-NTb", + want: NameClaims { + title: s("Show Name"), + resolution: Some(Resolution::P2160), + source: Some(Source::WebDl), + codec: Some(Codec::X265), + hdr: vec![HdrMarker::DolbyVision, HdrMarker::Hdr10], + group: s("NTb"), + ..NameClaims::default() + }, + }, + Case { + name: "Movie.2019.4K.HDR.DV.2160p.BDRemux.Ita.Eng.x265-NAHOM", + want: NameClaims { + title: s("Movie"), + year: Some(2019), + resolution: Some(Resolution::P2160), + source: Some(Source::Remux), + codec: Some(Codec::X265), + hdr: vec![HdrMarker::Hdr, HdrMarker::DolbyVision], + languages: vec![LanguageMarker::Italian, LanguageMarker::English], + group: s("NAHOM"), + ..NameClaims::default() + }, + }, + ] +} + +#[test] +fn corpus_rows_parse_as_expected() { + let mut failures = Vec::new(); + for Case { name, want } in cases() { + let got = parse(name); + if got != want { + failures.push(format!("{name:?}\n got: {got:?}\n want: {want:?}")); + } + } + assert!( + failures.is_empty(), + "{} mismatches:\n{}", + failures.len(), + failures.join("\n") + ); +} + +#[test] +fn garbage_never_panics() { + let inputs = [ + "", + " ", + "....", + "----", + "[[[]]]", + "()()()", + "ção áéíóú 日本語 🎬🎬🎬", + "-.-.-.-[]()s01e999999999999999999999", + "Movie.2024.1080p-", + "a", + &"x.".repeat(5000), + ]; + for input in inputs { + let _ = parse(input); + } +}