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_forecast(hours_ahead=12, days_ahead=7) 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, daily=weather.get("daily"))) daily = weather_client.daily_summary(days_ahead=7) if daily: lines.append(f"**На неделю**: {daily}") 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, days_ahead: int = 7, include_news: bool = False) -> dict: client = OpenMeteoClient() weather = client.fetch_forecast(hours_ahead=hours_ahead, days_ahead=days_ahead) result = { "weather": weather, "rain_summary": client.rain_summary(hours_ahead=hours_ahead, daily=weather.get("daily")) if weather.get("ok") else "", "daily_summary": client.daily_summary(days_ahead=days_ahead) if weather.get("ok") else "", "context": format_weather_snapshot(weather), } if include_news: result["news"] = RssClient().fetch_headlines(limit=7) return result