Skip to content

Commit 1e85643

Browse files
committed
update daily weather
1 parent cd039f7 commit 1e85643

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed
Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
11
document.addEventListener("DOMContentLoaded", () => {
2-
function render() {
3-
// Set the date
4-
document.getElementById("date").innerText = new Date().toDateString();
5-
let lon;
6-
let lat;
7-
// Fetch the user's location
8-
fetch("http://ip-api.com/json/")
9-
.then((response) => response.json())
10-
.then((data) => {
11-
console.log("Location data:", data);
12-
lon = data.lon;
13-
lat = data.lat;
14-
console.log(lon, lat);
2+
async function render() {
3+
try {
4+
// Set the current date
5+
document.getElementById("date").innerText = new Date().toDateString();
6+
7+
8+
const locationData = await window.electronAPI.getLocation();
9+
console.log("Location data:", locationData);
1510

16-
let apiBaseURL = "https://api.open-meteo.com/v1/forecast?";
17-
// Construct the API URL with latitude and longitude
18-
let apiURL =
19-
apiBaseURL +
20-
`latitude=${lat}&longitude=${lon}&current=temperature_2m,is_day,weather_code&daily=weather_code,temperature_2m_max&timeformat=unixtime&timezone=auto`;
21-
const country = document.getElementById("country");
22-
country.innerText = data.country;
23-
const regionName = document.getElementById("regionName");
24-
regionName.innerText = data.regionName;
25-
return fetch(apiURL);
26-
})
27-
.then((response) => response.json())
28-
.then((data) => {
29-
console.log("Weather data:", data);
30-
var current = data.current;
31-
const temp = document.getElementById("temp");
32-
// Display the current temperature
33-
temp.innerHTML = current.temperature_2m + "°C";
34-
})
35-
.catch((error) => {
36-
console.log("An error occurred:", error);
37-
});
11+
const { lon, lat, country, regionName } = locationData;
12+
13+
// Update location information in the DOM
14+
document.getElementById("country").innerText = country;
15+
document.getElementById("regionName").innerText = regionName;
16+
17+
// Construct the API URL with latitude and longitude
18+
const apiBaseURL = "https://api.open-meteo.com/v1/forecast";
19+
const apiURL = `${apiBaseURL}?latitude=${lat}&longitude=${lon}&current=temperature_2m,is_day,weather_code&daily=weather_code,temperature_2m_max&timeformat=unixtime&timezone=auto`;
20+
21+
// Fetch weather data
22+
const weatherResponse = await fetch(apiURL);
23+
const weatherData = await weatherResponse.json();
24+
const currentTime = new Date().toLocaleString();
25+
console.log(`${currentTime} Weather data:`, weatherData);
26+
27+
// Update weather information in the DOM
28+
const current = weatherData.current;
29+
document.getElementById("temp").innerText = `${current.temperature_2m}°C`;
30+
} catch (error) {
31+
console.error("An error occurred:", error);
32+
}
3833
}
39-
// Run the render function on load and every 5 minutes
40-
setInterval(render, 300000), render();
34+
35+
// Initial render and subsequent updates every 5 minutes
36+
render();
37+
setInterval(render, 300000);
4138
});

0 commit comments

Comments
 (0)