-
Notifications
You must be signed in to change notification settings - Fork 916
Add Weather module #31
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ sqlalchemy | |
python-telegram-bot>=10.1.0 | ||
psycopg2-binary | ||
feedparser | ||
pyowm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pyowm | ||
from pyowm import timeutils, exceptions | ||
|
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. early returns are preferred over increased indents.
|
||
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 | ||
|
||
try: | ||
owm = pyowm.OWM(API_WEATHER, language='en') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given that this is constant, you might want to consider making it a global variable - no need to instantiate it every time the function is called. |
||
observation = owm.weather_at_place(location) | ||
theweather = observation.get_weather() | ||
getloc = observation.get_location() | ||
thelocation = getloc.get_name() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as mentioned in last review, try to chain these if youre 100% sure they wont return None. if there is a chance of None, add checks for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If location not found, it will give exception pyowm.exceptions.not_found_error.NotFoundError |
||
temperature = theweather.get_temperature(unit='celsius')['temp'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. youre 100% sure that "temp" is in that dict, no chance of a KeyError? if there is, consider using |
||
fc = owm.three_hours_forecast(location) | ||
|
||
# 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 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8: too much whitespace; double newlines should not be found in a function body. Remove one. |
||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary else |
||
update.effective_message.reply_text("Write a location to check the weather.") | ||
|
||
|
||
__help__ = """ | ||
- /weather <city>: get weather info in a particular place | ||
""" | ||
|
||
__mod_name__ = "Weather" | ||
|
||
CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cuaca -> weather |
||
|
||
dispatcher.add_handler(CUACA_HANDLER) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: add a newline at EOF, for PEP8 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a newline at EOF |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ class Config(object): | |
SQLALCHEMY_DATABASE_URI = 'sqldbtype://username:pw@hostname:port/db_name' # needed for any database modules | ||
MESSAGE_DUMP = None # needed to make sure 'save from' messages persist | ||
LOAD = [] | ||
NO_LOAD = ['translation', 'rss'] | ||
NO_LOAD = ['translation', 'rss', 'weather'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
WEBHOOK = False | ||
URL = None | ||
|
||
|
@@ -34,6 +34,7 @@ class Config(object): | |
WORKERS = 8 # Number of subthreads to use. This is the recommended amount - see for yourself what works best! | ||
BAN_STICKER = 'CAADAgADOwADPPEcAXkko5EB3YGYAg' # banhammer marie sticker | ||
ALLOW_EXCL = False # Allow ! commands as well as / | ||
API_OPENWEATHER = None # OpenWeather API | ||
|
||
|
||
class Production(Config): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply PEP8 import order