From a9883cb2490fe61435c06e36821a8d75ab55a1d6 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 19 Apr 2023 18:31:47 -0700 Subject: [PATCH] [BUGFIX] Fix parsing chapters from comments --- src/ytdl_sub/utils/chapters.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/ytdl_sub/utils/chapters.py b/src/ytdl_sub/utils/chapters.py index b3557774..e6ad6aad 100644 --- a/src/ytdl_sub/utils/chapters.py +++ b/src/ytdl_sub/utils/chapters.py @@ -202,6 +202,7 @@ class Chapters: timestamps: List[Timestamp] = [] titles: List[str] = [] + # Try to accumulate chapters by parsing lines individually for line in input_str.split("\n"): # Timestamp captured, store it if match := Timestamp.TIMESTAMP_REGEX.search(line): @@ -211,14 +212,12 @@ class Chapters: # Remove timestamp and surrounding whitespace from it title_str = re.sub(f"\\s*{re.escape(timestamp_str)}\\s*", " ", line).strip() titles.append(title_str) - elif len(timestamps) >= 3: - return Chapters(timestamps=timestamps, titles=titles) - # Timestamp was not stored, if only contained 1, reset - else: - timestamps = [] - titles = [] - return Chapters(timestamps=timestamps, titles=titles) + # If more than 3 timestamps were parsed, return it + if len(timestamps) >= 3: + return Chapters(timestamps=timestamps, titles=titles) + # Otherwise return empty chapters + return Chapters(timestamps=[], titles=[]) @classmethod def from_embedded_chapters(cls, ffprobe_path: str, file_path: str) -> "Chapters":