mirror of
https://github.com/searxng/searxng.git
synced 2026-07-17 21:41:24 +00:00
[refactor] engines: use datetime.fromisoformat instead of datetime.strptime where possible (#6394)
Refactor engines that parse ISO 8601 dates with strptime to use
fromisoformat instead. In most cases this is a direct replacement of
strptime(text, "format") with fromisoformat(text).
For engines where the source has a trailing "Z" that strptime consumed
as a literal (e.g. "%Y-%m-%dT%H:%M:%S.%fZ" in huggingface.py), add
rstrip("Z") to keep the output naive and preserve the existing behavior.
In sogou.py the date is extracted with a regular expression, which can
yield strings like "2026-7-11". strptime accepts this via its format
string, but fromisoformat does not. To preserve the existing behavior
and satisfy the format fromisoformat expects, add zero-padding for the
month and day.
Closes: #6098
---------
Signed-off-by: OneVth <onebrotravel@gmail.com>
This commit is contained in:
@@ -83,7 +83,7 @@ def extract_video_data(video_block):
|
||||
published_date = None
|
||||
if create_time:
|
||||
try:
|
||||
published_date = datetime.strptime(create_time.strip(), "%Y-%m-%d")
|
||||
published_date = datetime.fromisoformat(create_time.strip())
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ def response(resp: "SXNG_Response") -> EngineResults:
|
||||
comments_elements = eval_xpath_getindex(entry, xpath_comment, 0, default=None)
|
||||
comments: str = "" if comments_elements is None else comments_elements.text
|
||||
|
||||
publishedDate = datetime.strptime(eval_xpath_getindex(entry, xpath_published, 0).text, "%Y-%m-%dT%H:%M:%SZ")
|
||||
publishedDate = datetime.fromisoformat(eval_xpath_getindex(entry, xpath_published, 0).text.rstrip("Z"))
|
||||
|
||||
res.add(
|
||||
res.types.Paper(
|
||||
|
||||
@@ -186,7 +186,7 @@ def parse_images(data):
|
||||
img_date = item.get("bdImgnewsDate")
|
||||
publishedDate = None
|
||||
if img_date:
|
||||
publishedDate = datetime.strptime(img_date, "%Y-%m-%d %H:%M")
|
||||
publishedDate = datetime.fromisoformat(img_date)
|
||||
results.append(
|
||||
{
|
||||
"template": "images.html",
|
||||
|
||||
@@ -44,7 +44,7 @@ def response(resp):
|
||||
"url": 'https://www.bitchute.com/video/' + item['video_id'],
|
||||
"content": html_to_text(item['description']),
|
||||
"author": item['channel']['channel_name'],
|
||||
"publishedDate": datetime.strptime(item["date_published"], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
"publishedDate": datetime.fromisoformat(item["date_published"].rstrip("Z")),
|
||||
"length": item['duration'],
|
||||
"views": item['view_count'],
|
||||
"thumbnail": item['thumbnail_url'],
|
||||
|
||||
@@ -104,7 +104,7 @@ def response(resp: "SXNG_Response") -> EngineResults:
|
||||
title=_remove_keyword_marker(result["Subject"]),
|
||||
content=_remove_keyword_marker(result["Text"]),
|
||||
url=result["Url"],
|
||||
publishedDate=datetime.strptime(result["Published"], "%Y-%m-%d %H:%M:%S"),
|
||||
publishedDate=datetime.fromisoformat(result["Published"]),
|
||||
metadata=gettext.gettext("Posted by {author}").format(author=result["Author"]),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -43,7 +43,7 @@ def response(resp):
|
||||
|
||||
publishedDate = None
|
||||
if recipe['submissionDate']:
|
||||
publishedDate = datetime.strptime(result['recipe']['submissionDate'][:19], "%Y-%m-%dT%H:%M:%S")
|
||||
publishedDate = datetime.fromisoformat(result['recipe']['submissionDate'][:19])
|
||||
|
||||
content = [
|
||||
f"Schwierigkeitsstufe (1-3): {recipe['difficulty']}",
|
||||
|
||||
@@ -37,7 +37,7 @@ def response(resp):
|
||||
for item in search_res:
|
||||
img = 'https://s3.thehackerblog.com/findthatmeme/' + item['image_path']
|
||||
thumb = 'https://s3.thehackerblog.com/findthatmeme/thumb/' + item.get('thumbnail', '')
|
||||
date = datetime.strptime(item["updated_at"].split("T")[0], "%Y-%m-%d")
|
||||
date = datetime.fromisoformat(item["updated_at"].split("T")[0])
|
||||
formatted_date = datetime.fromtimestamp(date.timestamp())
|
||||
|
||||
results.append(
|
||||
|
||||
@@ -47,7 +47,7 @@ def response(resp: "SXNG_Response"):
|
||||
title=result["title"],
|
||||
content=result["description"],
|
||||
thumbnail=result["smallImageURL"],
|
||||
publishedDate=datetime.strptime(result["status_since"], "%Y-%m-%d %H:%M:%S"),
|
||||
publishedDate=datetime.fromisoformat(result["status_since"]),
|
||||
metadata=f"Rank: {result['rank']} || {result['episode_count']} episodes",
|
||||
)
|
||||
)
|
||||
|
||||
@@ -91,7 +91,7 @@ def response(resp) -> EngineResults:
|
||||
|
||||
published_date = None
|
||||
try:
|
||||
published_date = datetime.strptime(entry["createdAt"], "%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
published_date = datetime.fromisoformat(entry["createdAt"].rstrip("Z"))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ def _result(video: dict[str, typing.Any], album_info: dict[str, typing.Any]):
|
||||
release_time = album_info.get("releaseTime", {}).get("value")
|
||||
if release_time:
|
||||
try:
|
||||
published_date = datetime.strptime(release_time, "%Y-%m-%d")
|
||||
published_date = datetime.fromisoformat(release_time)
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@ def _get_communities(json):
|
||||
'title': result['community']['title'],
|
||||
'content': markdown_to_text(result['community'].get('description', '')),
|
||||
'thumbnail': result['community'].get('icon', result['community'].get('banner')),
|
||||
'publishedDate': datetime.strptime(counts['published'][:19], '%Y-%m-%dT%H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(counts['published'][:19]),
|
||||
'metadata': metadata,
|
||||
}
|
||||
)
|
||||
@@ -141,7 +141,7 @@ def _get_posts(json):
|
||||
'title': result['post']['name'],
|
||||
'content': content,
|
||||
'thumbnail': thumbnail,
|
||||
'publishedDate': datetime.strptime(result['post']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(result['post']['published'][:19]),
|
||||
'metadata': metadata,
|
||||
}
|
||||
)
|
||||
@@ -170,7 +170,7 @@ def _get_comments(json):
|
||||
'url': result['comment']['ap_id'],
|
||||
'title': result['post']['name'],
|
||||
'content': markdown_to_text(result['comment']['content']),
|
||||
'publishedDate': datetime.strptime(result['comment']['published'][:19], '%Y-%m-%dT%H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(result['comment']['published'][:19]),
|
||||
'metadata': metadata,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -60,7 +60,7 @@ def response(resp):
|
||||
'title': result['username'] + f" ({result['followers_count']} followers)",
|
||||
'content': result['note'],
|
||||
'thumbnail': result.get('avatar'),
|
||||
'publishedDate': datetime.strptime(result['created_at'][:10], "%Y-%m-%d"),
|
||||
'publishedDate': datetime.fromisoformat(result['created_at'][:10]),
|
||||
}
|
||||
)
|
||||
elif mastodon_type == "hashtags":
|
||||
|
||||
@@ -44,7 +44,7 @@ def response(resp) -> EngineResults:
|
||||
|
||||
cve_id = item["cve"]["id"]
|
||||
description = item["cve"]["descriptions"][0]["value"]
|
||||
date = datetime.strptime(item["cve"]["published"], "%Y-%m-%dT%H:%M:%S.%f")
|
||||
date = datetime.fromisoformat(item["cve"]["published"])
|
||||
|
||||
# Extract severity (Low, Medium, High, or Critical) and CVSS score, if available
|
||||
info = item["cve"].get("metrics", {}).get("cvssMetricV31", [{}])[0].get("cvssData", {})
|
||||
|
||||
@@ -76,7 +76,7 @@ def response(resp):
|
||||
release_time = item["release_time"]
|
||||
duration = item["duration"]
|
||||
|
||||
release_date = datetime.strptime(release_time.split("T")[0], "%Y-%m-%d")
|
||||
release_date = datetime.fromisoformat(release_time.split("T")[0])
|
||||
formatted_date = datetime.fromtimestamp(release_date.timestamp())
|
||||
|
||||
url = f"https://odysee.com/{name}:{claim_id}"
|
||||
|
||||
@@ -54,7 +54,7 @@ def response(resp: "SXNG_Response"):
|
||||
title=result["title"],
|
||||
content=result["description"],
|
||||
thumbnail=result["image_url"],
|
||||
publishedDate=datetime.strptime(result["created_at"], "%Y-%m-%d %H:%M:%S"),
|
||||
publishedDate=datetime.fromisoformat(result["created_at"]),
|
||||
metadata=" | ".join(metadata),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -292,7 +292,7 @@ def parse_news_uchq(data):
|
||||
results = []
|
||||
for item in data.get('feed', []):
|
||||
try:
|
||||
published_date = datetime.strptime(item.get('time'), "%Y-%m-%d")
|
||||
published_date = datetime.fromisoformat(item.get('time'))
|
||||
except (ValueError, TypeError):
|
||||
# Sometime Quark will return non-standard format like "1天前", set published_date as None
|
||||
published_date = None
|
||||
|
||||
@@ -58,7 +58,7 @@ def response(resp):
|
||||
title = extract_text(result_dom.xpath(title_xpath))
|
||||
p_date = extract_text(result_dom.xpath(published_date))
|
||||
# fix offset date for line 644 webapp.py check
|
||||
fixed_date = datetime.strptime(p_date, '%Y-%m-%dT%H:%M:%S%z')
|
||||
fixed_date = datetime.fromisoformat(p_date)
|
||||
earned = extract_text(result_dom.xpath(earned_xpath))
|
||||
views = extract_text(result_dom.xpath(views_xpath))
|
||||
rumbles = extract_text(result_dom.xpath(rumbles_xpath))
|
||||
|
||||
@@ -120,7 +120,7 @@ def response(resp: "SXNG_Response") -> EngineResults:
|
||||
|
||||
publishedDate: datetime | None
|
||||
if "pubDate" in result:
|
||||
publishedDate = datetime.strptime(result["pubDate"], "%Y-%m-%d")
|
||||
publishedDate = datetime.fromisoformat(result["pubDate"])
|
||||
else:
|
||||
publishedDate = None
|
||||
|
||||
|
||||
@@ -95,7 +95,8 @@ def _parse_date(text):
|
||||
date_match = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", text)
|
||||
if date_match:
|
||||
try:
|
||||
return datetime.strptime(date_match.group(1), "%Y-%m-%d")
|
||||
y, m, d = date_match.group(1).split("-")
|
||||
return datetime(year=int(y), month=int(m), day=int(d))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
return None
|
||||
|
||||
@@ -54,7 +54,7 @@ def response(resp):
|
||||
published_date = None
|
||||
if entry.get("date") and entry.get("duration"):
|
||||
try:
|
||||
published_date = datetime.strptime(entry['date'], "%Y-%m-%d")
|
||||
published_date = datetime.fromisoformat(entry['date'])
|
||||
except (ValueError, TypeError):
|
||||
published_date = None
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ def response(resp: "SXNG_Response") -> EngineResults:
|
||||
return str(record.get(k, ""))
|
||||
|
||||
for record in json_data["records"]:
|
||||
published = datetime.strptime(record["publicationDate"], "%Y-%m-%d")
|
||||
published = datetime.fromisoformat(record["publicationDate"])
|
||||
authors: list[str] = [" ".join(author["creator"].split(", ")[::-1]) for author in record["creators"]]
|
||||
|
||||
pdf_url = ""
|
||||
|
||||
@@ -81,7 +81,7 @@ def _story(item):
|
||||
return {
|
||||
'title': item['title'],
|
||||
'thumbnail': item.get('teaserImage', {}).get('imageVariants', {}).get('16x9-256'),
|
||||
'publishedDate': datetime.strptime(item['date'][:19], '%Y-%m-%dT%H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(item['date'][:19]),
|
||||
'content': item.get('firstSentence'),
|
||||
'url': item['shareURL'] if use_source_url else item['detailsweb'],
|
||||
}
|
||||
@@ -103,7 +103,7 @@ def _video(item):
|
||||
'template': 'videos.html',
|
||||
'title': title,
|
||||
'thumbnail': item.get('teaserImage', {}).get('imageVariants', {}).get('16x9-256'),
|
||||
'publishedDate': datetime.strptime(item['date'][:19], '%Y-%m-%dT%H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(item['date'][:19]),
|
||||
'content': item.get('firstSentence', ''),
|
||||
'iframe_src': video_url,
|
||||
'url': url,
|
||||
|
||||
@@ -121,7 +121,7 @@ def parse_tineye_match(match_json):
|
||||
|
||||
crawl_date = backlink_json.get("crawl_date")
|
||||
if crawl_date:
|
||||
crawl_date = datetime.strptime(crawl_date, '%Y-%m-%d')
|
||||
crawl_date = datetime.fromisoformat(crawl_date)
|
||||
else:
|
||||
crawl_date = datetime.min
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ def response(resp):
|
||||
'title': title,
|
||||
'content': html_to_text(result['content']),
|
||||
'thumbnail': thumbnail,
|
||||
'publishedDate': datetime.strptime(result['created_at'], '%Y-%m-%d %H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(result['created_at']),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ def response(resp):
|
||||
'img_src': result['path'],
|
||||
'thumbnail_src': result['thumbs']['small'],
|
||||
'resolution': result['resolution'].replace('x', ' x '),
|
||||
'publishedDate': datetime.strptime(result['created_at'], '%Y-%m-%d %H:%M:%S'),
|
||||
'publishedDate': datetime.fromisoformat(result['created_at']),
|
||||
'img_format': result['file_type'],
|
||||
'filesize': humanize_bytes(result['file_size']),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user