nxy/bot/weather.py
jkhsjdhjs b6b8d91592 weather: switch to OpenWeatherMap
public yahoo api was shut down on Jan 3, 2019
2021-05-20 14:00:35 +00:00

44 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
import os
import requests
from docopt import Dict
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'
@command
def weather(self, mask: IrcString, target: IrcString, args: Dict):
"""Gets the weather from OpenWeatherMap
%%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']
)