From b6b1b1ae2b98cb32e660c272d2aa0ac22ce39884 Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Thu, 16 Nov 2023 16:27:05 -0700 Subject: [PATCH 1/2] Pass timespec all the way down in dumps --- m3u8/model.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/m3u8/model.py b/m3u8/model.py index a2bcef70..b906b5e9 100644 --- a/m3u8/model.py +++ b/m3u8/model.py @@ -364,7 +364,7 @@ def add_segment(self, segment): def add_rendition_report(self, report): self.rendition_reports.append(report) - def dumps(self): + def dumps(self, timespec="milliseconds"): """ Returns the current m3u8 as a string. You could also use unicode() or str() @@ -416,7 +416,7 @@ def dumps(self): for key in self.session_keys: output.append(str(key)) - output.append(str(self.segments)) + output.append(self.segments.dumps(timespec)) if self.preload_hint: output.append(str(self.preload_hint)) @@ -702,14 +702,17 @@ def base_uri(self, newbase_uri): class SegmentList(list, GroupedBasePathMixin): - def __str__(self): + def dumps(self, timespec="milliseconds"): output = [] last_segment = None for segment in self: - output.append(segment.dumps(last_segment)) + output.append(segment.dumps(last_segment, timespec)) last_segment = segment return "\n".join(output) + def __str__(self): + return self.dumps() + @property def uri(self): return [seg.uri for seg in self] From c04a166b551327abc4d039ae37a4cd713225ed78 Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Thu, 16 Nov 2023 16:48:25 -0700 Subject: [PATCH 2/2] Add test of using timespec on the playlist --- tests/test_model.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_model.py b/tests/test_model.py index 40c41e30..6cbdda5c 100755 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -765,6 +765,13 @@ def test_dump_segment_honors_timespec(): assert "EXT-X-PROGRAM-DATE-TIME:2014-08-13T13:36:33.000000+00:00" in segment_text +def test_dump_honors_timespec(): + obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST_WITH_PROGRAM_DATE_TIME) + obj_text = obj.dumps(timespec="microseconds").strip() + + assert "EXT-X-PROGRAM-DATE-TIME:2014-08-13T13:36:33.000000+00:00" in obj_text + + def test_dump_should_not_ignore_zero_duration(): obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST_WITH_ZERO_DURATION)