weather: switch to OpenWeatherMap

public yahoo api was shut down on Jan 3, 2019
This commit is contained in:
jkhsjdhjs 2021-05-20 14:00:35 +00:00
parent 586bdceca9
commit b6b8d91592
2 changed files with 26 additions and 23 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os
import requests import requests
from docopt import Dict from docopt import Dict
from irc3.plugins.command import command from irc3.plugins.command import command
@ -8,34 +9,35 @@ from . import Plugin
class Weather(Plugin): class Weather(Plugin):
URL = 'https://query.yahooapis.com/v1/public/yql?format=json&q=' \ URL = 'http://api.openweathermap.org/data/2.5/weather' \
'select * from weather.forecast where u="c" and woeid in ' \ '?q={}&appid={}&units=metric&lang=en'
'(select woeid from geo.places(1) where text="{}")'
@command @command
def weather(self, mask: IrcString, target: IrcString, args: Dict): def weather(self, mask: IrcString, target: IrcString, args: Dict):
"""Gets the weather from Yahoo weather API """Gets the weather from OpenWeatherMap
%%weather <location>... %%weather <location>...
""" """
req = requests.get(self.URL.format(' '.join(args['<location>']))) req = requests.get(self.URL.format(' '.join(args['<location>']), os.environ['OPENWEATHERMAP_API_KEY']))
if req.status_code == 404:
return '\x02[Weather]\x02 Location not found'
data = req.json() data = req.json()
if 'error' in data: return '\x02[Weather]\x02 {city}, {country}: ' \
return '\x02[Weather]\x02 Error' '\x02{temp}°C\x02 (\x0303{temp_min}°C\x0f - \x0304{temp_max}°C\x0f) \x02{text}\x02, ' \
elif not data['query']['results']: '\x02{direction} {speed} km/h\x02, ' \
return '\x02[Weather]\x02 Location not found' '\x02{humidity} % RH\x02, ' \
else: '\x02{pressure} hPa\x02'.format(
res = data['query']['results']['channel'] city=data['name'],
return '\x02[Weather]\x02 {city}, {region}, {country}: ' \ country=data['sys']['country'],
'\x02{temp}°{unit_temp} {text}\x02, ' \ temp=data['main']['temp'],
'\x02{direction} {speed} {unit_speed}\x02' \ temp_min=data['main']['temp_min'],
.format(city=res['location']['city'], temp_max=data['main']['temp_max'],
region=res['location']['region'].strip(), text=data['weather'][0]['description'],
country=res['location']['country'], direction='↓↙←↖↑↗→↘'[round(data['wind']['deg'] / 45) % 8],
temp=res['item']['condition']['temp'], speed=data['wind']['speed'],
text=res['item']['condition']['text'], humidity=data['main']['humidity'],
direction='↑↗→↘↓↙←↖'[round(int(res['wind']['direction']) / 45) % 8], pressure=data['main']['pressure']
speed=res['wind']['speed'], )
unit_temp=res['units']['temperature'],
unit_speed=res['units']['speed'])

View File

@ -1,2 +1,3 @@
DATABASE_URI=postgresql://<pgsql user>:<pgsql password>@<pgsql host>:<pgsql port>/<pgsql database> DATABASE_URI=postgresql://<pgsql user>:<pgsql password>@<pgsql host>:<pgsql port>/<pgsql database>
GOOGLE_API_KEY=<google api key> GOOGLE_API_KEY=<google api key>
OPENWEATHERMAP_API_KEY=<openweathermap api key>