From 96cf435b3c023a7ef6d2d28bb345d3c20326ee85 Mon Sep 17 00:00:00 2001 From: mrhanky Date: Tue, 30 May 2017 12:44:44 +0200 Subject: [PATCH] Simplified weather plugin --- nxy/plugins/weather.py | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/nxy/plugins/weather.py b/nxy/plugins/weather.py index 3dcdcc3..4bceeba 100644 --- a/nxy/plugins/weather.py +++ b/nxy/plugins/weather.py @@ -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 ... """ - qry = '''select * from weather.forecast where u="c" and woeid in - (select woeid from geo.places(1) where text="{location}") - '''.format(location=' '.join(args[''])) - req = requests.get(self.URL.format(qry)) + req = requests.get(self.URL.format(' '.join(args['']))) 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'])