2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on location
|
2016-03-30 18:31:23 +00:00
|
|
|
#--require-config openweathermap-api-key
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_WEATHER = "http://api.openweathermap.org/data/2.5/weather"
|
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-04-28 11:11:23 +00:00
|
|
|
def _user_location(self, user):
|
|
|
|
user_location = user.get_setting("location", None)
|
|
|
|
if not user_location == None:
|
2019-06-18 15:40:42 +00:00
|
|
|
name = user_location.get("name", None)
|
|
|
|
return [user_location["lat"], user_location["lon"], name]
|
2019-04-28 11:11:23 +00:00
|
|
|
|
2019-04-28 14:21:09 +00:00
|
|
|
@utils.hook("received.command.w", alias_of="weather")
|
2019-04-28 14:20:37 +00:00
|
|
|
@utils.hook("received.command.weather")
|
2016-03-29 11:56:58 +00:00
|
|
|
def weather(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-04-28 11:11:23 +00:00
|
|
|
:help: Get current weather for you or someone else
|
|
|
|
:usage: [nickname]
|
2019-05-23 10:50:12 +00:00
|
|
|
:require_setting: location
|
|
|
|
:require_setting_unless: 1
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
api_key = self.bot.config["openweathermap-api-key"]
|
2019-04-28 11:11:23 +00:00
|
|
|
|
|
|
|
location = None
|
2019-10-16 14:53:34 +00:00
|
|
|
query = None
|
2019-07-03 12:03:48 +00:00
|
|
|
nickname = None
|
2019-04-28 11:11:23 +00:00
|
|
|
if event["args"]:
|
2019-10-16 14:53:34 +00:00
|
|
|
query = event["args"]
|
2019-05-04 23:16:55 +00:00
|
|
|
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)
|
2019-10-16 14:53:34 +00:00
|
|
|
if not location == None:
|
2019-07-03 12:03:48 +00:00
|
|
|
nickname = target_user.nickname
|
2019-04-28 11:11:23 +00:00
|
|
|
else:
|
|
|
|
location = self._user_location(event["user"])
|
2019-07-03 12:03:48 +00:00
|
|
|
nickname = event["user"].nickname
|
2019-04-28 11:11:23 +00:00
|
|
|
if location == None:
|
|
|
|
raise utils.EventError("You don't have a location set")
|
|
|
|
|
2019-05-04 23:16:55 +00:00
|
|
|
args = {"units": "metric", "APPID": api_key}
|
|
|
|
|
2019-07-16 15:42:32 +00:00
|
|
|
|
2019-10-16 14:53:34 +00:00
|
|
|
if location == None and query:
|
|
|
|
location_info = self.exports.get_one("get-location")(query)
|
|
|
|
if not location_info == None:
|
|
|
|
location = [location_info["lat"], location_info["lon"],
|
|
|
|
location_info.get("name", None)]
|
2019-07-16 15:42:32 +00:00
|
|
|
if location == None:
|
2019-10-16 14:53:34 +00:00
|
|
|
raise utils.EventError("Unknown location")
|
2019-07-16 15:42:32 +00:00
|
|
|
|
|
|
|
lat, lon, location_name = location
|
|
|
|
args["lat"] = lat
|
|
|
|
args["lon"] = lon
|
2019-05-04 23:16:55 +00:00
|
|
|
|
2019-11-26 13:41:40 +00:00
|
|
|
page = utils.http.request(URL_WEATHER, get_params=args).json()
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
2019-11-26 13:41:40 +00:00
|
|
|
if "weather" in page:
|
2019-06-18 15:40:42 +00:00
|
|
|
if location_name:
|
|
|
|
location_str = location_name
|
2019-06-18 15:36:22 +00:00
|
|
|
else:
|
2019-11-26 13:41:40 +00:00
|
|
|
location_parts = [page["name"]]
|
|
|
|
if "country" in page["sys"]:
|
|
|
|
location_parts.append(page["sys"]["country"])
|
2019-06-18 15:36:22 +00:00
|
|
|
location_str = ", ".join(location_parts)
|
2019-06-18 15:34:51 +00:00
|
|
|
|
2019-11-26 13:41:40 +00:00
|
|
|
celsius = "%dC" % page["main"]["temp"]
|
|
|
|
fahrenheit = "%dF" % ((page["main"]["temp"]*(9/5))+32)
|
|
|
|
description = page["weather"][0]["description"].title()
|
|
|
|
humidity = "%s%%" % page["main"]["humidity"]
|
2019-06-24 06:30:29 +00:00
|
|
|
|
|
|
|
# wind speed is in metres per second - 3.6* for KMh
|
2019-11-26 13:41:40 +00:00
|
|
|
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:
|
2019-11-25 16:20:40 +00:00
|
|
|
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" % (
|
2019-06-18 15:34:51 +00:00
|
|
|
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:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|