bitbot-3.11-fork/modules/weather.py

91 lines
3.5 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:
name = user_location.get("name", None)
return [user_location["lat"], user_location["lon"], name]
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
query = None
2019-07-03 12:03:48 +00:00
nickname = None
if event["args"]:
query = 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 not location == None:
2019-07-03 12:03:48 +00:00
nickname = target_user.nickname
else:
location = self._user_location(event["user"])
2019-07-03 12:03:48 +00:00
nickname = event["user"].nickname
if location == None:
raise utils.EventError("You don't have a location set")
args = {"units": "metric", "APPID": api_key}
if location == None and query:
2020-03-08 14:14:00 +00:00
location_info = self.exports.get("get-location")(query)
if not location_info == None:
location = [location_info["lat"], location_info["lon"],
location_info.get("name", None)]
if location == None:
raise utils.EventError("Unknown location")
lat, lon, location_name = location
args["lat"] = lat
args["lon"] = lon
page = utils.http.request(URL_WEATHER, get_params=args).json()
2016-03-29 11:56:58 +00:00
if page:
if "weather" in page:
if location_name:
location_str = location_name
else:
location_parts = [page["name"]]
if "country" in page["sys"]:
location_parts.append(page["sys"]["country"])
location_str = ", ".join(location_parts)
celsius = "%dC" % page["main"]["temp"]
fahrenheit = "%dF" % ((page["main"]["temp"]*(9/5))+32)
description = page["weather"][0]["description"].title()
humidity = "%s%%" % page["main"]["humidity"]
# wind speed is in metres per second - 3.6* for KMh
wind_speed = 3.6*page["wind"]["speed"]
2020-03-04 14:27:48 +00:00
wind_speed_k = "%skm/h" % round(wind_speed, 1)
wind_speed_m = "%smi/h" % round(0.6214*wind_speed, 1)
2016-03-29 11:56:58 +00:00
2019-07-03 12:03:48 +00:00
if not nickname == None:
location_str = "(%s) %s" % (nickname, location_str)
2019-11-25 16:09:35 +00:00
2016-03-29 11:56:58 +00:00
event["stdout"].write(
2019-11-26 18:42:33 +00:00
"%s | %s/%s | %s | Humidity: %s | Wind: %s/%s" % (
location_str, celsius, fahrenheit, description, humidity,
2019-06-24 06:12:58 +00:00
wind_speed_k, wind_speed_m))
2016-03-29 11:56:58 +00:00
else:
event["stderr"].write("No weather information for this location")
else:
raise utils.EventResultsError()