Make MenuItem duration detection safer
This commit is contained in:
20
minfo.py
20
minfo.py
@@ -63,17 +63,31 @@ class AudioInfo:
|
|||||||
|
|
||||||
|
|
||||||
class MenuItem:
|
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):
|
def __init__(self, *, start: str, duration: str, name: str):
|
||||||
self.start = start
|
self.start = start
|
||||||
self.duration = duration
|
self.duration = duration
|
||||||
self.name = name
|
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"<MenuItem '{self.name}' at {self.start}, duration {self.duration}>"
|
||||||
|
|
||||||
|
|
||||||
class MenuInfo:
|
class MenuInfo:
|
||||||
REGEX = re.compile(r"^_\d{2}_\d{2}_\d{2}")
|
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.data = menu_dict
|
||||||
|
self.duration: Optional[float] = timeparse(duration) if duration else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def items(self) -> List[MenuItem]:
|
def items(self) -> List[MenuItem]:
|
||||||
@@ -100,7 +114,7 @@ class MenuInfo:
|
|||||||
result[-1].duration = duration
|
result[-1].duration = duration
|
||||||
result.append(MenuItem(start=key, duration="", name=name))
|
result.append(MenuItem(start=key, duration="", name=name))
|
||||||
if result:
|
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()))
|
dur_td = timedelta(seconds=round((td - td0).total_seconds()))
|
||||||
duration = str(dur_td)
|
duration = str(dur_td)
|
||||||
duration = f"{duration:0>8}"
|
duration = f"{duration:0>8}"
|
||||||
@@ -227,4 +241,4 @@ class MediaInfo:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def menu(self) -> MenuInfo:
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user