-
Notifications
You must be signed in to change notification settings - Fork 56
/
wunderground_historical.py
121 lines (109 loc) · 4.47 KB
/
wunderground_historical.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import urllib2
import json
import os
import glob
import time
from datetime import timedelta, date
from ISStreamer.Streamer import Streamer
# --------- User Settings ---------
STATE = "CA"
CITY = "San_Francisco"
WUNDERGROUND_API_KEY = "PLACE YOUR WUNDERGROUND API KEY HERE"
BUCKET_NAME = ":partly_sunny: " + CITY + " Weather"
BUCKET_KEY = "wu1"
ACCESS_KEY = "PLACE YOUR INITIAL STATE ACCESS KEY HERE"
STARTDATE = date(YYYY,MM,DD)
ENDDATE = date(YYYY,MM,DD)
SECONDS_BETWEEN_SEND = 5
# ---------------------------------
def isFloat(string):
try:
float(string)
if float(string) < 0:
raise ValueError
return True
except ValueError:
return False
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
def get_conditions(readDate):
api_conditions_url = "http://api.wunderground.com/api/" + WUNDERGROUND_API_KEY + "/history_" + readDate + "/q/" + STATE +"/"+ CITY + ".json"
try:
f = urllib2.urlopen(api_conditions_url)
except:
print "Failed to get conditions"
return False
json_conditions = f.read()
f.close()
return json.loads(json_conditions)
streamer = Streamer(bucket_name=BUCKET_NAME, bucket_key=BUCKET_KEY, access_key=ACCESS_KEY)
while True:
for single_date in daterange(STARTDATE,ENDDATE):
conditions = get_conditions(single_date.strftime("%Y%m%d"))
if (conditions != False):
for i in range(len(conditions['history']['observations'])):
dateInfo = conditions['history']['observations'][i]['date']
date = dateInfo['mday']+"."+dateInfo['mon']+"."+dateInfo['year']+" "+dateInfo['hour']+":"+dateInfo['min']+":00"
print date
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date, pattern)))
humidity_wu = conditions['history']['observations'][i]['hum']
temp_wu = conditions['history']['observations'][i]['tempi']
pressure_wu = conditions['history']['observations'][i]['pressurem']
precip_wu = conditions['history']['observations'][i]['precipi']
wind_speed_wu = conditions['history']['observations'][i]['wspdi']
# dewpt_wu = conditions['history']['observations'][i]['dewpti']
# wind_gust_wu = conditions['history']['observations'][i]['wgusti']
# wind_dir_wu = conditions['history']['observations'][i]['wdird']
# vis_wu = conditions['history']['observations'][i]['visi']
# wind_chill_wu = conditions['history']['observations'][i]['windchilli']
# heat_index_wu = conditions['history']['observations'][i]['heatindexi']
# conds_wu = conditions['history']['observations'][i]['conds']
# fog_wu = conditions['history']['observations'][i]['fog']
# rain_wu = conditions['history']['observations'][i]['rain']
# snow_wu = conditions['history']['observations'][i]['snow']
# hail_wu = conditions['history']['observations'][i]['hail']
# thunder_wu = conditions['history']['observations'][i]['thunder']
# tornado_wu = conditions['history']['observations'][i]['tornado']
if isFloat(humidity_wu):
streamer.log("WU Humidity (%)",humidity_wu,epoch)
if isFloat(temp_wu):
streamer.log("WU Temp",temp_wu,epoch)
if isFloat(pressure_wu):
streamer.log("WU Pressure",pressure_wu,epoch)
if isFloat(precip_wu):
streamer.log("WU Rain",precip_wu,epoch)
if isFloat(wind_speed_wu):
streamer.log("WU Wind Speed",wind_speed_wu,epoch)
# if isFloat(dewpt_wu):
# streamer.log("WU Dew Point",dewpt_wu,epoch)
# if isFloat(wind_gust_wu):
# streamer.log("WU Wind Gust",wind_gust_wu,epoch)
# if isFloat(wind_dir_wu):
# streamer.log("WU Wind Direction",wind_dir_wu,epoch)
# if isFloat(vis_wu):
# streamer.log("WU Visibility",vis_wu,epoch)
# if isFloat(wind_chill_wu):
# streamer.log("WU Wind Chill",wind_chill_wu,epoch)
# if isFloat(heat_index_wu):
# streamer.log("WU Heat Index",heat_index_wu,epoch)
# if isFloat(conds_wu):
# streamer.log("WU Conditions",conds_wu,epoch)
# if isFloat(fog_wu):
# streamer.log("WU Fog",fog_wu,epoch)
# if isFloat(rain_wu):
# streamer.log("WU Rain",rain_wu,epoch)
# if isFloat(snow_wu):
# streamer.log("WU Snow",snow_wu,epoch)
# if isFloat(hail_wu):
# streamer.log("WU Hail",hail_wu,epoch)
# if isFloat(thunder_wu):
# streamer.log("WU Thunder",thunder_wu,epoch)
# if isFloat(tornado_wu):
# streamer.log("WU Tornado",tornado_wu,epoch)
streamer.flush()
time.sleep(SECONDS_BETWEEN_SEND)
if single_date == ENDDATE:
print("All dates streamed")
exit()