2016-03-30 18:31:23 +00:00
|
|
|
#--require-config openweathermap-api-key
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import Utils
|
|
|
|
|
|
|
|
URL_WEATHER = "http://api.openweathermap.org/data/2.5/weather"
|
|
|
|
|
|
|
|
class Module(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
def __init__(self, bot, events):
|
2016-04-06 11:02:44 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("weather").hook(
|
2016-03-29 11:56:58 +00:00
|
|
|
self.weather, min_args=1,
|
2016-04-06 11:02:44 +00:00
|
|
|
help="Get current weather data for a provided location",
|
|
|
|
usage="<location>")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def weather(self, event):
|
|
|
|
api_key = self.bot.config["openweathermap-api-key"]
|
|
|
|
page = Utils.get_url(URL_WEATHER, get_params={
|
|
|
|
"q": event["args"], "units": "metric",
|
|
|
|
"APPID": api_key},
|
|
|
|
json=True)
|
|
|
|
if page:
|
|
|
|
if "weather" in page:
|
|
|
|
location = "%s, %s" % (page["name"], page["sys"][
|
|
|
|
"country"])
|
|
|
|
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 = "%sKM/h" % page["wind"]["speed"]
|
|
|
|
|
|
|
|
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:
|
|
|
|
event["stderr"].write("Failed to load results")
|