Added metar module

This commit is contained in:
Ville Ranki 2019-12-09 20:38:25 +02:00
parent a1b7d0b1ab
commit 5a2c125b61
3 changed files with 23 additions and 2 deletions

3
bot.py
View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
import asyncio
import os
import json
@ -44,6 +46,7 @@ class Bot:
try:
await moduleobject.matrix_message(bot, room, event)
except:
await self.send_text(room, f'Module {command} experienced difficulty: {sys.exc_info()[0]} - see log for details')
traceback.print_exc(file=sys.stderr)
async def unknown_cb(self, room, event):

View File

@ -1,9 +1,9 @@
class MatrixModule:
def matrix_start(self, bot):
print("Echo start!")
print("Echo started.")
def matrix_stop(self, bot):
print("Echo stop!")
print("Echo stopped")
async def matrix_message(self, bot, room, event):
args = event.body.split()

18
modules/metar.py Normal file
View File

@ -0,0 +1,18 @@
import subprocess
import os
import urllib.request
class MatrixModule:
async def matrix_message(self, bot, room, event):
args = event.body.split()
if len(args) == 2:
icao = args[1]
metar_url = "https://tgftp.nws.noaa.gov/data/observations/metar/stations/" + icao.upper() + ".TXT"
response = urllib.request.urlopen(metar_url)
lines = response.readlines()
await bot.send_text(room, lines[1].decode("utf-8").strip())
else:
await bot.send_text(room, 'Usage: !metar <icao code>')
def help(self):
return('Metar data access (usage: !metar <icao code>)')