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

Add Weather module #31

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

AyraHikari
Copy link

No description provided.

@AyraHikari AyraHikari changed the title Added Wheater module Added Weather module Jun 23, 2018
Copy link
Owner

@PaulSonOfLars PaulSonOfLars left a 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 :)

from telegram.ext import run_async

@run_async
def cuaca(bot: Bot, update: Update, args: List[str]):
Copy link
Owner

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

return

try:
bot.sendChatAction(update.effective_chat.id, "typing") # Bot typing before send message
Copy link
Owner

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.

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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary else

cuacanya = observation.get_weather()
obs = owm.weather_at_place(location)
lokasi = obs.get_location()
lokasinya = lokasi.get_name()
Copy link
Owner

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.

obs = owm.weather_at_place(location)
lokasi = obs.get_location()
lokasinya = lokasi.get_name()
# statusnya = cuacanya._detailed_status
Copy link
Owner

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)
Copy link
Owner

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

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.")
Copy link
Owner

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?

@@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*weather, not wheater

@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*weather

@@ -98,6 +99,7 @@
WORKERS = Config.WORKERS
BAN_STICKER = Config.BAN_STICKER
ALLOW_EXCL = Config.ALLOW_EXCL
API_WEATHER = Config.API_OPENWHEATER
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*weather

@AyraHikari
Copy link
Author

I think all fixed in new commit


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

Copy link
Owner

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


@run_async
def weather(bot: Bot, update: Update, args: List[str]):
if len(args) >= 1:
Copy link
Owner

@PaulSonOfLars PaulSonOfLars Jun 23, 2018

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.

return

try:
owm = pyowm.OWM(API_WEATHER, language='en')
Copy link
Owner

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.

observation = owm.weather_at_place(location)
theweather = observation.get_weather()
getloc = observation.get_location()
thelocation = getloc.get_name()
Copy link
Owner

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.

Copy link
Author

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

theweather = observation.get_weather()
getloc = observation.get_location()
thelocation = getloc.get_name()
temperature = theweather.get_temperature(unit='celsius')['temp']
Copy link
Owner

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()

status += "☁️ "
status += theweather._detailed_status


Copy link
Owner

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.


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

dispatcher.add_handler(CUACA_HANDLER)
Copy link
Owner

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


__mod_name__ = "Weather"

CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True)
Copy link
Owner

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
@AyraHikari AyraHikari changed the title Added Weather module Add Weather module Sep 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants