82 lines
3.3 KiB
Python
82 lines
3.3 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, RSS_URL_POSTS
|
||
from utils.cat import fetch_cat
|
||
|
||
|
||
async def morning(stop_event, bot):
|
||
"""Вывести погоду, лучшие статьи за сутки и котика"""
|
||
api_url = "https://wttr.in/Magnitogorsk?format=j1&lang=ru"
|
||
|
||
# Параллельный запрос погоды, новостей и котика
|
||
weather_data, articles, posts, cat_url = await asyncio.gather(
|
||
fetch_weather(api_url),
|
||
fetch_rss(RSS_URL_ARTICLES),
|
||
fetch_rss(RSS_URL_POSTS),
|
||
fetch_cat(),
|
||
)
|
||
|
||
print("Доброе утро!\n")
|
||
|
||
# --- Котик ---
|
||
if cat_url:
|
||
print(f"Котик: {cat_url}\n")
|
||
else:
|
||
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("Не удалось получить новости.")
|
||
|
||
print()
|
||
|
||
# --- Новости: посты ---
|
||
if posts is not None:
|
||
if posts:
|
||
lines = format_articles(posts,
|
||
"Лучшие новости за сутки / Искусственный интеллект / Хабr",
|
||
"https://habr.com/ru/hubs/artificial_intelligence/news/top/daily/")
|
||
print("\n".join(lines))
|
||
else:
|
||
print("Новостей пока нет.")
|
||
else:
|
||
print("Не удалось получить новости.")
|