-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
263 lines (233 loc) · 9.76 KB
/
script.js
File metadata and controls
263 lines (233 loc) · 9.76 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
let searchB = document.getElementById("searchB")
searchB.addEventListener("click",loadWeather)
let searchInput = document.getElementById("searchInp")
searchInput.addEventListener("input",hideIt)
let predictB = document.getElementById("predict")
predictB.addEventListener("click",displayPanel)
let panel = document.getElementById("panel")
function displayPanel(){
panel.style="display:flex"
}
window.addEventListener("load",async function(){
const response = await fetch("https://api.geoapify.com/v1/ipinfo?&apiKey=fedfad0353b44eac877bb867ef618aef")
const apiData = await response.json()
console.log(apiData)
currentWeather(apiData.city.name)
})
// THis works
function hideIt(){
predictB.style="display:none"
panel.style="display:none"
panel.innerHTML=""
console.log('************************************************')
}
let box2 = document.getElementById("box2")
let box3 = document.getElementById("box3")
let box4 = document.getElementById("box4")
let box34 = document.getElementById("box34")
async function currentWeather(currentCity){
box2.style="background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);"
box34.style="background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);"
console.log(currentCity)
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${currentCity}&appid=42d28598d8498ed311b177ab7b170f2b`)
const apiData = await response.json()
console.log(apiData)
let city = apiData.name
let country = apiData.sys.country
console.log(city+", "+country)
let weather = apiData.weather[0].main
console.log(weather)
let weatherDesc = apiData.weather[0].description
console.log(weatherDesc)
// in K
let temperature = apiData.main.temp
temperature = parseInt(temperature - 273)
console.log(temperature)
// in ms
let windSpeed = apiData.wind.speed
console.log(windSpeed)
// total percenrtage amount of moisture it could hold at the current temperature
let humidity = apiData.main.humidity
console.log(humidity)
// clear weather - sunny
// overcast clouds - overcast
// broken/scattered clouds - clouds
// thunder
let imageURL= ""
if (weatherDesc.includes("clear sky")){
imageURL = "sun.png"
}
else if(weatherDesc.includes("rain")){
imageURL = "rain.png"
}
else if(weatherDesc.includes("snow")){
imageURL = "snowflake.png"
}
else if(weatherDesc.includes("smoke")||weatherDesc.includes("haze")){
imageURL = "haze.png"
}
else if(weatherDesc.includes("overcast")){
imageURL = "overcast.png"
}
else if(weatherDesc.includes("few clouds")){
imageURL = "cloudy-day.png"
}
else if(weatherDesc.includes("clouds")){
imageURL = "clouds.png"
}
else if(weatherDesc.includes("thunderstorm")){
imageURL = "thunderstorm.png"
}
else if(weatherDesc.includes("dust")){
imageURL = "dust.png"
}
else if(weatherDesc.includes("drizzle")){
imageURL = "drizzle.png"
}
else if(weatherDesc.includes("mist")){
imageURL = "mist.png"
}
predictB.style="display:block"
box2.innerHTML=`<img src="${imageURL}" alt="${city}" width="170px">
<p style="font-size:24px">${temperature}<sup>°</sup>C, ${weatherDesc}</p>
<p style="font-size:20px">${city}, ${country}</p>`
box3.innerHTML=`<img src="winds.png" width="35px">
<p style="font-size:14px">Wind speed: ${windSpeed} m/s</p>`
box4.innerHTML=`<img title="total percentage amount of moisture it could hold at the current temperature" src="humidity.png" width="35px">
<p title="total percentage amount of moisture it could hold at the current temperature" style="font-size:14px">Humidity: ${humidity}%</p>`
console.log(apiData.coord.lon)
console.log(apiData.coord.lat)
predictWeather(apiData.coord.lon,apiData.coord.lat)
}
async function loadWeather(){
let userInput = document.getElementById("searchInp").value
box2.style="background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);"
box34.style="background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);"
console.log(userInput)
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${userInput}&appid=42d28598d8498ed311b177ab7b170f2b`)
const apiData = await response.json()
console.log(apiData)
let city = apiData.name
let country = apiData.sys.country
console.log(city+", "+country)
let weather = apiData.weather[0].main
console.log(weather)
let weatherDesc = apiData.weather[0].description
console.log(weatherDesc)
// in K
let temperature = apiData.main.temp
temperature = parseInt(temperature - 273)
console.log(temperature)
// in ms
let windSpeed = apiData.wind.speed
console.log(windSpeed)
// total percenrtage amount of moisture it could hold at the current temperature
let humidity = apiData.main.humidity
console.log(humidity)
// clear weather - sunny
// overcast clouds - overcast
// broken/scattered clouds - clouds
// thunder
let imageURL= ""
if (weatherDesc.includes("clear sky")){
imageURL = "sun.png"
}
else if(weatherDesc.includes("rain")){
imageURL = "rain.png"
}
else if(weatherDesc.includes("snow")){
imageURL = "snowflake.png"
}
else if(weatherDesc.includes("smoke")||weatherDesc.includes("haze")){
imageURL = "haze.png"
}
else if(weatherDesc.includes("overcast")){
imageURL = "overcast.png"
}
else if(weatherDesc.includes("few clouds")){
imageURL = "cloudy-day.png"
}
else if(weatherDesc.includes("clouds")){
imageURL = "clouds.png"
}
else if(weatherDesc.includes("thunderstorm")){
imageURL = "thunderstorm.png"
}
else if(weatherDesc.includes("dust")){
imageURL = "dust.png"
}
else if(weatherDesc.includes("drizzle")){
imageURL = "drizzle.png"
}
else if(weatherDesc.includes("mist")){
imageURL = "mist.png"
}
predictB.style="display:block"
box2.innerHTML=`<img src="${imageURL}" alt="${city}" width="170px">
<p style="font-size:24px">${temperature}<sup>°</sup>C, ${weatherDesc}</p>
<p style="font-size:20px">${city}, ${country}</p>`
box3.innerHTML=`<img src="winds.png" width="35px">
<p style="font-size:14px">Wind speed: ${windSpeed} m/s</p>`
box4.innerHTML=`<img title="total percentage amount of moisture it could hold at the current temperature" src="humidity.png" width="35px">
<p title="total percentage amount of moisture it could hold at the current temperature" style="font-size:14px">Humidity: ${humidity}%</p>`
console.log(apiData.coord.lon)
console.log(apiData.coord.lat)
predictWeather(apiData.coord.lon,apiData.coord.lat)
}
async function predictWeather(longitude,latitude){
const response = await fetch(`https://api.openweathermap.org/data/2.5/forecast?lat=${latitude}&lon=${longitude}&appid=0014c74507042b98f4f31e3090c4a218`);
const apiData = await response.json();
console.log(apiData);
for(let i=0;i<apiData.list.length;i++){
let date_time = apiData.list[i].dt_txt
let weather_description = apiData.list[i].weather[0].description
let temp = apiData.list[i].main.temp - 273 //To C
console.log("Date & Time : ",date_time)
console.log("Hourly-weather: ",weather_description)
let imageURL= ""
if (weather_description.includes("clear sky")){
imageURL = "sun.png"
}
else if(weather_description.includes("rain")){
imageURL = "rain.png"
}
else if(weather_description.includes("snow")){
imageURL = "snowflake.png"
}
else if(weather_description.includes("smoke")||weather_description.includes("haze")){
imageURL = "haze.png"
}
else if(weather_description.includes("overcast")){
imageURL = "overcast.png"
}
else if(weather_description.includes("few clouds")){
imageURL = "cloudy-day.png"
}
else if(weather_description.includes("clouds")){
imageURL = "clouds.png"
}
else if(weather_description.includes("thunderstorm")){
imageURL = "thunderstorm.png"
}
else if(weather_description.includes("dust")){
imageURL = "dust.png"
}
else if(weather_description.includes("drizzle")){
imageURL = "drizzle.png"
}
else if(weather_description.includes("mist")){
imageURL = "mist.png"
}
//Below is not working because display:none in html style attribute
//now this is fixed after changing dispay property from block to flex in displayPanel() function
panel.innerHTML+=` <div style="height:110px;width:82px;border-right:2px solid black;border-bottom:2px solid black;display:flex;align-items:center;justify-content:space-around;flex-direction:column;">
<div style="text-align:center">${date_time}</div>
<div><img src=${imageURL} width="35px"></div>
<div style="text-align:center">${weather_description}</div>
</div>`
//Now just empty previous innerHTML of panel
//Done !
//Error kind of -> heavy intensity rain overflows the panel box
// Fix that
}
}