-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (64 loc) · 2.43 KB
/
script.js
File metadata and controls
71 lines (64 loc) · 2.43 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
const API_URL = "https://api.open-meteo.com/v1/forecast";
const latitude = 45.421106;
const longitude = -75.690308;
const requestURL = `${API_URL}?latitude=${latitude}&longitude=${longitude}&daily=sunrise,sunset¤t_weather=true&timezone=auto`;
const weatherCodes = {
"0": "Clear sky",
"1": "Mainly clear",
"2": "Partly cloudy",
"3": "Overcast",
"45": "Fog",
"48": "Depositing rime fog",
"51": "Drizzle: Light",
"53": "Drizzle: Moderate",
"55": "Drizzle: Dense intensity",
"56": "Freezing Drizzle: Light",
"57": "Freezing Drizzle: Dense intensity",
"61": "Rain: Slight",
"63": "Rain: Moderate",
"65": "Rain: Heavy intensity",
"66": "Freezing Rain: Light",
"67": "Freezing Rain: Heavy intensity",
"71": "Snow fall: Slight",
"73": "Snow fall: Moderate",
"75": "Snow fall: Heavy intensity",
"77": "Snow grains",
"80": "Rain showers: Slight",
"81": "Rain showers: Moderate",
"82": "Rain showers: Violent",
"85": "Snow showers: Slight",
"86": "Snow showers: Heavy",
"95": "Thunderstorm: Slight or moderate",
"96": "Thunderstorm with slight hail",
"99": "Thunderstorm with heavy hail"
};
fetch(requestURL)
.then(response => response.json())
.then(data => {
console.log(data);
// Extract current temperature from the API response
weatherCodeNumber = data.current_weather.weathercode;
let temperature = data.current_weather.temperature;
let weatherInfo = weatherCodes[weatherCodeNumber];
let windDirection = data.current_weather.winddirection;
let windSpeed = data.current_weather.windspeed;
let dateTime = data.current_weather.time;
let time = dateTime.slice(-5);
let timeOfDay = time.slice(0,1);
console.log(timeOfDay)
if (timeOfDay === "1") {
time = time.slice(-4) + "PM"
} else {
time = time.slice(-4) + "AM"
}
const template = document.querySelector('#template');
template.innerHTML = `
<div class="block">
<div id="icon"></div>
<div id="temperature">${temperature}℃</div>
<div id="weatherInfo">${weatherInfo}</div>
<div id="windDirection">Wind Direction: ${windDirection}</div>
<div id="windSpeed">Wind Speed: ${windSpeed}</div>
<div id="dateTime">${time} ${dateTime.slice(0, 10)} (hourly basis)</div>
</div>`
});