-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
125 lines (117 loc) · 4.24 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
const temp = document.getElementById("temp"),
date = document.getElementById("date-time"),
condition = document.getElementById("condition"),
rain = document.getElementById("rain"),
mainIcon = document.getElementById("icon"),
currentLocation = document.getElementById("location"),
uvIndex = document.querySelector(".uv-index"),
uvText = document.querySelector(".uv-text"),
windSpeed = document.querySelector(".wind-speed"),
sunRise = document.querySelector(".sun-rise"),
sunSet = document.querySelector(".sun-set"),
humidity = document.querySelector(".humidity"),
visibilty = document.querySelector(".visibilty"),
humidityStatus = document.querySelector(".humidity-status"),
airQuality = document.querySelector(".air-quality"),
airQualityStatus = document.querySelector(".air-quality-status"),
visibilityStatus = document.querySelector(".visibilty-status"),
searchForm = document.querySelector("#search"),
search = document.querySelector("#query"),
celciusBtn = document.querySelector(".celcius"),
fahrenheitBtn = document.querySelector(".fahrenheit"),
tempUnit = document.querySelectorAll(".temp-unit"),
hourlyBtn = document.querySelector(".hourly"),
weekBtn = document.querySelector(".week"),
weatherCards = document.querySelector("#weather-cards");
let currentCity = "";
let currentUnit = "c";
let hourlyorWeek = "week";
// function to get date and time
function getDateTime() {
let now = new Date(),
hour = now.getHours(),
minute = now.getMinutes();
let days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
// 12 hours format
hour = hour % 12;
if (hour < 10) {
hour = "0" + hour;
}
if (minute < 10) {
minute = "0" + minute;
}
let dayString = days[now.getDay()];
return `${dayString}, ${hour}:${minute}`;
}
//Updating date and time
date.innerText = getDateTime();
setInterval(() => {
date.innerText = getDateTime();
}, 1000);
// function to get public ip address
function getPublicIp() {
fetch("https://geolocation-db.com/json/", {
method: "GET",
headers: {},
})
.then((response) => response.json())
.then((data) => {
currentCity = data.city;
getWeatherData(data.city, currentUnit, hourlyorWeek);
})
.catch((err) => {
console.error(err);
});
}
getPublicIp();
// function to get weather data
function getWeatherData(city, unit, hourlyorWeek) {
fetch(
`https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/${city}?unitGroup=metric&key=EJ6UBL2JEQGYB3AA4ENASN62J&contentType=json`,
{
method: "GET",
headers: {},
}
)
.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 + "%";
uvIndex.innerText = today.uvindex;
windSpeed.innerText = today.windspeed;
measureUvIndex(today.uvindex);
mainIcon.src = getIcon(today.icon);
changeBackground(today.icon);
humidity.innerText = today.humidity + "%";
updateHumidityStatus(today.humidity);
visibilty.innerText = today.visibility;
updateVisibiltyStatus(today.visibility);
airQuality.innerText = today.winddir;
updateAirQualityStatus(today.winddir);
if (hourlyorWeek === "hourly") {
updateForecast(data.days[0].hours, unit, "day");
} else {
updateForecast(data.days, unit, "week");
}
sunRise.innerText = covertTimeTo12HourFormat(today.sunrise);
sunSet.innerText = covertTimeTo12HourFormat(today.sunset);
})
.catch((err) => {
alert("City not found in our database");
});
}