weather: switch to OpenWeatherMap
public yahoo api was shut down on Jan 3, 2019
This commit is contained in:
parent
586bdceca9
commit
b6b8d91592
@ -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 <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()
|
||||
|
||||
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']
|
||||
)
|
||||
|
@ -1,2 +1,3 @@
|
||||
DATABASE_URI=postgresql://<pgsql user>:<pgsql password>@<pgsql host>:<pgsql port>/<pgsql database>
|
||||
GOOGLE_API_KEY=<google api key>
|
||||
OPENWEATHERMAP_API_KEY=<openweathermap api key>
|
||||
|
Loading…
Reference in New Issue
Block a user