Support duplicate chapter timestamps by merging (#1179)
Co-authored-by: Tyler Dakin <tyler@dakin.one>
This commit is contained in:
parent
0ebcfb6a57
commit
bb2bc49a49
2 changed files with 28 additions and 1 deletions
|
|
@ -220,10 +220,17 @@ class Chapters:
|
|||
# Timestamp captured, store it
|
||||
if match := Timestamp.TIMESTAMP_REGEX.search(line):
|
||||
timestamp_str = match.group(1)
|
||||
timestamps.append(Timestamp.from_str(timestamp_str))
|
||||
|
||||
# Remove timestamp and surrounding whitespace from it
|
||||
title_str = re.sub(f"\\s*{re.escape(timestamp_str)}\\s*", " ", line).strip()
|
||||
|
||||
timestamp = Timestamp.from_str(timestamp_str)
|
||||
if timestamps and (timestamps[-1].timestamp_sec == timestamp.timestamp_sec):
|
||||
# duplicate timestamp... combine into one chapter
|
||||
titles[-1] += " // " + title_str
|
||||
continue
|
||||
timestamps.append(timestamp)
|
||||
|
||||
titles.append(title_str)
|
||||
|
||||
# If more than 3 timestamps were parsed, return it
|
||||
|
|
|
|||
|
|
@ -86,6 +86,21 @@ def chapter_description_3() -> str:
|
|||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def chapter_duplicate_timecodes() -> str:
|
||||
return """01. 00:00 Ocean
|
||||
02:41 Dreams
|
||||
02:41 Nightmares
|
||||
05:16 Future Tales
|
||||
08:50 Mind Travelling
|
||||
11:05 Love Supreme
|
||||
14:17 Reflections
|
||||
14:17 Reflections VIP
|
||||
16:32 Moonlight Fading
|
||||
19:40 Between Two Worlds
|
||||
"""
|
||||
|
||||
|
||||
class TestChapters:
|
||||
def test_chapters_from_str_1(self, chapter_description_1):
|
||||
chapters = Chapters.from_string(chapter_description_1)
|
||||
|
|
@ -104,3 +119,8 @@ class TestChapters:
|
|||
chapters.titles[8]
|
||||
== "(Nightcore) Electro-Light & Jordan Kelvin James - Wait For You (feat. Anna Yvette)"
|
||||
)
|
||||
|
||||
def test_chapters_from_str_with_duplicate_timecodes(self, chapter_duplicate_timecodes):
|
||||
chapters = Chapters.from_string(chapter_duplicate_timecodes)
|
||||
assert len(chapters) == 8
|
||||
assert chapters.titles[5] == "Reflections // Reflections VIP"
|
||||
|
|
|
|||
Loading…
Reference in a new issue