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:
|
|
|
|
return "%s, %s" % (user_location["city"], user_location["country"])
|
|
|
|
|
|
|
|
@utils.hook("received.command.weather", usage="<location>")
|
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]
|
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
|
|
|
|
if event["args"]:
|
|
|
|
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")
|
|
|
|
else:
|
|
|
|
location = self._user_location(event["user"])
|
|
|
|
if location == None:
|
|
|
|
raise utils.EventError("You don't have a location set")
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_WEATHER, get_params={
|
2019-04-28 11:11:23 +00:00
|
|
|
"q": location, "units": "metric",
|
2016-03-29 11:56:58 +00:00
|
|
|
"APPID": api_key},
|
|
|
|
json=True)
|
|
|
|
if page:
|
2018-12-11 22:26:38 +00:00
|
|
|
if "weather" in page.data:
|
|
|
|
location = "%s, %s" % (page.data["name"], page.data["sys"][
|
2016-03-29 11:56:58 +00:00
|
|
|
"country"])
|
2018-12-11 22:26:38 +00:00
|
|
|
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:
|
2018-10-20 19:51:29 +00:00
|
|
|
raise utils.EventsResultsError()
|