From b22b02d65efce0e69285c635e935bb4fd9dc592c Mon Sep 17 00:00:00 2001 From: deadzilla Date: Sat, 11 Jul 2026 23:55:18 +0500 Subject: [PATCH] =?UTF-8?q?fix:=20=5Fparse=5Fdate=20=D0=B2=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B4=D0=B8=D1=80=D1=83=D0=B5=D1=82=20=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=20=D0=B4=D0=B0=D1=82=D1=8B=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20regex=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=82=D0=B0=20?= =?UTF-8?q?=D0=BC=D1=83=D1=81=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_format_articles.py | 15 +++++++-------- utils/news.py | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/test_format_articles.py b/tests/test_format_articles.py index d9f236e..882e562 100644 --- a/tests/test_format_articles.py +++ b/tests/test_format_articles.py @@ -48,9 +48,10 @@ class TestParseDate: [ ("Mon, 28 May 2026 10:00:00 +0000", "28.05.2026"), ("Mon, 28 May 2026 10:00:00 GMT", "28.05.2026"), - ("2026-05-28T10:00:00Z", "2026.05.28"), - ("2026-12-31T23:59:59Z", "2026.12.31"), - ("2026-01-01T00:00:00Z", "2026.01.01"), + ("2026-05-28T10:00:00Z", "28.05.2026"), + ("2026-12-31T23:59:59Z", "31.12.2026"), + ("2026-01-01T00:00:00Z", "01.01.2026"), + ("2026-05-28", "28.05.2026"), ], ) def test_parse_date_known(self, pub_date, expected) -> None: @@ -69,11 +70,9 @@ class TestParseDate: assert _parse_date(pub_date) == expected def test_parse_date_invalid(self) -> None: - """Невалидная дата должна вернуть первые 10 символов.""" + """Невалидная дата должна вернуть пустую строку.""" result = _parse_date("invalid-date-string") - assert ( - result == "invalid.da" - ) # первые 10 символов: 'invalid-da' → 'invalid.da' (replace('-','.')) + assert result == "" class TestFormatArticles: @@ -205,7 +204,7 @@ class TestFormatArticles: }, ] result = format_articles(articles, "Заголовок", "https://habr.com/feed") - assert result[1] == "Статья\n2026.05.28 " + assert result[1] == "Статья\n28.05.2026 " def test_format_articles_empty_date(self) -> None: """Пустая дата должна быть пустой строкой.""" diff --git a/utils/news.py b/utils/news.py index 89a4e04..78f8c8b 100644 --- a/utils/news.py +++ b/utils/news.py @@ -1,5 +1,6 @@ import asyncio import logging +import re from datetime import datetime from typing import Optional @@ -82,7 +83,13 @@ async def fetch_rss(url: str) -> Optional[list[dict]]: def _parse_date(pub_date: Optional[str]) -> str: - """Парсить дату из RSS в строку 'дд.мм.гггг' или вернуть часть даты.""" + """Парсить дату из RSS в строку 'дд.мм.гггг'. + + Поддерживает: + - RFC 822: "Mon, 01 Jan 2024 12:00:00 GMT" + - ISO 8601: "2024-01-01T12:00:00+00:00" или "2024-01-01" + Возвращает пустую строку, если формат не распознан. + """ if not pub_date: return "" try: @@ -90,7 +97,12 @@ def _parse_date(pub_date: Optional[str]) -> str: dt = datetime.strptime(d, "%a, %d %b %Y %H:%M:%S %z") return dt.strftime("%d.%m.%Y") except ValueError: - return pub_date[:10].replace("-", ".") + pass + # Fallback: YYYY-MM-DD или YYYY-MM-DDT... + match = re.match(r"(\d{4})-(\d{2})-(\d{2})", pub_date) + if match: + return f"{match.group(3)}.{match.group(2)}.{match.group(1)}" + return "" def truncate_title(title: str, max_len: int = 60) -> str: