-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
260 lines (242 loc) · 7.07 KB
/
script.js
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
const temp = document.getElementById("temp"),
date = document.getElementById("date-time"),
currentLocation = document.getElementById("location"),
condition = document.getElementById("condition"),
rain = document.getElementById("rain"),
mainIcon = document.getElementById("icon"),
windSpeed = document.querySelector(".wind-speed"),
humidity = document.querySelector(".humidity"),
visibility = document.querySelector(".visibility"),
humidityStatus = document.querySelector(".humidity-status"),
airQuality = document.querySelector(".air-quality"),
airQualityStatus = document.querySelector(".air-quality-status"),
visibilityStatus = document.querySelector(".visibility-status");
weatherCards = document.querySelector("#weather-cards"),
celciusBtn = document.querySelector(".celcius"),
fahrenheitBtn = document.querySelector(".fahrenheit"),
hourlyBtn = document.querySelector(".hourly"),
weekBtn = document.querySelector(".week"),
tempUnit = document.querySelectorAll(".temp-unit"),
searchForm = document.querySelector("#search"),
search = document.querySelector("#query");
let currentCity = "";
let currentUnit = "C";
let hourlyorWeek = "Week";
function getDateTime() {
let now = new Date(),
hour = now.getHours(),
minute = now.getMinutes();
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
hour = hour % 12;
if (hour < 10) {
hour = "0" + hour;
}
if (minute < 10) {
minute = "0" + minute;
}
let dayString = days[now.getDay()];
return `${dayString}, ${hour}:${minute}`;
}
date.innerText = getDateTime();
setInterval(() => {
date.innerText = getDateTime();
}, 1000);
function getPublicIp() {
fetch("https://geolocation-db.com/json/",
{
method: "GET",
})
.then((response) => response.json())
.then((data) => {
console.log(data);
currentCity = data.city;
getWeatherData(data.city, currentUnit, hourlyorWeek);
});
}
getPublicIp();
function getWeatherData(city, unit, hourlyorWeek) {
console.log(city);
const apiKey = "2VXNCLWCN2QYBZTHRJU89EP8Q";
fetch(
`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/${city}?unitGroup=metric&key=${apiKey}&contentType=json`,
{
method: "GET",
}
)
.then((response) => response.json())
.then((data) => {
let today = data.currentConditions;
if (unit === "C") {
temp.innerText = today.temp;
} else {
temp.innerText = celciusToFahrenheit(today.temp);
}
currentLocation.innerText = data.resolvedAddress;
condition.innerText = today.conditions;
rain.innerText = "Perc -" + today.precip + "%";
windSpeed.innerText = today.windspeed;
humidity.innerText = today.humidity + "%";
visibility.innerText = today.visibility;
airQuality.innerText = today.winddir;
measureHumidityStatus(today.humidity);
mainIcon.src = getIcon(today.icon);
if (hourlyorWeek === "hourly") {
updateForecast(data.days[0].hours, unit, "day");
} else {
updateForecast(data.days, unit, "week");
}
})
.catch((err)=>{
alert("Invalid Entry ");
});
}
function celciusToFahrenheit(temp) {
console.log(temp);
return ((temp * 9) / 5 + 32).toFixed(1);
}
function measureHumidityStatus(humidity) {
if (humidity <= 30) {
humidityStatus.innerText = "Low";
} else if (humidity <= 60) {
humidityStatus.innerText = "Moderate";
} else {
humidityStatus.innerText = "High";
}
}
function getIcon(condition) {
if (condition === "Partly-cloudy-day") {
return "icons/sun/27.png";
} else if (condition === "partly-cloudy-night") {
return "icons/moon/15.png";
} else if (condition === "rain") {
return "icons/rain/39.png";
} else if (condition === "clear-day") {
return "icons/sun/26.png";
} else if (condition === "clear-night") {
return "icons/moon/10.png";
} else {
return "icons/sun/26.png";
}
}
function getDayName(date) {
let day = new Date(date);
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
return days[day.getDay()];
}
function getHour(time) {
let hour = time.split(":")[0];
let min = time.split(":")[1];
if (hour > 12) {
hour = hour - 12;
return `${hour}:${min} PM`;
} else {
return `${hour}:${min} AM`;
}
}
function updateForecast(data, unit, type) {
weatherCards.innerHTML = "";
let day = 0;
let numCards = 0;
if (type === "day") {
numCards = 24;
} else {
numCards = 7;
}
for (let i = 0; i < numCards; i++) {
let card = document.createElement("div");
card.classList.add("card");
let dayName = getHour(data[day].datetime);
if (type === "week") {
dayName = getDayName(data[day].datetime);
}
let dayTemp = data[day].temp;
if (unit === "F") {
dayTemp = celciusToFahrenheit(data[day].temp);
}
let iconCondition = data[day].icon;
let iconSrc = getIcon(iconCondition);
let tempUnit = "°C";
if (unit === "F") {
tempUnit = "°F";
}
card.innerHTML = `
<h2 class="day-name">${dayName}</h2>
<div class="card-icon">
<img src="${iconSrc}" alt="" />
</div>
<div class="day-temp">
<h2 class="temp">${dayTemp}</h2>
<span class="temp-unit">${tempUnit}</span>
</div>
`;
weatherCards.appendChild(card);
day++;
}
}
fahrenheitBtn.addEventListener("click", () => {
changeUnit("F");
});
celciusBtn.addEventListener("click", () => {
changeUnit("C");
});
function changeUnit(unit) {
if (currentUnit !== unit) {
currentUnit = unit;
{
tempUnit.forEach((elem) => {
elem.innerText = `°${unit.toUpperCase()}`;
});
if (unit === "c") {
celciusBtn.classList.add("active");
fahrenheitBtn.classList.remove("active");
} else {
celciusBtn.classList.remove("active");
fahrenheitBtn.classList.add("active");
}
getWeatherData(currentCity, currentUnit, hourlyorWeek);
}
}
}
hourlyBtn.addEventListener("click", () => {
changeTimeSpan("hourly");
});
weekBtn.addEventListener("click", () => {
changeTimeSpan("week");
});
function changeTimeSpan(unit) {
if (hourlyorWeek !== unit) {
hourlyorWeek = unit;
if (unit === "hourly") {
hourlyBtn.classList.add("active");
weekBtn.classList.remove("active");
} else {
hourlyBtn.classList.remove("active");
weekBtn.classList.add("active");
}
getWeatherData(currentCity, currentUnit, hourlyorWeek);
}
}
searchForm.addEventListener("submit", (e) => {
e.preventDefault();
let location = search.value;
if (location) {
currentCity = location;
getWeatherData(currentCity, currentUnit, hourlyorWeek);
}
});