· 6 years ago · May 17, 2019, 06:56 AM
1import requests
2from datetime import datetime, timedelta
3import time
4import sqlite3
5conn = sqlite3.connect('historical_weather.db') # establish database
6c = conn.cursor() # assign variable to c for short key cursor
7
8
9# function to get weather forecast
10# api_forecast_mode = currently, hourly, daily
11api_forecast_mode = 'daily'
12# json_return_int = 0, 1, 2 etc.
13json_return_int = 0
14
15api_items = [
16 'time',
17 'summary',
18 'sunriseTime',
19 'sunsetTime',
20 'moonPhase',
21 'precipIntensity',
22 'precipIntensityMax',
23 'precipIntensityMaxTime',
24 'precipProbability',
25 'precipType',
26 'temperatureHigh',
27 'temperatureHighTime',
28 'temperatureLow',
29 'temperatureLowTime',
30 'apparentTemperatureHigh',
31 'apparentTemperatureHighTim',
32 'apparentTemperatureLow',
33 'apparentTemperatureLowTime',
34 'dewPoint',
35 'humidity',
36 'pressure',
37 'windSpeed',
38 'windGust',
39 'windGustTime',
40 'windBearing',
41 'cloudCover',
42 'uvIndex',
43 'uvIndexTime',
44 'visibility',
45 'ozone',
46 'temperatureMin',
47 'temperatureMinTime',
48 'temperatureMax',
49 'temperatureMaxTime',
50 'apparentTemperatureMin',
51 'apparentTemperatureMinTime',
52 'apparentTemperatureMax',
53 'apparentTemperatureMaxTime',
54]
55#THIS IS WHERE I HAVE A PROBLEM
56def get_forecast_weather(api_forecast_mode, json_return_int):
57 f_list = []
58 for api_item in api_items:
59 f_list.append(api_item)
60 for f_list_item in f_list:
61 if f_list_item == "f_run_time":
62 f_list_item = "datetime.today()"
63 elif f_list_item == "f_api_calls":
64 f_list_item = "response.headers['x-forecast-api-calls']"
65 else:
66 f_list_item = (data['daily']['data'][search_date][f_list_item])
67 print (f_list_item)
68def unix_convert_time(api_forecast_mode, json_return_int, field):
69 unix_field = (datetime.utcfromtimestamp(data[api_forecast_mode]['data'][json_return_int][field]).strftime('%H:%M:%S'))
70 return unix_field
71
72
73def unix_convert_date(api_forecast_mode, json_return_int, field):
74 unix_field = (datetime.utcfromtimestamp(data[api_forecast_mode]['data'][json_return_int][field]).strftime('%Y-%m-%d'))
75 return unix_field
76
77# function to create table for forecast weather data
78def create_forecast_weather_table():
79 c.execute("""CREATE TABLE IF NOT EXISTS forecast_weather_tbl (
80 f_run_time text,
81 f_date text,
82 f_summary text,
83 f_sunriseTime text,
84 f_sunsetTime text,
85 f_moonPhase text,
86 f_precipIntensity text,
87 f_precipIntensityMax text,
88 f_precipIntensityMaxTime text,
89 f_precipProbability text,
90 f_precipType text,
91 f_temperatureHigh text,
92 f_temperatureHighTime text,
93 f_temperatureLow text,
94 f_temperatureLowTime text,
95 f_apparentTemperatureHigh text,
96 f_apparentTemperatureHighTim text,
97 f_apparentTemperatureLow text,
98 f_apparentTemperatureLowTime text,
99 f_dewPoint text,
100 f_humidity text,
101 f_pressure text,
102 f_windSpeed text,
103 f_windGust text,
104 f_windGustTime text,
105 f_windBearing text,
106 f_cloudCover text,
107 f_uvIndex text,
108 f_uvIndexTime text,
109 f_visibility text,
110 f_ozone text,
111 f_temperatureMin text,
112 f_temperatureMinTime text,
113 f_temperatureMax text,
114 f_temperatureMaxTime text,
115 f_apparentTemperatureMin text,
116 f_apparentTemperatureMinTime text,
117 f_apparentTemperatureMax text,
118 f_apparentTemperatureMaxTime text,
119 f_api_calls text
120 )""")
121
122
123# function to insert the data from the get_weather function into current data table
124def insert_forecast_weather_data():
125 c.execute("""INSERT INTO forecast_weather_tbl
126 (
127 f_run_time,
128 f_date,
129 f_summary,
130 f_sunriseTime,
131 f_sunsetTime,
132 f_moonPhase,
133 f_precipIntensity,
134 f_precipIntensityMax,
135 f_precipIntensityMaxTime,
136 f_precipProbability,
137 f_precipType,
138 f_temperatureHigh,
139 f_temperatureHighTime,
140 f_temperatureLow,
141 f_temperatureLowTime,
142 f_apparentTemperatureHigh,
143 f_apparentTemperatureHighTim,
144 f_apparentTemperatureLow,
145 f_apparentTemperatureLowTime,
146 f_dewPoint,
147 f_humidity,
148 f_pressure,
149 f_windSpeed,
150 f_windGust,
151 f_windGustTime,
152 f_windBearing,
153 f_cloudCover,
154 f_uvIndex,
155 f_uvIndexTime,
156 f_visibility,
157 f_ozone,
158 f_temperatureMin,
159 f_temperatureMinTime,
160 f_temperatureMax,
161 f_temperatureMaxTime,
162 f_apparentTemperatureMin,
163 f_apparentTemperatureMinTime,
164 f_apparentTemperatureMax,
165 f_apparentTemperatureMaxTime,
166 f_api_calls) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
167 list(get_forecast_weather(api_forecast_mode, json_return_int).values())
168 )
169
170create_forecast_weather_table()
171
172# time to offset in hours converted to seconds for the api response
173api_time_offset = 0
174
175# Number of days to go back for historical records
176daysToSubtract = range(200)
177
178#Begin loop
179for dayToSubtract in daysToSubtract:
180 # Convert day into unix time for the API request
181 search_date = datetime.today()
182 unixDayInTime = search_date - timedelta(days=dayToSubtract)
183 # Convert to int to remove decimal float, then convert to unix time
184 unixDayInTime = int((unixDayInTime - datetime(1970, 1, 1, 00, 00, 00)).total_seconds()) + api_time_offset
185 # Get weather data via api in json format and convert to object for parsing
186 response = requests.get(
187 'https://api.darksky.net/forecast/MYAPIKEY/COORDX,COORDY,' + str(unixDayInTime) + '?exclude=currently,hourly,flags')
188 data = response.json()
189 # Function to retrieve day and hour
190 get_forecast_weather(api_forecast_mode, json_return_int)
191 insert_forecast_weather_data()
192 print('Completed',dayToSubtract, "/", '135')
193 time.sleep(2)
194
195# commit changes to database and close
196conn.commit()
197c.close()
198conn.close()
199print("Done")