-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathweather-api.js
More file actions
420 lines (295 loc) · 16.9 KB
/
weather-api.js
File metadata and controls
420 lines (295 loc) · 16.9 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// version 1: Weather API
// Goal: when search button is clicked, run functions that will:
// 1: check user input date against current date and select current weather query or forecast query
// 2: use information entered by user to query Weatherbit.io for current and future weather conditions
// 3: generate weather photos loosely based on weather conditions
// **GLOBAL VARIABLES** //
// define variable for html search button
var searchButton = $(".button");
// define current date using a moment
var currentDate = moment().format('YYYY-MM-DD');
var currentHour = moment().format('HH');
// console.log('WA: This is the current date: ' + currentDate);
// define variables that capture the city, state, and date indicated by user, by default are Chicago
var cityInput = "Chicago";
var stateInput = "IL";
var dateInput = currentDate
// console.log(dateInput);
// define variables that represent the WeatherBit APIKey
var APIKey = "faa9f8bb779e4165b52c0af7edcdbf68";
//set date with full word month and day format
var currentDateMD = moment().format('MMMM D');
//set variable that captures weather image div and applies
var weatherImage = $('#weather-image').attr('src', 'images/weather/wfog.jpg');
//define weather element to which current weather will be added
var weatherCard = $('#weather-primary').addClass('has-text-white is-overlay has-text-weight-semibold has-text-left');
//define weather forecast element to which projected forecast will be added
var weatherForecastID = $('#weather-forecast.content').addClass('has-text-left');
//define variable into which weather icon data will be pushed for photos
var weatherIcon = "";
// **CLICK EVENT** //
//create on click event trigged by main page Search button that will check user inputs and initiate queries
searchButton.on("click", selectQuery);
// **FUNCTIONS & AJAX CALLS** //
// check user input date against current date and select current weather query or forecast query
function selectQuery () {
// captures the city, state and date variables to values entered by the user
cityInput = $('#City').val();
stateInput = $('#state').val();
dateInput = $('#date').val();
// console log result of user entry
// console.log('WA-sQ: Date entered: ' + dateInput);
// console.log('WA-sQ: City entered: ' + cityInput);
// console.log('WA-sQ: State entered: ' + stateInput);
// check date user entered against the current date to determine whether current or future weather query must be run
if (dateInput === currentDate) {
// console.log('WA-sQ: Dates both = today, query for current weather');
displayCTWeather ()
}
else if (dateInput > currentDate) {
// console.log("WA-sQ: Date entered is later than today's date, query for forecast");
displayFTWeather ();
}
// end select query function
}
// query current weather conditions and render details
function displayCTWeather () {
// define variable that represents current conditions query info
var queryCTWeather = "https://api.weatherbit.io/v2.0/current?" + "city=" + cityInput + "," + stateInput + "&units=I" + "&key=" + APIKey;
$.ajax({
url: queryCTWeather,
method: "GET"
})
// store retrieved data inside response object
.then(function(response) {
// console log response object
console.log('WA-cW: current weather query was run');
// console.log(response);
//clear sections of any existing divs
weatherCard.empty();
weatherForecastID.empty();
weatherIcon = "";
var wCityName = response.data[0].city_name;
// set variables for returned temperature and round up
var wTempNow = Math.ceil(response.data[0].temp);
var wTempFeels = Math.ceil(response.data[0].app_temp) + String.fromCharCode(176);
// set remaining weather variables
var wUV = Math.ceil(response.data[0].uv);
var wHumidity = response.data[0].rh;
var wDescription = response.data[0].weather.description;
var wWindSpeed = response.data[0].wind_spd;
var wIcon = response.data[0].weather.icon;
//update weather icon for set Icon function
weatherIcon = wIcon;
// create divs and append to weather card
//define variables for creating first level div
var levelCurrentDiv = $('<div id="city-temp">').addClass('level is-mobile is-marginless');
//define variables that will be appended to the first level div
var wcityNameDiv = $('<span id=city-name>').addClass('is-size-3 level-left').text(wCityName);
var wtempCurrentDiv = $('<div id=temp-current>').addClass('is-size-3 level-right').text(wTempNow + String.fromCharCode(176));
//define variables for creating second level div
var levelDetailDiv = $('<div id="temp-details">').addClass('level is-mobile');
//define variables that will be appended to the second level div
var wDescriptionDiv = $('<div id=description>').addClass('level-left').text(wDescription);
var wtempIcon = $('<img width=60px height=60px>').attr('src', "https://www.weatherbit.io/static/img/icons/" + wIcon + ".png")
//define remaining weather variables and divs to be created
var wtempFeelsDiv = $('<div id=temp-feels>').text("Feels like: "
+ wTempFeels);
var wUVDiv = $('<div id=uv>').text("UV Index: " + wUV);
var wHumidityDiv = $('<div id=humidity>').text("Humidity: " + wHumidity + "%");
var wWindSpeedDiv = $('<div id=humidity>').text("Wind Speed: " + wWindSpeed + " mph");
//append weather details and divs to weather card
weatherCard.append(levelCurrentDiv, levelDetailDiv, wtempFeelsDiv, wUVDiv, wHumidityDiv, wWindSpeedDiv);
levelCurrentDiv.append(wcityNameDiv, wtempIcon);
levelDetailDiv.append(wtempCurrentDiv, wDescriptionDiv);
//run projected forecast function to add future weather details to card
addProjectedFT ();
setWIconImage ();
// console log results
// console.log('This is the current temp: ' + wTempNow);
// console.log('Feels like: ' + wTempFeels);
// console.log('This is the UV Index: ' + wUV);
// console.log('The humidity is: ' + wHumidity);
// console.log('The weather is: ' + wDescription);
//end ajax call
});
// end displayWeatherCT function
}
// query forecast weather conditions and render details
function displayFTWeather () {
// define variable that represents forecast conditions query info
var queryFTWeather = "https://api.weatherbit.io/v2.0/forecast/daily?" + "city=" + cityInput + "," + stateInput + "&units=I" + "&key=" + APIKey;
$.ajax({
url: queryFTWeather,
method: "GET"
})
// store retrieved data inside response object
.then(function(response) {
//console log type of response run
console.log('WA-cW: forecast weather query was run');
// console.log(response);
//clear sections of any existing divs
weatherCard.empty();
weatherForecastID.empty();
weatherIcon = "";
//define variable that will update to yes if date entered by user falls within forecast dates provided by forecast response
var result = "";
// set variable to return number value of response length
// var responseLength = response.data.length;
// console.log('This is the response length: ' + responseLength);
// run for loop that checks that the date user entered is within the scope of the returned forecast information from the response object
for (var i = 0; i < response.data.length; i++) {
if (dateInput === response.data[i].valid_date) {
// console.log("WA-fW: dateInput" + " = " + response.data[i].valid_date + " so provide weather details");
result = 'yes';
// console.log("WA-fw: " + result);
// set variables for returned temperature and round up
var wftemp = Math.ceil(response.data[i].temp);
var wftempLow = Math.ceil(response.data[i].low_temp);
var wftempHigh = Math.ceil(response.data[i].high_temp);
var wfcityName = cityInput;
// set variables for UV Index, Humidity, Description and Icon
var wfHumidity = response.data[i].rh;
var wfUV = Math.ceil(response.data[i].uv);
var wfDescription = response.data[i].weather.description;
var wfWindSpeed = response.data[i].wind_spd;
var wfIcon = response.data[i].weather.icon;
weatherIcon = wfIcon;
// create divs and append to card in index file
//define variables for creating first level div
var levelCurrentDiv = $('<div id="city-temp">').addClass('level is-mobile is-marginless');
//define variables that will be appended to the first level div
var wfcityNameDiv = $('<span id=city-name>').text(wfcityName).addClass('is-size-3 level-left');
var wftempForecastDiv = $('<div id=temp-forecast>').addClass('is-size-3 level-right').text(wftemp + String.fromCharCode(176));
//define variables for creating second level div
var levelDetailDiv = $('<div id="temp-details">').addClass('level is-mobile');
//define variables that will be appended to the second level div
var wfDescriptionDiv = $('<div id=description>').text(wfDescription).addClass('level-left');
var wftempIcon = $('<img width=60px height=60px>').attr('src', "https://www.weatherbit.io/static/img/icons/" + wfIcon + ".png").addClass('level-right');
//define remaining weather variables and divs to be created
var wftempLowDiv = $('<span id=temp-low>').text("Low: " + wftempLow + " " + String.fromCharCode(176) + " " + String.fromCharCode(8226));
var wftempHighDiv = $('<span id=temp-high>').text(" " + " High: " + wftempHigh + String.fromCharCode(176));
var wfUVDiv = $('<div id=uv>').text("UV Index: " + wfUV);
var wfHumidityDiv = $('<div id=humidity>').text("Humidity: " + wfHumidity + "%");
var wfWindSpeedDiv = $('<div id=humidity>').text("Wind Speed: " + wfWindSpeed + " mph");
//append weather details and divs to weather card
weatherCard.append(levelCurrentDiv, levelDetailDiv, wftempLowDiv, wftempHighDiv, wfUVDiv, wfHumidityDiv, wfWindSpeedDiv);
levelCurrentDiv.append(wfcityNameDiv, wftempIcon);
levelDetailDiv.append(wftempForecastDiv, wfDescriptionDiv);
//console log results
// console.log('The temp will be: ' + wftemp);
// console.log('The temp low will be: ' + wftempLow);
// console.log('The temp high will be: ' + wftempHigh);
// console.log('The humidity will be: ' + wfHumidity);
// console.log('THe UV Index will be: ' + wfUV);
// console.log('The weather will be: ' + wfDescription);
//close if statement
}
// close for loop
}
// if result not found for date entered by user, indicate weather information is not yet available
if (result !== 'yes') {
// console.log('The weather is not yet available for this date. Stay tuned!');
var noResults = $('<p>').text("The weather is not yet available for this date. Stay tuned!");
weatherCard.append(noResults);
} else if (result == 'yes') {
//run projected forecast function to add future weather details to card
addProjectedFT ();
// end else statement
}
//run function to update image paired with weather icon
setWIconImage ();
// end ajax call
});
}
//query forecast weather and render projected forecast to card
function addProjectedFT () {
//clear sections of any existing divs
weatherForecastID.empty();
// define variable that represents forecast conditions query info
var queryForecast = "https://api.weatherbit.io/v2.0/forecast/daily?" + "city=" + cityInput + "," + stateInput + "&units=I" + "&key=" + APIKey;
$.ajax({
url: queryForecast,
method: "GET"
})
// store retrieved data inside response object
.then(function(response) {
//create for loop for adding projected forecast section to card
for (var j = 1; j < 6; j++) {
//set var for Month Date
var longDate = moment(dateInput, 'YYYY-MM-DD').add(+j, 'days').format('MMMM D');
//set var for dateInput + 1 day to represent forecast dates to pull back
var updatedDate = moment(dateInput, 'YYYY-MM-DD').add(+j, 'days').format('YYYY-MM-DD');
// console.log("This is the long date: " + longDate);
// console.log("This is the updated date: " + updatedDate);
//run through the full list of data from the response
for (var i = 0; i < response.data.length; i++) {
// console.log("This is the updated date: " + updatedDate);
// console.log("This is the response date searched: " + response.data[i].valid_date);
//if the updated date input matches a date in the response data, create forecast divs
if (updatedDate === response.data[i].valid_date) {
//create variables
var wpfTempLow = Math.ceil(response.data[i].low_temp);
var wpfTempHigh = Math.ceil(response.data[i].high_temp);
var wpfIcon = response.data[i].weather.icon
//create divs
var longDateDiv = $('<div>').text(longDate).addClass('has-text-grey-darker');
var wpfTempLowDiv = $('<div>').text("L: " + wpfTempLow + String.fromCharCode(176)).addClass('has-text-grey');
var wpfTempHighDiv = $('<div>').text("H: " + wpfTempHigh + String.fromCharCode(176));
var wpfTempIcons = $('<img width=40px height=40px>').attr('src', "https://www.weatherbit.io/static/img/icons/" + wpfIcon + ".png");
var levelProjFT = $('<div id="projected">').addClass('level is-mobile is-marginless');
weatherForecastID.append(levelProjFT);
levelProjFT.append(longDateDiv, wpfTempIcons, wpfTempHighDiv, wpfTempLowDiv);
// end if statement
}
// end internal for loop
}
//end for loop
}
// end then response
});
// close projectedFT function
}
//generate photo background for card based on weather icon
function setWIconImage () {
// console.log(weatherIcon);
// console.log(dateInput == currentDate);
// console.log(cityInput == "Chicago")
// console.log(currentHour > 17)
// console.log(currentHour)
//if thunderstorm
if (weatherIcon.startsWith('t')) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wthunder.jpg');
}
//if drizzle, rain, or unknown precipitation
if (weatherIcon.startsWith('f') || weatherIcon.startsWith('d') || weatherIcon.startsWith('r') || weatherIcon.startsWith('u')) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wrain.jpg');
}
//if snow
if (weatherIcon.startsWith('s') || weatherIcon.startsWith('a')) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wsnow.jpg');
}
//if fog
if (weatherIcon.startsWith('a')) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wfog.jpg');
}
//if clear or clouds
//if clear or clouds and the city is not Chicago, display the wclear image
if (weatherIcon.startsWith('c') && cityInput !== "Chicago") {
weatherImage = $('#weather-image').attr('src', 'images/weather/wclear3.jpg');
//if clear or clouds and the city input is Chicago
} else if (weatherIcon.startsWith('c') && cityInput == "Chicago") {
//if during the day today or a future date, set generic clear photo
if (dateInput == currentDate && currentHour < 17 || dateInput !== currentDate) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wclear3.jpg');
}
//if it's today and it's the evening, display an evening variable
if (dateInput == currentDate && currentHour >= 17) {
weatherImage = $('#weather-image').attr('src', 'images/weather/wnight2.jpg');
}
//end else if statement
}
// end set icon image function
}
//run the display current weather function at startup
displayCTWeather ();