Simplified weather plugin
This commit is contained in:
parent
83e3876b06
commit
96cf435b3c
@ -13,37 +13,30 @@ from . import 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)
|
||||
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="{}")'
|
||||
|
||||
@command
|
||||
def weather(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
||||
"""Saves a message for nick to forward on activity
|
||||
"""Gets the weather from Yahoo weather API.
|
||||
|
||||
%%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))
|
||||
req = requests.get(self.URL.format(' '.join(args['<location>'])))
|
||||
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,
|
||||
return '[Weather] {city}, {region}, {country}: {temp}°{unit_temp} ' \
|
||||
'{text}, {direction} {speed} {unit_speed}' \
|
||||
.format(city=res['location']['city'],
|
||||
region=res['location']['region'].strip(),
|
||||
country=res['location']['country'],
|
||||
temp=res['item']['condition']['temp'],
|
||||
text=res['item']['condition']['text'],
|
||||
direction='↑↗→↘↓↙←↖'[round(int(res['wind']['direction'])
|
||||
/ 45) % 8],
|
||||
speed=res['wind']['speed'],
|
||||
unit_temp=res['units']['temperature'],
|
||||
unit_speed=res['units']['speed'])
|
||||
|
Loading…
Reference in New Issue
Block a user