nxy/bot/weather.py

44 lines
1.6 KiB
Python
Raw Normal View History

2017-05-28 10:27:50 +00:00
# -*- coding: utf-8 -*-
import os
2017-05-28 10:27:50 +00:00
import requests
2017-08-22 13:57:57 +00:00
from docopt import Dict
2017-05-28 10:27:50 +00:00
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
class Weather(Plugin):
URL = 'http://api.openweathermap.org/data/2.5/weather' \
'?q={}&appid={}&units=metric&lang=en'
2017-05-28 10:27:50 +00:00
@command
2017-08-22 13:57:57 +00:00
def weather(self, mask: IrcString, target: IrcString, args: Dict):
"""Gets the weather from OpenWeatherMap
2017-05-28 10:27:50 +00:00
%%weather <location>...
"""
req = requests.get(self.URL.format(' '.join(args['<location>']), os.environ['OPENWEATHERMAP_API_KEY']))
if req.status_code == 404:
return '\x02[Weather]\x02 Location not found'
data = req.json()
return '\x02[Weather]\x02 {city}, {country}: ' \
'\x02{temp}°C\x02 (\x0303{temp_min}°C\x0f - \x0304{temp_max}°C\x0f) \x02{text}\x02, ' \
'\x02{direction} {speed} km/h\x02, ' \
'\x02{humidity} % RH\x02, ' \
'\x02{pressure} hPa\x02'.format(
city=data['name'],
country=data['sys']['country'],
temp=data['main']['temp'],
temp_min=data['main']['temp_min'],
temp_max=data['main']['temp_max'],
text=data['weather'][0]['description'],
direction='↓↙←↖↑↗→↘'[round(data['wind']['deg'] / 45) % 8],
speed=data['wind']['speed'],
humidity=data['main']['humidity'],
pressure=data['main']['pressure']
)