Added weather plugin

This commit is contained in:
mrhanky 2017-05-28 12:27:50 +02:00
parent fe9b8be203
commit 4fdc951718
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
2 changed files with 52 additions and 0 deletions

View File

@ -20,6 +20,7 @@
"nxy.plugins.tell",
"nxy.plugins.timer",
"nxy.plugins.useless",
"nxy.plugins.weather",
"nxy.plugins.youtube"
],
"irc3.plugins.command": {

51
nxy/plugins/weather.py Normal file
View File

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
import irc3
import requests
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import Plugin
from ..utils import pp
@irc3.plugin
class Weather(Plugin):
requires = ['irc3.plugins.command']
URL = 'https://query.yahooapis.com/v1/public/yql?format=json&q={}'
def __init__(self, bot: irc3.IrcBot):
super().__init__(bot)
# noinspection PyUnusedLocal
@command
def weather(self, mask: IrcString, channel: IrcString, args: DocOptDict):
"""Saves a message for nick to forward on activity
%%weather <location>...
"""
qry = '''select * from weather.forecast where u="c" and woeid in
(select woeid from geo.places(1) where text="{location}")
'''.format(location=' '.join(args['<location>']))
req = requests.get(self.URL.format(qry))
data = req.json()
if 'error' in data:
return '[Weather] Location not found'
res = data['query']['results']['channel']
condition = res['item']['condition']
location = res['location']
direction = '↑↗→↘↓↙←↖'[round(int(res['wind']['direction']) / 45) % 8]
return '[Weather] {city}, {region}, {country}: ' \
'{temp}°{unit_temp} {text}, ' \
'{direction} {speed} {unit_speed}' \
.format(city=location['city'],
region=location['region'].strip(),
country=location['country'],
temp=condition['temp'],
text=condition['text'],
direction=direction,
speed=res['wind']['speed'],
unit_temp=res['units']['temperature'],
unit_speed=res['units']['speed'])