-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusl.py
46 lines (31 loc) · 1.28 KB
/
usl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import datetime
import requests
import bs4
import wxchal
import util
def get_forecast(forecast_time: datetime.datetime,
station: str) -> wxchal.Forecast:
USL_URL = 'http://microclimates.org/forecast/'
if forecast_time.hour not in [12, 22]:
raise ValueError('USL only has 12z and 22z forecasts')
request_url = '/'.join([USL_URL,
station.upper(),
forecast_time.strftime('%Y%m%d_%H')])
request_url += '.html'
resp = util.cached_get(request_url, datetime.timedelta(minutes=15))
if resp.status_code != 200 or resp.url != request_url:
raise RuntimeError('Forecast unavailable for station and time')
soup = bs4.BeautifulSoup(resp.text, 'html.parser')
cols = soup.find(id='stats').find_all('tr')[1].find_all('td')
cols[:] = [c.string for c in cols]
high = int(cols[0].split('°')[0])
low = int(cols[1].split('°')[0])
wind = int(cols[2].split(' ')[0])
precip = float(cols[3][:-1])
forecast = wxchal.Forecast(forecast_time.date() +
datetime.timedelta(days=1),
high,
low,
wind,
precip)
return forecast