diff options
| author | 2016-03-29 12:56:58 +0100 | |
|---|---|---|
| committer | 2016-03-29 12:56:58 +0100 | |
| commit | f943d63098a50746f4e470e403a991a4d9713030 (patch) | |
| tree | deeb98058917d0155227211d72576f0cbab28d3f /modules/weather.py | |
| parent | Initial commit (diff) | |
first commit.
Diffstat (limited to 'modules/weather.py')
| -rw-r--r-- | modules/weather.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/modules/weather.py b/modules/weather.py new file mode 100644 index 00000000..868f9fca --- /dev/null +++ b/modules/weather.py @@ -0,0 +1,35 @@ +import Utils + +URL_WEATHER = "http://api.openweathermap.org/data/2.5/weather" + +class Module(object): + def __init__(self, bot): + bot.events.on("received").on("command").on("weather").hook( + self.weather, min_args=1, + help="Get current weather data for a provided location") + self.bot = bot + + 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") |
