feat(titles): add optional Episode.part, selected with -w s1e1.2

Opt-in and additive: an episode without a part behaves exactly as before.
This commit is contained in:
imSp4rky
2026-08-04 17:38:30 -06:00
parent 10bfaf6a73
commit 041ba5293b
17 changed files with 946 additions and 44 deletions
+44 -13
View File
@@ -174,11 +174,18 @@ class SeasonRange(click.ParamType):
MIN_EPISODE = 0
MAX_EPISODE = 999
MIN_PART = 1
MAX_PART = 99
def parse_tokens(self, *tokens: str) -> list[str]:
"""
Parse multiple tokens or ranged tokens as '{s}x{e}' strings.
An episode split into separately playable parts is addressed as '{s}x{e}.{p}'.
A part range must stay inside one episode, since how many parts an episode has
is not knowable here. A part-qualified exclusion cannot be removed from the
computed keys (they are base keys), so it becomes a '!' key resolved at match time.
Supports exclusioning by putting a `-` before the token.
Example:
@@ -189,6 +196,10 @@ class SeasonRange(click.ParamType):
["2x1", "2x3", "2x4", "2x5"]
>>> sr.parse_tokens("S01-S05", "-S03", "-S02E01")
["1x0", "1x1", ..., "2x0", (...), "2x2", (...), "4x0", ..., "5x0", ...]
>>> sr.parse_tokens("S01E01.1-S01E01.3")
["1x1.1", "1x1.2", "1x1.3"]
>>> sr.parse_tokens("S01E01", "-S01E01.2")
["1x1", "!1x1.2"]
"""
if len(tokens) == 0:
return []
@@ -199,36 +210,56 @@ class SeasonRange(click.ParamType):
if exclude:
token = token[1:]
parsed = [
re.match(r"^S(?P<season>\d+)(E(?P<episode>\d+))?$", x, re.IGNORECASE) for x in re.split(r"[:-]", token)
re.match(r"^S(?P<season>\d+)(E(?P<episode>\d+)(\.(?P<part>\d+))?)?$", x, re.IGNORECASE)
for x in re.split(r"[:-]", token)
]
if len(parsed) > 2:
self.fail(f"Invalid token, only a left and right range is acceptable: {token}")
if len(parsed) == 1:
# the same match object is read with different per-side defaults, so a bare
# S01 spans from MIN_EPISODE to MAX_EPISODE
parsed.append(parsed[0])
if any(x is None for x in parsed):
self.fail(f"Invalid token, syntax error occurred: {token}")
from_season, from_episode = [
int(v) if v is not None else self.MIN_EPISODE
for k, v in parsed[0].groupdict().items()
if parsed[0] # type: ignore[union-attr]
]
to_season, to_episode = [
int(v) if v is not None else self.MAX_EPISODE
for k, v in parsed[1].groupdict().items()
if parsed[1] # type: ignore[union-attr]
]
left, right = parsed[0], parsed[1]
from_season = int(left.group("season")) # type: ignore[union-attr]
from_episode_raw = left.group("episode") # type: ignore[union-attr]
from_episode = int(from_episode_raw) if from_episode_raw is not None else self.MIN_EPISODE
from_part_raw = left.group("part") # type: ignore[union-attr]
from_part = int(from_part_raw) if from_part_raw is not None else None
to_season = int(right.group("season")) # type: ignore[union-attr]
to_episode_raw = right.group("episode") # type: ignore[union-attr]
to_episode = int(to_episode_raw) if to_episode_raw is not None else self.MAX_EPISODE
to_part_raw = right.group("part") # type: ignore[union-attr]
to_part = int(to_part_raw) if to_part_raw is not None else None
if from_season > to_season:
self.fail(f"Invalid range, left side season cannot be bigger than right side season: {token}")
if from_season == to_season and from_episode > to_episode:
self.fail(f"Invalid range, left side episode cannot be bigger than right side episode: {token}")
if (from_part is None) != (to_part is None):
self.fail(f"Invalid range, a part must be given on both sides or on neither: {token}")
if from_part is not None and to_part is not None:
if (from_season, from_episode) != (to_season, to_episode):
self.fail(f"Invalid range, a part range must stay within one episode: {token}")
if not all(self.MIN_PART <= p <= self.MAX_PART for p in (from_part, to_part)):
self.fail(f"Invalid part, must be between {self.MIN_PART} and {self.MAX_PART}: {token}")
if from_part > to_part:
self.fail(f"Invalid range, left side part cannot be bigger than right side part: {token}")
for p in range(from_part, to_part + 1):
(computed if not exclude else exclusions).append(f"{from_season}x{from_episode}.{p}")
continue
for s in range(from_season, to_season + 1):
for e in range(
from_episode if s == from_season else 0, (self.MAX_EPISODE if s < to_season else to_episode) + 1
):
(computed if not exclude else exclusions).append(f"{s}x{e}")
for exclusion in exclusions:
if exclusion in computed:
computed.remove(exclusion)
if "." in exclusion:
computed.append(f"!{exclusion}") # base key stays; resolved at match time
else:
# a base exclusion must drop that episode's part keys too, or they still match
prefix = f"{exclusion}."
computed = [k for k in computed if k != exclusion and not k.startswith(prefix)]
return list(set(computed))
def convert(