-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
138 lines (127 loc) · 3.75 KB
/
Copy pathmain.js
File metadata and controls
138 lines (127 loc) · 3.75 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
//keep API key extra for easy manipulation if needed
var API_KEY = "e15a7f3fd3259dcf51aadaef984af2ba";
var cel = false;
//globally declare weather data to access it no matter what function the user is in
var wd;
//create converter function taking 2 elements: number, celcius
function displayTemp(fTemp, c){
if (c == true) {
return Math.round((fTemp - 32) * (5/9)) + " C";
} else {
return Math.round(fTemp) + " F";
}
};
//set up helper function to manipulate elements by clicking F/C toggle without re-calling the API request every time
function render (wd, cel) {
var currentLocation = wd.name;
var currentWeather = wd.weather[0].description;
var currentTemp = displayTemp(wd.main.temp, cel);
var icon = wd.weather[0].icon;
$("#location").html(currentLocation);
$("#temperature").html(currentTemp);
$("#condition").html(currentWeather);
var iconSrc = 'http://openweathermap.org/img/w/' + icon + '.png';
$("#location").prepend('<img src="' + iconSrc + '">');
// Set up video selection based on weather weatherCode
var weatherCode = wd.weather[0].id.toString();
console.log(weatherCode);
var videos = {
800: "sunshine.mp4",
801: "sunshine.mp4",
802: "sunshine.mp4",
803: "sunshine.mp4",
804: "sunshine.mp4",
904: "sunshine.mp4",
951: "sunshine.mp4",
952: "sunshine.mp4",
953: "sunshine.mp4",
954: "sunshine.mp4",
200: "greyClouds.mp4",
201: "greyClouds.mp4",
202: "greyClouds.mp4",
210: "greyClouds.mp4",
211: "greyClouds.mp4",
212: "greyClouds.mp4",
221: "greyClouds.mp4",
230: "greyClouds.mp4",
231: "greyClouds.mp4",
232: "greyClouds.mp4",
905: "greyClouds.mp4",
300: "rain.mp4",
301: "rain.mp4",
302: "rain.mp4",
310: "rain.mp4",
311: "rain.mp4",
312: "rain.mp4",
313: "rain.mp4",
314: "rain.mp4",
321: "rain.mp4",
500: "rain.mp4",
501: "rain.mp4",
502: "rain.mp4",
503: "rain.mp4",
504: "rain.mp4",
511: "rain.mp4",
520: "rain.mp4",
521: "rain.mp4",
522: "rain.mp4",
531: "rain.mp4",
600: "snow.mp4",
601: "snow.mp4",
602: "snow.mp4",
611: "snow.mp4",
612: "snow.mp4",
615: "snow.mp4",
616: "snow.mp4",
620: "snow.mp4",
621: "snow.mp4",
622: "snow.mp4",
903: "snow.mp4",
906: "snow.mp4",
701: "fog.mp4",
711: "fog.mp4",
721: "fog.mp4",
731: "fog.mp4",
741: "fog.mp4",
751: "fog.mp4",
761: "fog.mp4",
762: "fog.mp4",
771: "fog.mp4",
781: "fog.mp4",
900: "wind.mp4",
901: "wind.mp4",
902: "wind.mp4",
905: "wind.mp4",
956: "wind.mp4",
957: "wind.mp4",
958: "wind.mp4",
959: "wind.mp4",
960: "wind.mp4",
961: "wind.mp4",
962: "wind.mp4",
};
$("#myVideo").attr("src",videos[weatherCode]);
}
$(function (){
var loc;
// get user ip to determine location
$.getJSON("http://ipinfo.io", function(d){
console.log(d);
//crete new array with 2 parameters long+lat to put into GetJSON
loc = d.loc.split(",");
console.log(loc);
$.getJSON("http://api.openweathermap.org/data/2.5/weather?units=imperial&lat="
+ loc[0] + "&lon=" + loc[1] +"&APPID=" + API_KEY,
function(apiData){
//wd = weather data
// wd.weather[0] to access inner object, description to access "fewer clouds"etc
var wd = apiData;
console.log(wd);
render (wd, cel);
$("#toggle").click(function(){
cel =! cel;
render (wd, cel);
});
})
})
})