Answer the question
In order to leave comments, you need to log in
How to add here exceptions in the weather?
@dp.message_handler(commands=["start"])
async def start_command(message: types.Message):
await message.reply("Привет! Напиши мне название города и я пришлю сводку погоды!")
@dp.message_handler()
async def get_weather(message: types.Message):
code_to_smile = {
"Clear": "Ясно \U00002600",
"Clouds": "Облачно \U00002601",
"Rain": "Дождь \U00002614",
"Drizzle": "Дождь \U00002614",
"Thunderstorm": "Гроза \U000026A1",
"Snow": "Снег \U0001F328",
"Mist": "Туман \U0001F32B"
}
try:
r = requests.get(
f"http://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
)
data = r.json()
city = data["name"]
cur_weather = data["main"]["temp"]
weather_description = data["weather"][0]["main"]
if weather_description in code_to_smile:
wd = code_to_smile[weather_description]
else:
wd = "Посмотри в окно, не пойму что там за погода!"
humidity = data["main"]["humidity"]
pressure = data["main"]["pressure"]
wind = data["wind"]["speed"]
sunrise_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
sunset_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
length_of_the_day = datetime.datetime.fromtimestamp(data["sys"]["sunset"]) - datetime.datetime.fromtimestamp(
data["sys"]["sunrise"])
await message.reply(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
f"Погода в городе: {city}\nТемпература: {cur_weather}C° {wd}\n"
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\nВетер: {wind} м/с\n"
f"Восход солнца: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n"
f"***Хорошего дня!***"
)
except:
await message.reply("\U00002620 Проверьте название города \U00002620")
Answer the question
In order to leave comments, you need to log in
if(int(cur_weather) >20):
cur_weather=cur_weather + "очень жарко"
weather_stat = ""
if(int(cur_weather) >20):
weather_stat = "Очень жарко"
...
await message.reply(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
f"Погода в городе: {city}\nТемпература: {cur_weather}C° {wd}" + weather_stat + "\n"
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\nВетер: {wind} м/с\n"
f"Восход солнца: {sunrise_timestamp}\nЗакат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n"
f"***Хорошего дня!***"
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question