now-arriving/bom.py

42 lines
1.2 KiB
Python

from ftplib import FTP
import xml.etree.ElementTree as ET
def fetch_forecast():
file = []
with FTP('ftp.bom.gov.au') as ftp:
ftp.login()
ftp.retrlines('RETR anon/gen/fwo/IDV10450.xml', file.append)
return ''.join(file)
def parse_forecast(xml):
root = ET.fromstring(xml)
[today] = root.findall('./forecast/area[@aac="VIC_PT042"]/forecast-period[@index="0"]')
return today
import cache
import datetime
with cache.open() as cache:
print(parse_forecast(cache.get('weather', datetime.timedelta(minutes=10), fetch_forecast)))
file = []
with FTP('ftp.bom.gov.au') as ftp:
ftp.login()
ftp.retrlines('RETR anon/gen/fwo/IDV60920.xml', file.append)
root = ET.fromstringlist(file)
stations = root.findall('./observations/station')
from math import cos, asin, sqrt, pi
def distance(lat1, lon1, lat2, lon2):
r = 6371 # km
p = pi / 180
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
return 2 * r * asin(sqrt(a))
def dist(station):
lat, lon = [float(station.attrib[k]) for k in ['lat', 'lon']]
return distance(lat, lon, -37.82635, 145.09653)
stations.sort(key=dist)
for station in stations:
print(station.attrib['stn-name'], dist(station))