Added weather plugin
This commit is contained in:
parent
fe9b8be203
commit
4fdc951718
|
@ -20,6 +20,7 @@
|
||||||
"nxy.plugins.tell",
|
"nxy.plugins.tell",
|
||||||
"nxy.plugins.timer",
|
"nxy.plugins.timer",
|
||||||
"nxy.plugins.useless",
|
"nxy.plugins.useless",
|
||||||
|
"nxy.plugins.weather",
|
||||||
"nxy.plugins.youtube"
|
"nxy.plugins.youtube"
|
||||||
],
|
],
|
||||||
"irc3.plugins.command": {
|
"irc3.plugins.command": {
|
||||||
|
|
51
nxy/plugins/weather.py
Normal file
51
nxy/plugins/weather.py
Normal 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'])
|
Loading…
Reference in New Issue
Block a user