2017-05-28 10:27:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-05-20 14:00:35 +00:00
|
|
|
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):
|
2021-05-20 14:00:35 +00:00
|
|
|
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):
|
2021-05-20 14:00:35 +00:00
|
|
|
"""Gets the weather from OpenWeatherMap
|
2017-05-28 10:27:50 +00:00
|
|
|
|
|
|
|
%%weather <location>...
|
|
|
|
"""
|
2021-05-20 14:00:35 +00:00
|
|
|
req = requests.get(self.URL.format(' '.join(args['<location>']), os.environ['OPENWEATHERMAP_API_KEY']))
|
2017-08-22 14:15:52 +00:00
|
|
|
|
2021-05-20 14:00:35 +00:00
|
|
|
if req.status_code == 404:
|
2017-08-22 14:15:52 +00:00
|
|
|
return '\x02[Weather]\x02 Location not found'
|
2021-05-20 14:00:35 +00:00
|
|
|
|
|
|
|
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']
|
|
|
|
)
|