-
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?
Changes from 1 commit
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 |
---|---|---|
|
@@ -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 | ||
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. *weather |
||
|
||
|
||
SUDO_USERS.add(OWNER_ID) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
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. Apply PEP8 import order
|
||
@run_async | ||
def cuaca(bot: Bot, update: Update, args: List[str]): | ||
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. English variables please - keep it consistent |
||
location = " ".join(args) | ||
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.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 | ||
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. I'm against sending chat actions, as it's unnecessary and causes extra api calls; it just slows down the bot. |
||
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() | ||
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. Are the above lines guaranteed not to return |
||
# statusnya = cuacanya._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. Nit: remove commented out code |
||
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 | ||
|
||
|
||
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(lokasinya, | ||
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. "The weather forecast for {} today is: 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. don't forget this |
||
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.") | ||
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. Does APICallError only occur when no location is specified...? |
||
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 |
||
return | ||
|
||
|
||
__help__ = """ | ||
- /weather <city>: get weather info in a particular place | ||
""" | ||
|
||
__mod_name__ = "Weather" | ||
|
||
CUACA_HANDLER = DisableAbleCommandHandler("weather", cuaca, pass_args=True) | ||
|
||
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_OPENWHEATER = None # OpenWeather API | ||
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. *weather, not wheater |
||
|
||
|
||
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.
*weather