-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-daily.py
95 lines (77 loc) · 2.61 KB
/
run-daily.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pickle, ConfigParser, shutil, os.path
from datetime import timedelta, datetime
from accuweatherday import *
from persistday import *
# data is a dictionary where the keys are the 4 letter waypoint identifer
# The values are list of forecast tuples
def generate_dates(end):
day = timedelta(days=1)
cur_date = datetime.now().date()
prev_month = cur_date.month
dates = []
while (cur_date <= end_date):
dates.append(cur_date)
prev_month = cur_date.month
cur_date = cur_date + day
return dates
def refresh_data(dates, waypoints):
print "refresh_data ..."
ac = accuweatherday(aw_params)
cache_dir = time.strftime("%m-%d-%Y")
print "refreshing data for %s" % cache_dir
row_format = '"%s","%s",%s,%s,%s,%s,%s,%s,%s,%s,%s\n'
data = {'waypoints' : waypoints, 'dates' : dates}
for date in dates:
row = []
for waypoint in waypoints:
cache = "%s/%s-%s.pik" % (cache_dir, waypoint, ac.day)
forecast = ac.getforcast(waypoint, date)
row.append(forecast)
#csv_file.write(row_format % forecast)
f = open(cache, "w")
pickle.dump(forecast, f)
f.close()
ac.incrementDay()
data[date] = row
return data
def load_data(dates, waypoints):
print "load_data ..."
cache_dir = time.strftime("%m-%d-%Y")
data = {'waypoints' : waypoints, 'dates' : dates}
day = 1
for date in dates:
row = []
for waypoint in waypoints:
cache = "%s/%s-%s.pik" % (cache_dir, waypoint, day)
f = open(cache, "r")
row.append(pickle.load(f))
f.close()
day += 1
data[date] = row
return data
def get_data(dates, waypoints):
cache_dir = time.strftime("%m-%d-%Y")
if not os.path.isdir(cache_dir):
use_cache = False
os.makedirs(cache_dir)
data = refresh_data(dates, waypoints)
else:
data = load_data(dates, waypoints)
return data
## start program logic
config = ConfigParser.RawConfigParser()
config.read('flight-weather.cfg')
env = config.get('App', 'env')
dir = time.strftime("%m-%d-%Y")
isProd = False
if env == 'prod':
isProd = True
if os.path.exists(dir):
shutil.rmtree(dir)
end_date = datetime.strptime(end_date_str, "%d %b %Y").date()
dates = generate_dates(end_date)
data = get_data(dates, waypoints)
r = persistday()
r.save(data, isProd)
sendEmail("Flight Weather Daily Update Success", "The Daily update is complete")
print "Complete"