bitbot-3.11-fork/modules/weather.py

68 lines
2.6 KiB
Python
Raw Normal View History

#--depends-on commands
#--depends-on location
#--require-config openweathermap-api-key
from src import ModuleManager, utils
2016-03-29 11:56:58 +00:00
URL_WEATHER = "http://api.openweathermap.org/data/2.5/weather"
class Module(ModuleManager.BaseModule):
def _user_location(self, user):
user_location = user.get_setting("location", None)
if not user_location == None:
return [user_location["lat"], user_location["lon"]]
2019-04-28 14:21:09 +00:00
@utils.hook("received.command.w", alias_of="weather")
@utils.hook("received.command.weather")
2016-03-29 11:56:58 +00:00
def weather(self, event):
"""
:help: Get current weather for you or someone else
:usage: [nickname]
:require_setting: location
:require_setting_unless: 1
"""
2016-03-29 11:56:58 +00:00
api_key = self.bot.config["openweathermap-api-key"]
location = None
if event["args"]:
if len(event["args_split"]) == 1 and event["server"].has_user_id(
event["args_split"][0]):
target_user = event["server"].get_user(event["args_split"][0])
location = self._user_location(target_user)
if location == None:
raise utils.EventError("%s doesn't have a location set"
% target_user.nickname)
else:
location = self._user_location(event["user"])
if location == None:
raise utils.EventError("You don't have a location set")
args = {"units": "metric", "APPID": api_key}
if location:
lat, lon = location
args["lat"] = lat
args["lon"] = lon
else:
args["q"] = event["args"]
page = utils.http.request(URL_WEATHER, get_params=args, json=True)
2016-03-29 11:56:58 +00:00
if page:
if "weather" in page.data:
location = "%s, %s" % (page.data["name"], page.data["sys"][
2016-03-29 11:56:58 +00:00
"country"])
celsius = "%dC" % page.data["main"]["temp"]
fahrenheit = "%dF" % ((page.data["main"]["temp"]*(9/5))+32)
description = page.data["weather"][0]["description"].title()
humidity = "%s%%" % page.data["main"]["humidity"]
wind_speed = "%sKM/h" % page.data["wind"]["speed"]
2016-03-29 11:56:58 +00:00
event["stdout"].write(
"(%s) %s/%s | %s | Humidity: %s | Wind: %s" % (
location, celsius, fahrenheit, description, humidity,
wind_speed))
else:
event["stderr"].write("No weather information for this location")
else:
raise utils.EventsResultsError()