-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetchForecast.py
executable file
·57 lines (47 loc) · 1.8 KB
/
fetchForecast.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
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
"""
This file has two uses:
1) Run this file standalone via cron to update the forecast twice a day:
env/bin/python fetchForecast.py
2) From webserver, call loadForecast periodically to get new data for the page
NOTE: Because the low temp is for the day following the actual forecast day, if we
get the forecast after midnight, we won't have access to the forecast for that
morning. Therefore, we should get the forecast twice daily - 6am and 6pm.
"""
import os
os.environ["RUNTIME_CONFIG"] = 'dev'
import arrow #http://crsmithdev.com/arrow/
import pickle
import json
from flaskapp import app, create_tables
from models import *
from wunderground import *
def loadForecast():
"Load the current forecasts from the file. Call this from webserver"
fname = app.config['FORECAST_FILE']
try: # lead tho forecasts object from previous call
with open(fname,'r') as f:
forecast = pickle.load(f)
except: # no such file - create the inital obj
forecast = None
return forecast
def getLatestForecast():
forecast = Forecast()
"Load current forecasts and update it & db"
forecast.setDaily(getWundergroundDailyForecasts())
forecast.setHourly(getWundergroundHourlyForecasts())
# save the forecasts to a file
fname = app.config['FORECAST_FILE']
with open(fname, 'w') as f:
pickle.dump(forecast, f)
return forecast
if __name__ == '__main__': # allow funcs above to be imported as a module
forecast = getLatestForecast()
print "Daily Forecasts: "
for fc in forecast.daily:
for key,val in fc.__dict__.items():
print ' %s: %s' % (key, val)
print "Hourly Forecasts: "
for fc in forecast.hourly[:12]:
for key,val in fc.__dict__.items():
print ' %s: %s' % (key, val)