From 024e9bfd5ec6ed6be14c958244077320afaf2f27 Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Mon, 16 Oct 2023 11:33:03 -0600 Subject: [PATCH 1/3] Use built-in ISO 8601 parse for Python 3.11 and later --- m3u8/parser.py | 8 ++++++-- requirements.txt | 2 +- setup.py | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/m3u8/parser.py b/m3u8/parser.py index 3f511e3c..5fceccfa 100644 --- a/m3u8/parser.py +++ b/m3u8/parser.py @@ -8,7 +8,11 @@ import re from urllib.parse import urljoin as _urljoin -import iso8601 +try: + from iso8601 import parse_date +except ImportError: + parse_date = datetime.datetime.fromisoformat + from m3u8 import protocol @@ -20,7 +24,7 @@ def cast_date_time(value): - return iso8601.parse_date(value) + return parse_date(value) def format_date_time(value, **kwargs): diff --git a/requirements.txt b/requirements.txt index 57c80f08..f2d13e13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -iso8601 +iso8601; python_version < '3.11' diff --git a/setup.py b/setup.py index d9a95375..f1c01d4a 100644 --- a/setup.py +++ b/setup.py @@ -23,5 +23,5 @@ url="https://github.com/globocom/m3u8", description="Python m3u8 parser", long_description=long_description, - python_requires=">=3.6", + python_requires=">=3.7", ) From bb558f5c7270155abeba91407d98dfc6e6ba1925 Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Mon, 16 Oct 2023 11:43:03 -0600 Subject: [PATCH 2/3] Use backport of built-in function for performance improvement --- m3u8/parser.py | 7 ++++--- requirements.txt | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/m3u8/parser.py b/m3u8/parser.py index 5fceccfa..07f9ac83 100644 --- a/m3u8/parser.py +++ b/m3u8/parser.py @@ -9,9 +9,10 @@ from urllib.parse import urljoin as _urljoin try: - from iso8601 import parse_date + from backports.datetime_fromisoformat import MonkeyPatch + MonkeyPatch.patch_fromisoformat() except ImportError: - parse_date = datetime.datetime.fromisoformat + pass from m3u8 import protocol @@ -24,7 +25,7 @@ def cast_date_time(value): - return parse_date(value) + return datetime.datetime.fromisoformat(value) def format_date_time(value, **kwargs): diff --git a/requirements.txt b/requirements.txt index f2d13e13..c785d132 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -iso8601; python_version < '3.11' +backports-datetime-fromisoformat; python_version < '3.11' From e25844fcaa8d0853746493d2673a199afceca790 Mon Sep 17 00:00:00 2001 From: Dave Johansen Date: Mon, 16 Oct 2023 14:33:23 -0600 Subject: [PATCH 3/3] Switch to from import --- m3u8/parser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/m3u8/parser.py b/m3u8/parser.py index 07f9ac83..55e56e6b 100644 --- a/m3u8/parser.py +++ b/m3u8/parser.py @@ -3,9 +3,9 @@ # Use of this source code is governed by a MIT License # license that can be found in the LICENSE file. -import datetime import itertools import re +from datetime import datetime, timedelta from urllib.parse import urljoin as _urljoin try: @@ -25,7 +25,7 @@ def cast_date_time(value): - return datetime.datetime.fromisoformat(value) + return datetime.fromisoformat(value) def format_date_time(value, **kwargs): @@ -292,7 +292,7 @@ def _parse_ts_chunk(line, data, state): segment["program_date_time"] = state.pop("program_date_time") if state.get("current_program_date_time"): segment["current_program_date_time"] = state["current_program_date_time"] - state["current_program_date_time"] += datetime.timedelta( + state["current_program_date_time"] += timedelta( seconds=segment["duration"] ) segment["uri"] = line @@ -580,7 +580,7 @@ def _parse_part(line, data, state): # this should always be true according to spec if state.get("current_program_date_time"): part["program_date_time"] = state["current_program_date_time"] - state["current_program_date_time"] += datetime.timedelta( + state["current_program_date_time"] += timedelta( seconds=part["duration"] )