59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
import asyncio
|
||
|
||
from utils.pogoda import fetch_weather, pressure_to_mmhg, translate_weather
|
||
from utils.news import fetch_rss, format_articles, RSS_URL_ARTICLES
|
||
|
||
|
||
async def morning(stop_event, bot):
|
||
"""Вывести погоду и лучшие статьи за сутки"""
|
||
api_url = "https://wttr.in/Magnitogorsk?format=j1&lang=ru"
|
||
|
||
# Параллельный запрос погоды и новостей
|
||
weather_data, articles = await asyncio.gather(
|
||
fetch_weather(api_url),
|
||
fetch_rss(RSS_URL_ARTICLES),
|
||
)
|
||
|
||
print("Доброе утро!\n")
|
||
|
||
# --- Погода ---
|
||
if weather_data is not None:
|
||
current = weather_data.get("current_condition", [{}])[0]
|
||
if current:
|
||
temp = current.get("temp_C", "—")
|
||
feels_like = current.get("FeelsLikeC", "—")
|
||
description = translate_weather(current.get("weatherDesc", [{}])[0].get("value", "—"))
|
||
humidity = current.get("humidity", "—")
|
||
wind_kmh = current.get("windspeedKmph", "—")
|
||
try:
|
||
wind = round(int(wind_kmh) / 3.6, 1) if wind_kmh != "—" else "—"
|
||
except (ValueError, TypeError):
|
||
wind = "—"
|
||
pressure_mb = current.get("pressure", "—")
|
||
pressure_mm = pressure_to_mmhg(pressure_mb)
|
||
|
||
print(f"**Погода в Магнитогорске:**")
|
||
print(f"Температура: {temp}°C (ощущается как {feels_like}°C)")
|
||
print(f"Описание: {description}")
|
||
print(f"Влажность: {humidity}%")
|
||
print(f"Ветер: {wind} м/с")
|
||
print(f"Давление: {pressure_mm} мм рт. ст.")
|
||
else:
|
||
print("Не удалось получить данные о погоде.")
|
||
else:
|
||
print("Не удалось получить данные о погоде.")
|
||
|
||
print()
|
||
|
||
# --- Новости ---
|
||
if articles is not None:
|
||
if articles:
|
||
lines = format_articles(articles,
|
||
"Лучшие статьи за сутки / Искусственный интеллект / Хабr",
|
||
"https://habr.com/ru/hubs/artificial_intelligence/articles/top/daily/")
|
||
print("\n".join(lines))
|
||
else:
|
||
print("Новостей пока нет.")
|
||
else:
|
||
print("Не удалось получить новости.")
|