From: John Runyon Date: Sun, 26 Nov 2023 00:09:48 +0000 (-0700) Subject: add avweather module to pull METAR from aviationweather.gov X-Git-Url: https://jfr.im/git/erebus.git/commitdiff_plain/c865bf373c32944a78596e2d5c31dec1ab17e990 add avweather module to pull METAR from aviationweather.gov --- diff --git a/modules/avweather.py b/modules/avweather.py new file mode 100644 index 0000000..cc6837d --- /dev/null +++ b/modules/avweather.py @@ -0,0 +1,116 @@ +# Erebus IRC bot - Author: Erebus Team +# vim: fileencoding=utf-8 +# weather module (from aviationweather.gov) +# This file is released into the public domain; see http://unlicense.org/ + +# module info +modinfo = { + 'author': 'Erebus Team', + 'license': 'public domain', + 'compatible': [0], + 'depends': ['userinfo'], + 'softdeps': ['help'], +} + +# preamble +import modlib +lib = modlib.modlib(__name__) +modstart = lib.modstart +modstop = lib.modstop + +# module code +import json +import sys +import re + +if sys.version_info.major < 3: + from urllib import urlopen, quote_plus +else: + from urllib.request import urlopen + from urllib.parse import quote_plus + +def location(person, default=None): return lib.mod('userinfo').get(person, 'location', default=None) + +def _dayofweek(dayname): + return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower()) + +def _time_adjust(d): + t = d['current']['observation_time'] + #XXX + #mo = re.match(r"(\d\d):(\d\d) (AM|PM)", t) + #if mo: + # return + return t + ' UTC' + +def _c2f(celsius): + return round(celsius * 9.0/5 + 32, 2) + +def _kmh2mph(kmh): + return round(kmh / 1.60934, 2) + +def _weather(place): + if not lib.parent.cfg.get('weatherstack_weather', 'key'): + return "Weather is not enabled - please set the API key in the config file" + + if place is not None: + url = 'http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib.parent.cfg.get('weatherstack_weather', 'key'), quote_plus(place)) + if sys.version_info.major < 3: + url = url.encode('utf8') + weather = json.load(urlopen(url)) + if lib.parent.cfg.getboolean('debug', 'weather'): + lib.parent.log('*', "?", repr(weather)) + if 'error' in weather: + return "Error from WeatherStack: (%d) %s" % (weather['error']['code'], weather['error']['info']) + + return u"Weather in %(location)s, %(region)s, %(country)s: As of %(time)s, %(conditions)s, %(cel)s°C (%(far)s°F) (feels like %(flcel)s°C (%(flfar)s°F)). Wind %(windk)skm/h (%(windm)smph) %(winddir)s." % { + 'location': weather['location']['name'], + 'region': weather['location']['region'], + 'country': weather['location']['country'], + 'time': _time_adjust(weather), + 'conditions': ', '.join(weather['current']['weather_descriptions']), + 'cel': weather['current']['temperature'], 'far': _c2f(weather['current']['temperature']), + 'flcel': weather['current']['feelslike'], 'flfar': _c2f(weather['current']['feelslike']), + 'windk': weather['current']['wind_speed'], 'windm': _kmh2mph(weather['current']['wind_speed']), + 'winddir': weather['current']['wind_dir'], + } + else: + return "I don't know where to look! Try %ssetinfo location " % (lib.parent.trigger,) + + +def _get_metar(place): + url = 'https://aviationweather.gov/cgi-bin/data/metar.php?ids=%s&hours=0&order=id%%2C-obs&sep=true&format=raw' % (quote_plus(place)) + if sys.version_info.major < 3: + url = url.encode('utf8') + return urlopen(url).read().decode('utf8').strip() + +METAR_REGEX = re.compile('(?P[A-Z]{4}) (?P