From b6b8d91592c5b06df4a803a956012d05b1806034 Mon Sep 17 00:00:00 2001 From: jkhsjdhjs Date: Thu, 20 May 2021 14:00:35 +0000 Subject: [PATCH] weather: switch to OpenWeatherMap public yahoo api was shut down on Jan 3, 2019 --- bot/weather.py | 48 +++++++++++++++++++++++++----------------------- files/.env | 1 + 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/bot/weather.py b/bot/weather.py index e01ac80..23d5c72 100644 --- a/bot/weather.py +++ b/bot/weather.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import os import requests from docopt import Dict from irc3.plugins.command import command @@ -8,34 +9,35 @@ from . import Plugin class Weather(Plugin): - 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="{}")' + URL = 'http://api.openweathermap.org/data/2.5/weather' \ + '?q={}&appid={}&units=metric&lang=en' @command def weather(self, mask: IrcString, target: IrcString, args: Dict): - """Gets the weather from Yahoo weather API + """Gets the weather from OpenWeatherMap %%weather ... """ - req = requests.get(self.URL.format(' '.join(args['']))) + req = requests.get(self.URL.format(' '.join(args['']), os.environ['OPENWEATHERMAP_API_KEY'])) + + if req.status_code == 404: + return '\x02[Weather]\x02 Location not found' + data = req.json() - if 'error' in data: - return '\x02[Weather]\x02 Error' - elif not data['query']['results']: - return '\x02[Weather]\x02 Location not found' - else: - res = data['query']['results']['channel'] - return '\x02[Weather]\x02 {city}, {region}, {country}: ' \ - '\x02{temp}°{unit_temp} {text}\x02, ' \ - '\x02{direction} {speed} {unit_speed}\x02' \ - .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']) + return '\x02[Weather]\x02 {city}, {country}: ' \ + '\x02{temp}°C\x02 (\x0303{temp_min}°C\x0f - \x0304{temp_max}°C\x0f) \x02{text}\x02, ' \ + '\x02{direction} {speed} km/h\x02, ' \ + '\x02{humidity} % RH\x02, ' \ + '\x02{pressure} hPa\x02'.format( + city=data['name'], + country=data['sys']['country'], + temp=data['main']['temp'], + temp_min=data['main']['temp_min'], + temp_max=data['main']['temp_max'], + text=data['weather'][0]['description'], + direction='↓↙←↖↑↗→↘'[round(data['wind']['deg'] / 45) % 8], + speed=data['wind']['speed'], + humidity=data['main']['humidity'], + pressure=data['main']['pressure'] + ) diff --git a/files/.env b/files/.env index b6902fe..4abe10a 100644 --- a/files/.env +++ b/files/.env @@ -1,2 +1,3 @@ DATABASE_URI=postgresql://:@:/ GOOGLE_API_KEY= +OPENWEATHERMAP_API_KEY=