From 288d00d6491f5699b9280a587002e7c113da2f91 Mon Sep 17 00:00:00 2001 From: Alexandru Pisarenco Date: Fri, 15 Jan 2021 04:02:21 +0100 Subject: [PATCH] Make MenuItem duration detection safer --- minfo.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/minfo.py b/minfo.py index 2ab5db0..6c48838 100644 --- a/minfo.py +++ b/minfo.py @@ -63,17 +63,31 @@ class AudioInfo: class MenuItem: + TIME_RE = re.compile(r"(\d{2})[^\d]+(\d{2})[^\d]+(\d{2})(?:[^\d]+(\d+))") def __init__(self, *, start: str, duration: str, name: str): self.start = start self.duration = duration self.name = name + @property + def str_start(self) -> timedelta: + match = self.TIME_RE.search(self.start) + hours, minutes, seconds, millis = tuple(map(int, (match.group(i + 1) for i in range(4)))) + return timedelta(hours=hours, minutes=minutes, seconds=seconds, milliseconds=millis) + + def __str__(self) -> str: + return f"'{self.name}' at {self.str_start}, duration {self.duration}" + + def __repr__(self) -> str: + return f"" + class MenuInfo: REGEX = re.compile(r"^_\d{2}_\d{2}_\d{2}") - def __init__(self, menu_dict: Dict[str, str]): + def __init__(self, menu_dict: Dict[str, str], duration: str = None): self.data = menu_dict + self.duration: Optional[float] = timeparse(duration) if duration else None @property def items(self) -> List[MenuItem]: @@ -100,7 +114,7 @@ class MenuInfo: result[-1].duration = duration result.append(MenuItem(start=key, duration="", name=name)) if result: - td = timedelta(seconds=float(self.data["Duration"])) + td = timedelta(seconds=float(self.data.get("Duration", self.duration))) dur_td = timedelta(seconds=round((td - td0).total_seconds())) duration = str(dur_td) duration = f"{duration:0>8}" @@ -227,4 +241,4 @@ class MediaInfo: @property def menu(self) -> MenuInfo: - return MenuInfo(self.menu_data or { self.duration: "Chapter 1" }) + return MenuInfo(self.menu_data or { self.duration: "Chapter 1" }, duration=self.duration)