2017-05-28 10:27:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
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):
|
2017-05-30 10:44:44 +00:00
|
|
|
URL = 'https://query.yahooapis.com/v1/public/yql?format=json&q=' \
|
|
|
|
'select * from weather.forecast where u="c" and woeid in ' \
|
|
|
|
'(select woeid from geo.places(1) where text="{}")'
|
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):
|
2017-08-22 14:15:52 +00:00
|
|
|
"""Gets the weather from Yahoo weather API
|
2017-05-28 10:27:50 +00:00
|
|
|
|
|
|
|
%%weather <location>...
|
|
|
|
"""
|
2017-05-30 10:44:44 +00:00
|
|
|
req = requests.get(self.URL.format(' '.join(args['<location>'])))
|
2017-05-28 10:27:50 +00:00
|
|
|
data = req.json()
|
2017-08-22 14:15:52 +00:00
|
|
|
|
2017-05-28 10:27:50 +00:00
|
|
|
if 'error' in data:
|
2017-08-22 14:15:52 +00:00
|
|
|
return '\x02[Weather]\x02 Error'
|
2017-06-30 18:14:02 +00:00
|
|
|
elif not data['query']['results']:
|
2017-08-22 14:15:52 +00:00
|
|
|
return '\x02[Weather]\x02 Location not found'
|
2017-06-30 18:14:02 +00:00
|
|
|
else:
|
|
|
|
res = data['query']['results']['channel']
|
2017-08-22 14:15:52 +00:00
|
|
|
return '\x02[Weather]\x02 {city}, {region}, {country}: ' \
|
|
|
|
'\x02{temp}°{unit_temp} {text}\x02, ' \
|
|
|
|
'\x02{direction} {speed} {unit_speed}\x02' \
|
2017-06-30 18:14:02 +00:00
|
|
|
.format(city=res['location']['city'],
|
|
|
|
region=res['location']['region'].strip(),
|
|
|
|
country=res['location']['country'],
|
|
|
|
temp=res['item']['condition']['temp'],
|
|
|
|
text=res['item']['condition']['text'],
|
2017-07-07 00:11:20 +00:00
|
|
|
direction='↑↗→↘↓↙←↖'[round(int(res['wind']['direction']) / 45) % 8],
|
2017-06-30 18:14:02 +00:00
|
|
|
speed=res['wind']['speed'],
|
|
|
|
unit_temp=res['units']['temperature'],
|
|
|
|
unit_speed=res['units']['speed'])
|