-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (105 loc) · 3.46 KB
/
main.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
const getLoc = async () => {
try {
const url = 'http://ip-api.com/json/?fields=country,city,lat,lon,timezone';
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching location:', error);
throw error;
}
};
const getWeather = async (lat, lon) => {
try {
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=8f16bfc505364bbcdd10443b420beee2`;
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching weather:', error);
throw error;
}
};
function DayOrNight() {
let DayOrNight;
const d = new Date();
if (d.getHours() >= 6 && d.getHours() <= 19) {
DayOrNight = 'Day';
} else {
DayOrNight = 'Night';
}
return DayOrNight;
}
const backGroundVideo = document.getElementById('BgVideo');
const icon = document.querySelector('.icon');
function getBackground(weatherMode) {
let videoSrc;
let iconSrc;
switch (weatherMode) {
case 'Thunderstorm':
videoSrc = '/videos/Thunder.mp4';
iconSrc = '/icons/storm.png';
break;
case 'Drizzle':
case 'Rain':
videoSrc = '/videos/Rainy.mp4';
iconSrc = '/icons/rain.png';
break;
case 'Snow':
videoSrc = '/videos/Snow.mp4';
iconSrc = '/icons/snowy.png';
break;
case 'Clear':
videoSrc = '/videos/Sunny.mp4';
iconSrc = '/icons/Sun.png';
break;
case 'Clouds':
videoSrc = '/videos/Cloudy.mp4';
iconSrc = '/icons/cloudy.png';
break;
default:
videoSrc = "./Sunny.mp4";
iconSrc = '/icons/Sun.png';
}
backGroundVideo.src = videoSrc;
backGroundVideo.load();
icon.src = iconSrc;
}
function getTemp(weTemp) {
const k = weTemp;
const f = (k - 273.15) * 9 / 5 + 32;
const c = k - 273.15;
return {
kel: Math.floor(k),
far: Math.floor(f),
can: Math.floor(c)
};
}
const locTimeZone = document.querySelector('.TimeZone');
const Temperature = document.querySelector('.Temperature');
const weatherType = document.querySelector('.weather');
const Desc = document.querySelector('.Description');
getLoc().then(locData => {
const locationTimeZone = locData.timezone;
locTimeZone.textContent = locationTimeZone;
return getWeather(locData.lat, locData.lon)
}).then(weData => {
const weatherTemp = weData.main.temp;
const weatherMode = weData.weather[0].main;
const weatherDesc = weData.weather[0].description;
Temperature.textContent = weatherTemp
const changeButtonK = document.querySelector('.changeTempK')
changeButtonK.addEventListener('click', () => {
Temperature.textContent = weatherTemp
})
const changeButtonF = document.querySelector('.changeTempF')
changeButtonF.addEventListener('click', () => {
const currentTemperature = parseFloat(Temperature.textContent); // Assuming current temperature is displayed in Kelvin
const convertedTemperatures = getTemp(currentTemperature);
// Change Kelvin to Fahrenheit
Temperature.textContent = convertedTemperatures.far + '°F';
});
weatherType.textContent = weatherMode
getBackground(weatherMode);
Desc.textContent = weatherDesc
})