2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
weather.py - Phenny Weather Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2013-06-09 01:27:24 -04:00
|
|
|
import re
|
2013-01-10 20:24:17 -05:00
|
|
|
import metar
|
2013-08-24 21:45:05 -04:00
|
|
|
import json
|
2008-02-21 07:06:33 -05:00
|
|
|
import web
|
2012-06-02 01:17:09 -04:00
|
|
|
from tools import deprecated, GrumbleError
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
r_from = re.compile(r'(?i)([+-]\d+):00 from')
|
|
|
|
|
2013-08-24 21:45:05 -04:00
|
|
|
|
|
|
|
def location(q):
|
2017-05-29 17:28:14 -04:00
|
|
|
uri = 'https://nominatim.openstreetmap.org/search?{type}={query}&format=json'
|
2017-02-17 14:43:28 -05:00
|
|
|
if q.isdigit():
|
2017-05-29 17:28:14 -04:00
|
|
|
uri = uri . format(type = 'postalcode', query = web.quote(q))
|
2017-02-17 14:43:28 -05:00
|
|
|
else:
|
2017-05-29 17:28:14 -04:00
|
|
|
uri = uri . format(type = 'q', query = web.quote(q))
|
2013-08-24 21:45:05 -04:00
|
|
|
results = web.get(uri)
|
|
|
|
data = json.loads(results)
|
|
|
|
|
2018-04-02 10:11:03 -04:00
|
|
|
if not data:
|
|
|
|
return None, None
|
2013-08-24 21:45:05 -04:00
|
|
|
|
2018-04-02 10:11:03 -04:00
|
|
|
latitude = float(data[0]['lat'])
|
|
|
|
longitude = float(data[0]['lon'])
|
|
|
|
|
|
|
|
return latitude, longitude
|
2013-08-24 21:45:05 -04:00
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
def local(icao, hour, minute):
|
2012-01-03 14:09:34 -05:00
|
|
|
uri = ('http://www.flightstats.com/' +
|
|
|
|
'go/Airport/airportDetails.do?airportCode=%s')
|
|
|
|
try: bytes = web.get(uri % icao)
|
2013-01-10 20:24:17 -05:00
|
|
|
except AttributeError:
|
2012-01-03 14:09:34 -05:00
|
|
|
raise GrumbleError('A WEBSITE HAS GONE DOWN WTF STUPID WEB')
|
|
|
|
m = r_from.search(bytes)
|
2013-01-10 20:24:17 -05:00
|
|
|
if m:
|
2012-01-03 14:09:34 -05:00
|
|
|
offset = m.group(1)
|
|
|
|
lhour = int(hour) + int(offset)
|
|
|
|
lhour = lhour % 24
|
|
|
|
return (str(lhour) + ':' + str(minute) + ', ' + str(hour) +
|
|
|
|
str(minute) + 'Z')
|
|
|
|
# return (str(lhour) + ':' + str(minute) + ' (' + str(hour) +
|
|
|
|
# ':' + str(minute) + 'Z)')
|
|
|
|
return str(hour) + ':' + str(minute) + 'Z'
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2013-08-24 21:45:05 -04:00
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
def code(phenny, search):
|
2012-01-03 14:09:34 -05:00
|
|
|
from icao import data
|
|
|
|
|
|
|
|
if search.upper() in [loc[0] for loc in data]:
|
|
|
|
return search.upper()
|
|
|
|
else:
|
2018-04-02 10:11:03 -04:00
|
|
|
latitude, longitude = location(search)
|
2013-08-24 21:45:05 -04:00
|
|
|
if not latitude or not longitude:
|
|
|
|
return False
|
2012-01-03 14:09:34 -05:00
|
|
|
sumOfSquares = (99999999999999999999999999999, 'ICAO')
|
2013-01-10 20:24:17 -05:00
|
|
|
for icao_code, lat, lon in data:
|
2012-01-03 14:09:34 -05:00
|
|
|
latDiff = abs(latitude - lat)
|
|
|
|
lonDiff = abs(longitude - lon)
|
|
|
|
diff = (latDiff * latDiff) + (lonDiff * lonDiff)
|
2013-01-10 20:24:17 -05:00
|
|
|
if diff < sumOfSquares[0]:
|
2012-01-03 14:09:34 -05:00
|
|
|
sumOfSquares = (diff, icao_code)
|
|
|
|
return sumOfSquares[1]
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2013-08-24 21:45:05 -04:00
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
def f_weather(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
""".weather <ICAO> - Show the weather at airport with the code <ICAO>."""
|
2013-01-10 20:24:17 -05:00
|
|
|
icao_code = input.group(2)
|
|
|
|
if not icao_code:
|
|
|
|
return phenny.say("Try .weather London, for example?")
|
2012-01-03 14:09:34 -05:00
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
icao_code = code(phenny, icao_code)
|
2012-01-03 14:09:34 -05:00
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
if not icao_code:
|
|
|
|
phenny.say("No ICAO code found, sorry")
|
2012-01-03 14:09:34 -05:00
|
|
|
return
|
|
|
|
|
2017-02-11 01:52:53 -05:00
|
|
|
uri = 'http://tgftp.nws.noaa.gov/data/observations/metar/stations/%s.TXT'
|
2013-01-10 20:24:17 -05:00
|
|
|
try:
|
|
|
|
bytes = web.get(uri % icao_code)
|
|
|
|
except AttributeError:
|
2012-01-03 14:09:34 -05:00
|
|
|
raise GrumbleError('OH CRAP NOAA HAS GONE DOWN THE WEB IS BROKEN')
|
2013-06-09 01:27:24 -04:00
|
|
|
except web.HTTPError:
|
2013-01-10 20:24:17 -05:00
|
|
|
phenny.say("No NOAA data available for that location.")
|
2012-01-03 14:09:34 -05:00
|
|
|
return
|
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
if 'Not Found' in bytes:
|
|
|
|
phenny.say(icao_code + ": no such ICAO code, or no NOAA data")
|
2012-01-03 14:09:34 -05:00
|
|
|
return
|
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
phenny.say(str(metar.parse(bytes)))
|
2008-02-21 07:06:33 -05:00
|
|
|
f_weather.rule = (['weather'], r'(.*)')
|
|
|
|
|
2013-01-10 20:24:17 -05:00
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|