Simplified weather plugin
This commit is contained in:
parent
83e3876b06
commit
96cf435b3c
@ -13,37 +13,30 @@ from . import Plugin
|
|||||||
class Weather(Plugin):
|
class Weather(Plugin):
|
||||||
requires = ['irc3.plugins.command']
|
requires = ['irc3.plugins.command']
|
||||||
|
|
||||||
URL = 'https://query.yahooapis.com/v1/public/yql?format=json&q={}'
|
URL = 'https://query.yahooapis.com/v1/public/yql?format=json&q=' \
|
||||||
|
'select * from weather.forecast where u="c" and woeid in ' \
|
||||||
def __init__(self, bot: irc3.IrcBot):
|
'(select woeid from geo.places(1) where text="{}")'
|
||||||
super().__init__(bot)
|
|
||||||
|
|
||||||
@command
|
@command
|
||||||
def weather(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
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>...
|
%%weather <location>...
|
||||||
"""
|
"""
|
||||||
qry = '''select * from weather.forecast where u="c" and woeid in
|
req = requests.get(self.URL.format(' '.join(args['<location>'])))
|
||||||
(select woeid from geo.places(1) where text="{location}")
|
|
||||||
'''.format(location=' '.join(args['<location>']))
|
|
||||||
req = requests.get(self.URL.format(qry))
|
|
||||||
data = req.json()
|
data = req.json()
|
||||||
if 'error' in data:
|
if 'error' in data:
|
||||||
return '[Weather] Location not found'
|
return '[Weather] Location not found'
|
||||||
res = data['query']['results']['channel']
|
res = data['query']['results']['channel']
|
||||||
condition = res['item']['condition']
|
return '[Weather] {city}, {region}, {country}: {temp}°{unit_temp} ' \
|
||||||
location = res['location']
|
'{text}, {direction} {speed} {unit_speed}' \
|
||||||
direction = '↑↗→↘↓↙←↖'[round(int(res['wind']['direction']) / 45) % 8]
|
.format(city=res['location']['city'],
|
||||||
return '[Weather] {city}, {region}, {country}: ' \
|
region=res['location']['region'].strip(),
|
||||||
'{temp}°{unit_temp} {text}, ' \
|
country=res['location']['country'],
|
||||||
'{direction} {speed} {unit_speed}' \
|
temp=res['item']['condition']['temp'],
|
||||||
.format(city=location['city'],
|
text=res['item']['condition']['text'],
|
||||||
region=location['region'].strip(),
|
direction='↑↗→↘↓↙←↖'[round(int(res['wind']['direction'])
|
||||||
country=location['country'],
|
/ 45) % 8],
|
||||||
temp=condition['temp'],
|
|
||||||
text=condition['text'],
|
|
||||||
direction=direction,
|
|
||||||
speed=res['wind']['speed'],
|
speed=res['wind']['speed'],
|
||||||
unit_temp=res['units']['temperature'],
|
unit_temp=res['units']['temperature'],
|
||||||
unit_speed=res['units']['speed'])
|
unit_speed=res['units']['speed'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user