Add IMDb ID validation with regex pattern matching for tt followed by 7-8 digits

This commit is contained in:
Esa Kataja
2025-04-17 13:16:22 +03:00
parent cf4b7fa106
commit 404b61aa00
+12 -1
View File
@@ -2,6 +2,8 @@ import json
import subprocess import subprocess
import typer import typer
from pathlib import Path from pathlib import Path
import re
import click
from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream, OriginalMediaType from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream, OriginalMediaType
from movie_details import get_movie_details from movie_details import get_movie_details
@@ -23,10 +25,19 @@ def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile:
return VideoFile return VideoFile
def validate_imdb(ctx: click.Context, param: click.Parameter, value: str) -> str:
"""Validate that an IMDb ID matches 'tt' followed by 7-8 digits."""
if value is None:
return value
if not re.match(r"^tt\d{7,8}$", value):
raise click.BadParameter("IMDb ID must be 'tt' followed by 7-8 digits (e.g. tt1234567)")
return value
@app.command() @app.command()
def main( def main(
filename: Path = typer.Argument(..., exists=True, help="Input video file"), filename: Path = typer.Argument(..., exists=True, help="Input video file"),
imdb: str = typer.Option(None, "-i", "--imdb", help="IMDB ID"), imdb: str = typer.Option(None, "-i", "--imdb", callback=validate_imdb, help="IMDB ID"),
nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"), nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"),
preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"), preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"),
crf: int = typer.Option(30, "-c", "--crf", help="CRF value"), crf: int = typer.Option(30, "-c", "--crf", help="CRF value"),