[dt] replace 'parse_compat' with compat workaround

This commit is contained in:
Mike Fährmann
2025-10-19 18:26:32 +02:00
parent 6c71b279b6
commit 989136bc59
3 changed files with 16 additions and 33 deletions

View File

@@ -38,19 +38,10 @@ def convert(value):
"""Convert 'value' to a naive UTC datetime object"""
if not value:
return NONE
if isinstance(value, datetime):
return value
if isinstance(value, str):
try:
if value[-1] == "Z":
# compat for Python < 3.11
value = value[:-1]
return normalize(datetime.fromisoformat(value))
except Exception:
pass
return normalize(value)
if isinstance(value, str) and (dt := parse_iso(value)) is not NONE:
return dt
return parse_ts(value)
@@ -70,14 +61,13 @@ if sys.hexversion < 0x30c0000:
if dt_string[-1] == "Z":
# compat for Python < 3.11
dt_string = dt_string[:-1]
elif dt_string[-5] in "+-":
# compat for Python < 3.11
dt_string = f"{dt_string[:-2]}:{dt_string[-2:]}"
return normalize(datetime.fromisoformat(dt_string))
except Exception:
return NONE
def parse_compat(dt_string, format="%Y-%m-%dT%H:%M:%S%z"):
"""Parse 'dt_string' as ISO 8601 value using 'format'"""
return parse(dt_string, format)
from_ts = datetime.utcfromtimestamp
now = datetime.utcnow
@@ -90,10 +80,6 @@ else:
except Exception:
return NONE
def parse_compat(dt_string, format=None):
"""Parse 'dt_string' as ISO 8601 value"""
return parse_iso(dt_string)
def from_ts(ts=None):
"""Convert Unix timestamp to naive UTC datetime"""
Y, m, d, H, M, S, _, _, _ = time.gmtime(ts)

View File

@@ -218,7 +218,7 @@ class NewgroundsExtractor(Extractor):
"description": text.unescape(extr(':description" content="', '"')),
"type" : "art",
"_type" : "i",
"date" : dt.parse_compat(extr(
"date" : dt.parse_iso(extr(
'itemprop="datePublished" content="', '"')),
"rating" : extr('class="rated-', '"'),
"url" : full('src="', '"'),
@@ -268,7 +268,7 @@ class NewgroundsExtractor(Extractor):
"description": text.unescape(extr(':description" content="', '"')),
"type" : "audio",
"_type" : "a",
"date" : dt.parse_compat(extr(
"date" : dt.parse_iso(extr(
'itemprop="datePublished" content="', '"')),
"url" : extr('{"url":"', '"').replace("\\/", "/"),
"index" : text.parse_int(index),
@@ -287,7 +287,7 @@ class NewgroundsExtractor(Extractor):
src = src.replace("\\/", "/")
formats = ()
type = extr(',"description":"', '"')
date = dt.parse_compat(extr(
date = dt.parse_iso(extr(
'itemprop="datePublished" content="', '"'))
if type:
type = type.rpartition(" ")[2].lower()