zkt25/assignment2/weather.html
2025-05-11 00:01:27 +02:00

123 lines
3.0 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Weather Info</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<style>
* {
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(to right, #e0f7fa, #80deea);
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
text-align: center;
}
h1 {
margin-bottom: 1.5rem;
font-size: 1.75rem;
font-weight: 600;
color: #00796b;
}
input {
padding: 0.5rem;
width: 65%;
border: 1px solid #ccc;
border-radius: 5px;
margin-right: 0.5rem;
font-size: 1rem;
}
button {
padding: 0.55rem 1rem;
background-color: #00796b;
color: white;
border: none;
border-radius: 5px;
font-size: 1rem;
cursor: pointer;
}
button:hover {
background-color: #00695c;
}
#weather {
margin-top: 2rem;
font-size: 1.1rem;
}
#weather p {
margin: 0.25rem 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🌦️ Check the Weather</h1>
<input type="text" id="city" placeholder="Enter city" />
<button onclick="getWeather()">Get Weather</button>
<div id="weather"></div>
</div>
<script>
async function getWeather() {
const city = document.getElementById("city").value;
const apiKey = "0ef7f5ac4207c6da232b7843eb1a663e";
try {
const res = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`
);
const data = await res.json();
if (data.cod === 200) {
document.getElementById("weather").innerHTML = `
<h2>${data.name}, ${data.sys.country}</h2>
<p><strong>${data.weather[0].description}</strong></p>
<p>🌡️ <strong>${data.main.temp}°C</strong></p>
`;
// Log to backend
await fetch("http://192.168.49.2:31000/log", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
city: data.name,
temperature: data.main.temp,
description: data.weather[0].description
})
});
} else {
document.getElementById("weather").innerHTML = `<p style="color:red;">City not found!</p>`;
}
} catch (err) {
console.error("Fetch failed:", err);
document.getElementById("weather").innerHTML = `<p style="color:red;">Error getting weather data.</p>`;
}
}
</script>
</body>
</html>