From 7097a266c6d9d5adec10f7f890eed76608e04342 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Fri, 22 Sep 2017 03:53:06 +0000 Subject: [PATCH] Weather --- app/dragon-bot.py | 7 +++++++ app/help_methods.py | 5 ++++- app/weather.py | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/dragon-bot.py b/app/dragon-bot.py index 113078bd..71462400 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -20,6 +20,7 @@ import help_methods import lewds import role_check import wallpaper +import weather import wolfram # Client object @@ -117,6 +118,12 @@ async def on_message(message): lewds.get_lewd(channel_name=message.channel.name) ) + if message.content.startswith('!weather'): + await client.send_message( + message.channel, + weather.get_weather(message.content) + ) + if message.content.startswith('!purge'): num = 20 if len(message.content.split()) > 1: diff --git a/app/help_methods.py b/app/help_methods.py index 647e3731..9c3b9fcf 100644 --- a/app/help_methods.py +++ b/app/help_methods.py @@ -59,7 +59,10 @@ def get_help_message(method): 'This function now outputs the SWEET-ASS picture of the day.', ' Note this picture only changes once a day.', '\nUsage: !homepage' - ] + ], + 'weather': [ + 'Returns the weather for the location you entered.', + '\nUsage: !weather Berkeley'] } # Print out every help method diff --git a/app/weather.py b/app/weather.py index 311ae871..233766cb 100644 --- a/app/weather.py +++ b/app/weather.py @@ -7,8 +7,19 @@ emojis = {'clear sky': ':sunny:', 'scattered clouds': ':partly_sunny:', 'light rain': ':white_sun_rain_cloud:', 'overcast clouds': ':cloud:', 'broken clouds': ':white_sun_cloud:', 'moderate rain': ':cloud_with_rain:', 'light snow': ':cloud_snow:', 'few clouds': ':white_sun_small_cloud:'} +openweathermap = OWM('593e0e182f9278a10443da354c4014db') -def get_weather(location): +def parse_loc(message): + if len(message.split()) > 1: + try: + res = get_weather(' '.join(message.split()[1:])) + except exceptions.api_call_error.APICallError: + res = '```Please input a valid location: !weather [location]```' + return res + return '```Please input a valid location: !weather [location]```' + + +def get_weather(message): """ get_weather(location) @@ -18,8 +29,7 @@ def get_weather(location): TODO: Replace basic code block with nice looking, bold text w/ emojis """ try: - openweathermap = OWM('593e0e182f9278a10443da354c4014db') - forecaster = openweathermap.three_hours_forecast(location) + forecaster = openweathermap.three_hours_forecast(message) forecast = forecaster.get_forecast() reg = forecast.get(0)