import pyowm
from pyowm import timeutils, exceptions
from telegram import Message, Chat, Update, Bot
from telegram.ext import run_async
from tg_bot import dispatcher, updater, BAN_STICKER, API_WEATHER
from tg_bot.modules.disable import DisableAbleCommandHandler
@run_async
def weather(bot, update, args):
if len(args) == 0:
update.effective_message.reply_text("Напишите место, чтобы узнать погоду. \nПример: /weather Чикаго")
return
location = " ".join(args)
if location.lower() == bot.first_name.lower():
update.effective_message.reply_text("Буду следить и за счастливыми, и за печальными моментами!")
bot.send_sticker(update.effective_chat.id, BAN_STICKER)
return
try:
owm = pyowm.OWM(API_WEATHER, language='ru')
observation = owm.weather_at_place(location)
getloc = observation.get_location()
thelocation = getloc.get_name()
if thelocation == None:
thelocation = "Неизвестно"
theweather = observation.get_weather()
temperature = theweather.get_temperature(unit='celsius').get('temp')
mini = theweather.get_temperature(unit='celsius').get('temp_min')
maxi = theweather.get_temperature(unit='celsius').get('temp_max')
wind = theweather.get_wind().get('speed')
humidity = theweather.get_humidity()
if temperature == None:
temperature = "Неизвестно"
if mini == None:
mini = "Неизвестно"
if maxi == None:
maxi = "Неизвестно"
if humidity == None:
humidity = "Неизвестно"
if wind == None:
wind = "Неизвестно"
status = theweather._detailed_status
icon = theweather.get_weather_code()
if icon < 232:
icon = "⛈️ "
elif icon < 321:
icon = "?️ "
elif icon < 504:
icon = "?️ "
elif icon < 531:
icon = "⛈️ "
elif icon < 622:
icon = "?️ "
elif icon < 781:
icon = "?️ "
elif icon < 800:
icon = "?️ "
elif icon < 801:
icon = "⛅️ "
elif icon < 805:
icon = "☁️ "
update.message.reply_text("? Погода в городе {}, \n{}Сейчас {},\n? Около: {} °C, \n\
? Минимальная: {} °C, \n? Максимальная: {} °C,\n\
? Скорость ветра: {} м/с, \n? Влажность: {}%".format(thelocation, icon,
status, temperature, mini, maxi, wind, humidity))
except pyowm.exceptions.api_response_error.NotFoundError:
update.effective_message.reply_text("Извините, местоположение " + str(location) + " не найдено.")
__help__ = """
- /weather <город>: Получить информацию о погоде в определенном месте
"""
__mod_name__ = "Погода"
WEATHER_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True)
dispatcher.add_handler(WEATHER_HANDLER)