initial commit
This commit is contained in:
76
app/services/newsletter.py
Normal file
76
app/services/newsletter.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from datetime import datetime
|
||||
from html import escape
|
||||
from typing import Any
|
||||
|
||||
|
||||
def filter_articles(
|
||||
articles: list[dict[str, Any]],
|
||||
category_filter: str | None = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
if not category_filter:
|
||||
return articles
|
||||
wanted = category_filter.strip().lower()
|
||||
return [
|
||||
a
|
||||
for a in articles
|
||||
if any(c.lower() == wanted for c in a.get("categories", []))
|
||||
]
|
||||
|
||||
|
||||
def create_plain_text(articles: list[dict[str, Any]], title: str) -> str:
|
||||
lines = [title, "=" * len(title), ""]
|
||||
if not articles:
|
||||
lines.append("Keine Artikel im gewählten Zeitraum gefunden.")
|
||||
return "\n".join(lines)
|
||||
|
||||
for idx, item in enumerate(articles, start=1):
|
||||
stamp = _format_ts(item.get("timestamp"))
|
||||
cat = ", ".join(item.get("categories", [])) or "Keine Kategorie"
|
||||
comment = item.get("comment") or "-"
|
||||
lines.extend(
|
||||
[
|
||||
f"{idx}. {item.get('title', 'Ohne Titel')}",
|
||||
f" Zeit: {stamp}",
|
||||
f" Benutzer: {item.get('user', '-')}",
|
||||
f" Kategorien: {cat}",
|
||||
f" Kommentar: {comment}",
|
||||
"",
|
||||
]
|
||||
)
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def create_html(articles: list[dict[str, Any]], title: str) -> str:
|
||||
if not articles:
|
||||
return f"<h2>{escape(title)}</h2><p>Keine Artikel im gewählten Zeitraum gefunden.</p>"
|
||||
|
||||
blocks = [f"<h2>{escape(title)}</h2>", "<ul>"]
|
||||
for item in articles:
|
||||
article_title = escape(item.get("title", "Ohne Titel"))
|
||||
stamp = escape(_format_ts(item.get("timestamp")))
|
||||
user = escape(item.get("user", "-"))
|
||||
cat = escape(", ".join(item.get("categories", [])) or "Keine Kategorie")
|
||||
comment = escape(item.get("comment") or "-")
|
||||
blocks.append(
|
||||
(
|
||||
"<li>"
|
||||
f"<strong>{article_title}</strong><br>"
|
||||
f"Zeit: {stamp}<br>"
|
||||
f"Benutzer: {user}<br>"
|
||||
f"Kategorien: {cat}<br>"
|
||||
f"Kommentar: {comment}"
|
||||
"</li>"
|
||||
)
|
||||
)
|
||||
blocks.append("</ul>")
|
||||
return "\n".join(blocks)
|
||||
|
||||
|
||||
def _format_ts(value: str | None) -> str:
|
||||
if not value:
|
||||
return "-"
|
||||
try:
|
||||
dt = datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
|
||||
return dt.strftime("%d.%m.%Y %H:%M UTC")
|
||||
except ValueError:
|
||||
return value
|
||||
Reference in New Issue
Block a user