55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from app.homelab.openmeteo import OpenMeteoClient, format_weather_snapshot
|
|
from app.homelab.rss import RssClient
|
|
|
|
|
|
def build_morning_digest(db: Session, *, include_news: bool = True) -> str:
|
|
del db # timezone resolved via weather client / profile in future extensions
|
|
weather_client = OpenMeteoClient()
|
|
weather = weather_client.fetch_current_and_hourly(hours_ahead=12)
|
|
|
|
lines = ["🌤 **Утренний дайджест**", ""]
|
|
|
|
if weather.get("ok"):
|
|
cur = weather.get("current") or {}
|
|
lines.append(
|
|
f"**Погода ({weather.get('location')})**: "
|
|
f"{cur.get('temperature_c')}°C, {cur.get('conditions')}, "
|
|
f"ветер {cur.get('wind_speed_kmh')} км/ч."
|
|
)
|
|
lines.append(weather_client.rain_summary(hours_ahead=12))
|
|
else:
|
|
lines.append(f"**Погода**: недоступна ({weather.get('error', 'ошибка')}).")
|
|
|
|
if include_news:
|
|
headlines = RssClient().fetch_headlines(limit=7)
|
|
lines.append("")
|
|
if headlines:
|
|
lines.append("**Новости:**")
|
|
for item in headlines:
|
|
title = item.get("title", "")
|
|
link = item.get("link", "")
|
|
source = item.get("source", "")
|
|
if link:
|
|
lines.append(f"- [{title}]({link}) — {source}")
|
|
else:
|
|
lines.append(f"- {title} — {source}")
|
|
else:
|
|
lines.append("**Новости**: ленты временно недоступны.")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
def build_weather_briefing(hours_ahead: int = 12, include_news: bool = False) -> dict:
|
|
client = OpenMeteoClient()
|
|
weather = client.fetch_current_and_hourly(hours_ahead=hours_ahead)
|
|
result = {
|
|
"weather": weather,
|
|
"rain_summary": client.rain_summary(hours_ahead=hours_ahead) if weather.get("ok") else "",
|
|
"context": format_weather_snapshot(weather),
|
|
}
|
|
if include_news:
|
|
result["news"] = RssClient().fetch_headlines(limit=7)
|
|
return result
|