added api

This commit is contained in:
2026-06-10 10:29:21 +03:00
parent d0bdd1e95c
commit 73baf4dbe1
26 changed files with 1201 additions and 6 deletions
+54
View File
@@ -0,0 +1,54 @@
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