-
Notifications
You must be signed in to change notification settings - Fork 917
Add Weather module #31
base: master
Are you sure you want to change the base?
Conversation
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.
Looks good, but make sure all comments and variable names are in English, to keep the project consistent :)
tg_bot/modules/weather.py
Outdated
from telegram.ext import run_async | ||
|
||
@run_async | ||
def cuaca(bot: Bot, update: Update, args: List[str]): |
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.
English variables please - keep it consistent
tg_bot/modules/weather.py
Outdated
return | ||
|
||
try: | ||
bot.sendChatAction(update.effective_chat.id, "typing") # Bot typing before send message |
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.
I'm against sending chat actions, as it's unnecessary and causes extra api calls; it just slows down the bot.
tg_bot/modules/weather.py
Outdated
update.effective_message.reply_text("Sorry, location not found.") | ||
except pyowm.exceptions.api_call_error.APICallError: | ||
update.effective_message.reply_text("Write a location to check the weather.") | ||
else: |
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.
Unnecessary else
tg_bot/modules/weather.py
Outdated
cuacanya = observation.get_weather() | ||
obs = owm.weather_at_place(location) | ||
lokasi = obs.get_location() | ||
lokasinya = lokasi.get_name() |
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.
Are the above lines guaranteed not to return None
?
If yes, chain them up so there's only only necessary variable allocations.
If no, add checks for that.
tg_bot/modules/weather.py
Outdated
obs = owm.weather_at_place(location) | ||
lokasi = obs.get_location() | ||
lokasinya = lokasi.get_name() | ||
# statusnya = cuacanya._detailed_status |
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.
Nit: remove commented out code
|
||
@run_async | ||
def cuaca(bot: Bot, update: Update, args: List[str]): | ||
location = " ".join(args) |
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.
If len(args) == 0:
inform user to give a location and then return
tg_bot/modules/weather.py
Outdated
except pyowm.exceptions.not_found_error.NotFoundError: | ||
update.effective_message.reply_text("Sorry, location not found.") | ||
except pyowm.exceptions.api_call_error.APICallError: | ||
update.effective_message.reply_text("Write a location to check the weather.") |
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.
Does APICallError only occur when no location is specified...?
I believe the length check should fix that, so maybe make this reply more generic?
tg_bot/sample_config.py
Outdated
@@ -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_OPENWHEATER = None # OpenWeather API |
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.
*weather, not wheater
tg_bot/__init__.py
Outdated
@@ -57,6 +57,7 @@ | |||
WORKERS = int(os.environ.get('WORKERS', 8)) | |||
BAN_STICKER = os.environ.get('BAN_STICKER', 'CAADAgADOwADPPEcAXkko5EB3YGYAg') | |||
ALLOW_EXCL = os.environ.get('ALLOW_EXCL', False) | |||
API_WEATHER = os.environ.get('API_OPENWHEATER', None) |
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.
*weather
tg_bot/__init__.py
Outdated
@@ -98,6 +99,7 @@ | |||
WORKERS = Config.WORKERS | |||
BAN_STICKER = Config.BAN_STICKER | |||
ALLOW_EXCL = Config.ALLOW_EXCL | |||
API_WEATHER = Config.API_OPENWHEATER |
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.
*weather
I think all fixed in new commit |
tg_bot/modules/weather.py
Outdated
|
||
from telegram import Message, Chat, Update, Bot | ||
from telegram.ext import run_async | ||
|
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
- standard library
- third party
- local
tg_bot/modules/weather.py
Outdated
|
||
@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 comment
The reason will be displayed to describe this comment to others. Learn more.
early returns are preferred over increased indents.
if len(args) == 0:
# reply that a location is needed
return
# do other stuff
# no else needed, since returned
# less indent = easier to follow
```
This reduces indentation, and makes code easier to read and understand.
tg_bot/modules/weather.py
Outdated
return | ||
|
||
try: | ||
owm = pyowm.OWM(API_WEATHER, language='en') |
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.
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.
tg_bot/modules/weather.py
Outdated
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 comment
The 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 comment
The 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
tg_bot/modules/weather.py
Outdated
theweather = observation.get_weather() | ||
getloc = observation.get_location() | ||
thelocation = getloc.get_name() | ||
temperature = theweather.get_temperature(unit='celsius')['temp'] |
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.
youre 100% sure that "temp" is in that dict, no chance of a KeyError? if there is, consider using .get()
tg_bot/modules/weather.py
Outdated
status += "☁️ " | ||
status += theweather._detailed_status | ||
|
||
|
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.
PEP8: too much whitespace; double newlines should not be found in a function body. Remove one.
tg_bot/modules/weather.py
Outdated
|
||
CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True) | ||
|
||
dispatcher.add_handler(CUACA_HANDLER) |
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.
Need a newline at EOF
tg_bot/modules/weather.py
Outdated
|
||
__mod_name__ = "Weather" | ||
|
||
CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True) |
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.
cuaca -> weather
- reorder import like - short check if args is none handle - check if location name & temp is none - and changes some code
8a20971
to
85581f3
Compare
No description provided.