From 63505072fca05d2def69438d96cebc4f334ac14e Mon Sep 17 00:00:00 2001 From: Ayra Hikari <36266025+AyraHikari@users.noreply.github.com> Date: Sat, 23 Jun 2018 19:37:25 +0700 Subject: [PATCH 1/5] Added Wheater module --- requirements.txt | 1 + tg_bot/__init__.py | 2 ++ tg_bot/modules/weather.py | 74 +++++++++++++++++++++++++++++++++++++++ tg_bot/sample_config.py | 3 +- 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tg_bot/modules/weather.py diff --git a/requirements.txt b/requirements.txt index 0082b9dee..377098d1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ sqlalchemy python-telegram-bot>=10.1.0 psycopg2-binary feedparser +pyowm diff --git a/tg_bot/__init__.py b/tg_bot/__init__.py index fc4e687a5..c3699f2c8 100644 --- a/tg_bot/__init__.py +++ b/tg_bot/__init__.py @@ -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) else: from tg_bot.config import Development as Config @@ -98,6 +99,7 @@ WORKERS = Config.WORKERS BAN_STICKER = Config.BAN_STICKER ALLOW_EXCL = Config.ALLOW_EXCL + API_WEATHER = Config.API_OPENWHEATER SUDO_USERS.add(OWNER_ID) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py new file mode 100644 index 000000000..9f00dbf7f --- /dev/null +++ b/tg_bot/modules/weather.py @@ -0,0 +1,74 @@ +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 cuaca(bot: Bot, update: Update, args: List[str]): + 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: + bot.sendChatAction(update.effective_chat.id, "typing") # Bot typing before send message + owm = pyowm.OWM(API_WEATHER, language='en') + observation = owm.weather_at_place(location) + cuacanya = observation.get_weather() + obs = owm.weather_at_place(location) + lokasi = obs.get_location() + lokasinya = lokasi.get_name() + # statusnya = cuacanya._detailed_status + temperatur = cuacanya.get_temperature(unit='celsius')['temp'] + fc = owm.three_hours_forecast(location) + + # Simbol cuaca + statusnya = "" + cuacaskrg = cuacanya.get_weather_code() + if cuacaskrg < 232: # Hujan badai + statusnya += "⛈️ " + elif cuacaskrg < 321: # Gerimis + statusnya += "🌧️ " + elif cuacaskrg < 504: # Hujan terang + statusnya += "🌦️ " + elif cuacaskrg < 531: # Hujan berawan + statusnya += "⛈️ " + elif cuacaskrg < 622: # Bersalju + statusnya += "🌨️ " + elif cuacaskrg < 781: # Atmosfer + statusnya += "🌪️ " + elif cuacaskrg < 800: # Cerah + statusnya += "🌤️ " + elif cuacaskrg < 801: # Sedikit berawan + statusnya += "⛅️ " + elif cuacaskrg < 804: # Berawan + statusnya += "☁️ " + statusnya += cuacanya._detailed_status + + + update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(lokasinya, + statusnya, temperatur)) + + 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.") + else: + return + + +__help__ = """ + - /weather : get weather info in a particular place +""" + +__mod_name__ = "Weather" + +CUACA_HANDLER = DisableAbleCommandHandler("weather", cuaca, pass_args=True) + +dispatcher.add_handler(CUACA_HANDLER) \ No newline at end of file diff --git a/tg_bot/sample_config.py b/tg_bot/sample_config.py index a6b7c2293..71cf17647 100644 --- a/tg_bot/sample_config.py +++ b/tg_bot/sample_config.py @@ -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'] 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_OPENWHEATER = None # OpenWeather API class Production(Config): From d7495635b89f1c8c066f712eab40d757be45296e Mon Sep 17 00:00:00 2001 From: Ayra Hikari <36266025+AyraHikari@users.noreply.github.com> Date: Sat, 23 Jun 2018 20:36:02 +0700 Subject: [PATCH 2/5] Change comment and some fix for weather module --- tg_bot/__init__.py | 4 +- tg_bot/modules/weather.py | 92 +++++++++++++++++++-------------------- tg_bot/sample_config.py | 2 +- 3 files changed, 47 insertions(+), 51 deletions(-) diff --git a/tg_bot/__init__.py b/tg_bot/__init__.py index c3699f2c8..2b4ea2614 100644 --- a/tg_bot/__init__.py +++ b/tg_bot/__init__.py @@ -57,7 +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) + API_WEATHER = os.environ.get('API_OPENWEATHER', None) else: from tg_bot.config import Development as Config @@ -99,7 +99,7 @@ WORKERS = Config.WORKERS BAN_STICKER = Config.BAN_STICKER ALLOW_EXCL = Config.ALLOW_EXCL - API_WEATHER = Config.API_OPENWHEATER + API_WEATHER = Config.API_OPENWEATHER SUDO_USERS.add(OWNER_ID) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py index 9f00dbf7f..0ec279f93 100644 --- a/tg_bot/modules/weather.py +++ b/tg_bot/modules/weather.py @@ -9,58 +9,54 @@ from telegram.ext import run_async @run_async -def cuaca(bot: Bot, update: Update, args: List[str]): - 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: 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 - try: - bot.sendChatAction(update.effective_chat.id, "typing") # Bot typing before send message - owm = pyowm.OWM(API_WEATHER, language='en') - observation = owm.weather_at_place(location) - cuacanya = observation.get_weather() - obs = owm.weather_at_place(location) - lokasi = obs.get_location() - lokasinya = lokasi.get_name() - # statusnya = cuacanya._detailed_status - temperatur = cuacanya.get_temperature(unit='celsius')['temp'] - fc = owm.three_hours_forecast(location) + 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) - # Simbol cuaca - statusnya = "" - cuacaskrg = cuacanya.get_weather_code() - if cuacaskrg < 232: # Hujan badai - statusnya += "⛈️ " - elif cuacaskrg < 321: # Gerimis - statusnya += "🌧️ " - elif cuacaskrg < 504: # Hujan terang - statusnya += "🌦️ " - elif cuacaskrg < 531: # Hujan berawan - statusnya += "⛈️ " - elif cuacaskrg < 622: # Bersalju - statusnya += "🌨️ " - elif cuacaskrg < 781: # Atmosfer - statusnya += "🌪️ " - elif cuacaskrg < 800: # Cerah - statusnya += "🌤️ " - elif cuacaskrg < 801: # Sedikit berawan - statusnya += "⛅️ " - elif cuacaskrg < 804: # Berawan - statusnya += "☁️ " - statusnya += cuacanya._detailed_status - + # 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 + - update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(lokasinya, - statusnya, temperatur)) + 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.") - except pyowm.exceptions.api_call_error.APICallError: - 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.") else: - return + update.effective_message.reply_text("Write a location to check the weather.") __help__ = """ @@ -69,6 +65,6 @@ def cuaca(bot: Bot, update: Update, args: List[str]): __mod_name__ = "Weather" -CUACA_HANDLER = DisableAbleCommandHandler("weather", cuaca, pass_args=True) +CUACA_HANDLER = DisableAbleCommandHandler("weather", weather, pass_args=True) dispatcher.add_handler(CUACA_HANDLER) \ No newline at end of file diff --git a/tg_bot/sample_config.py b/tg_bot/sample_config.py index 71cf17647..4603e6ddf 100644 --- a/tg_bot/sample_config.py +++ b/tg_bot/sample_config.py @@ -34,7 +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 + API_OPENWEATHER = None # OpenWeather API class Production(Config): From 85581f3095cfb09f073f20fc2bb4b639ebeb1d15 Mon Sep 17 00:00:00 2001 From: Ayra Hikari <36266025+AyraHikari@users.noreply.github.com> Date: Sat, 23 Jun 2018 21:42:33 +0700 Subject: [PATCH 3/5] Update with some changes - reorder import like - short check if args is none handle - check if location name & temp is none - and changes some code --- tg_bot/modules/weather.py | 100 +++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py index 0ec279f93..1e982c4ac 100644 --- a/tg_bot/modules/weather.py +++ b/tg_bot/modules/weather.py @@ -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__ = """ @@ -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) \ No newline at end of file +dispatcher.add_handler(WEATHER_HANDLER) From 6754d792cc8a13dc785203fac70fb01cf0227530 Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 02:10:36 +0700 Subject: [PATCH 4/5] Remove icon weather --- tg_bot/modules/weather.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py index 1e982c4ac..acfe76a44 100644 --- a/tg_bot/modules/weather.py +++ b/tg_bot/modules/weather.py @@ -30,30 +30,6 @@ def weather(bot, update, args): 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)) From b1fb1fc2333ce67a85b908890bb6db73e7da0442 Mon Sep 17 00:00:00 2001 From: Ayra Hikari Date: Tue, 25 Sep 2018 05:33:12 +0700 Subject: [PATCH 5/5] Added back status message --- tg_bot/modules/weather.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tg_bot/modules/weather.py b/tg_bot/modules/weather.py index acfe76a44..ae7b51a23 100644 --- a/tg_bot/modules/weather.py +++ b/tg_bot/modules/weather.py @@ -29,6 +29,8 @@ def weather(bot, update, args): temperature = theweather.get_temperature(unit='celsius').get('temp') if temperature == None: temperature = "Unknown" + + status = theweather._detailed_status update.message.reply_text("Today in {} is being {}, around {}°C.\n".format(thelocation, status, temperature))