Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Update with some changes
Browse files Browse the repository at this point in the history
- reorder import like
- short check if args is none handle
- check if location name & temp is none
- and changes some code
  • Loading branch information
AyraHikari committed Jun 23, 2018
1 parent d749563 commit 85581f3
Showing 1 changed file with 51 additions and 49 deletions.
100 changes: 51 additions & 49 deletions tg_bot/modules/weather.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
import pyowm
from pyowm import timeutils, exceptions
from telegram import Message, Chat, Update, Bot
from telegram.ext import run_async

from typing import Optional, List
from tg_bot import dispatcher, updater, API_WEATHER
from tg_bot.modules.disable import DisableAbleCommandHandler

from telegram import Message, Chat, Update, Bot
from telegram.ext import run_async

@run_async
def weather(bot: Bot, update: Update, args: List[str]):
if len(args) >= 1:
location = " ".join(args)
if location.lower() == bot.first_name.lower():
update.effective_message.reply_text("I will keep an eye on both happy and sad times!")
bot.send_sticker(update.effective_chat.id, BAN_STICKER)
return
def weather(bot, update, args):
if len(args) == 0:
update.effective_message.reply_text("Write a location to check the weather.")
return

try:
owm = pyowm.OWM(API_WEATHER, language='en')
observation = owm.weather_at_place(location)
theweather = observation.get_weather()
getloc = observation.get_location()
thelocation = getloc.get_name()
temperature = theweather.get_temperature(unit='celsius')['temp']
fc = owm.three_hours_forecast(location)
location = " ".join(args)
if location.lower() == bot.first_name.lower():
update.effective_message.reply_text("I will keep an eye on both happy and sad times!")
bot.send_sticker(update.effective_chat.id, BAN_STICKER)
return

# Weather symbols
status = ""
cuacaskrg = theweather.get_weather_code()
if cuacaskrg < 232: # Rain storm
status += "⛈️ "
elif cuacaskrg < 321: # Drizzle
status += "🌧️ "
elif cuacaskrg < 504: # Light rain
status += "🌦️ "
elif cuacaskrg < 531: # Cloudy rain
status += "⛈️ "
elif cuacaskrg < 622: # Snow
status += "🌨️ "
elif cuacaskrg < 781: # Atmosphere
status += "🌪️ "
elif cuacaskrg < 800: # Bright
status += "🌤️ "
elif cuacaskrg < 801: # A little cloudy
status += "⛅️ "
elif cuacaskrg < 804: # Cloudy
status += "☁️ "
status += theweather._detailed_status
try:
owm = pyowm.OWM(API_WEATHER)
observation = owm.weather_at_place(location)
getloc = observation.get_location()
thelocation = getloc.get_name()
if thelocation == None:
thelocation = "Unknown"
theweather = observation.get_weather()
temperature = theweather.get_temperature(unit='celsius').get('temp')
if temperature == None:
temperature = "Unknown"

# Weather symbols
status = ""
status_now = theweather.get_weather_code()
if status_now < 232: # Rain storm
status += "⛈️ "
elif status_now < 321: # Drizzle
status += "🌧️ "
elif status_now < 504: # Light rain
status += "🌦️ "
elif status_now < 531: # Cloudy rain
status += "⛈️ "
elif status_now < 622: # Snow
status += "🌨️ "
elif status_now < 781: # Atmosphere
status += "🌪️ "
elif status_now < 800: # Bright
status += "🌤️ "
elif status_now < 801: # A little cloudy
status += "⛅️ "
elif status_now < 804: # Cloudy
status += "☁️ "
status += theweather._detailed_status


update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(thelocation,
status, temperature))
update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(thelocation,
status, temperature))

except pyowm.exceptions.not_found_error.NotFoundError:
update.effective_message.reply_text("Sorry, location not found.")
else:
update.effective_message.reply_text("Write a location to check the weather.")
except pyowm.exceptions.not_found_error.NotFoundError:
update.effective_message.reply_text("Sorry, location not found.")


__help__ = """
Expand All @@ -65,6 +67,6 @@ def weather(bot: Bot, update: Update, args: List[str]):

__mod_name__ = "Weather"

CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True)
WEATHER_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True)

dispatcher.add_handler(CUACA_HANDLER)
dispatcher.add_handler(WEATHER_HANDLER)

0 comments on commit 85581f3

Please sign in to comment.