From e1242eaa61a220fd53d6d1fd439d825ed7245318 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 19 Apr 2023 18:44:25 -0700 Subject: [PATCH] [BUGFIX] Fix parsing chapters from comments (#592) --- 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 785123ee..7bdbf736 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_entry_chapters(cls, entry: Entry) -> "Chapters":