-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
172 lines (159 loc) · 6.25 KB
/
index.html
File metadata and controls
172 lines (159 loc) · 6.25 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Data - Badagry</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
}
h1 {
text-align: center;
color: #333;
}
.button {
background-color: darkgreen;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
margin: 10px 0;
}
.button:hover {
background-color: green;
}
.search-container {
margin: 20px 0;
}
.search-container input {
padding: 5px;
width: 200px;
}
.search-container button {
background-color: darkblue;
color: white;
padding: 5px 10px;
border: none;
cursor: pointer;
}
.search-container button:hover {
background-color: blue;
}
.output {
white-space: pre-wrap;
background-color: #fff;
padding: 10px;
border: 1px solid #ccc;
height: 300px;
overflow-y: auto;
}
.help-text {
margin-top: 20px;
padding: 10px;
background-color: #e7f3e7;
border: 1px solid #c3e6c3;
display: none;
}
</style>
</head>
<body style="border: 1px solid black; border-radius: 4px; padding: 10px;">
<h1>Weather Data - Badagry</h1>
<button class="button" onclick="fetchWeather()">Get Weather</button>
<div class="search-container">
<label for="search-date">Search by Date (YYYY-MM-DD):</label>
<input type="text" id="search-date" placeholder="Enter date">
<button onclick="searchWeather()">Search</button>
</div>
<div class="output" id="output">Weather data will appear here...</div>
<button class="button" onclick="toggleHelp()">Get Navigation Help</button>
<div class="help-text" id="help-text">
To get started, click the "Get Weather" button to fetch weather data for the next 15 days.
Use the search bar to filter data by date (YYYY-MM-DD).
Click this button again to hide this help text.
</div>
<script>
let weatherData = "";
async function fetchWeather() {
try {
const response = await fetch('https://api.open-meteo.com/v1/forecast?latitude=6.4183&longitude=2.88132&hourly=temperature_2m,weathercode&timezone=auto&forecast_days=16&temperature_unit=fahrenheit&past_days=1');
const data = await response.json();
if (!data.hourly || !data.hourly.temperature_2m || !data.hourly.weathercode) {
document.getElementById('output').textContent = "Error: Missing 'hourly' data in the response.";
return;
}
const times = data.hourly.time;
const temperatures = data.hourly.temperature_2m;
const weatherCodes = data.hourly.weathercode;
const WEATHER_CODE_MAP = {
0: "Sunny (Clear sky)",
1: "Sunny (Mostly clear)",
2: "Partly cloudy",
3: "Cloudy sky",
45: "Foggy weather",
48: "Less foggy",
51: "Light drizzle",
53: "Moderate drizzle",
55: "Heavy drizzle",
56: "Freezing Drizzle: Light",
57: "Freezing Drizzle: Dense",
61: "Light rain",
63: "Moderate rain",
65: "Heavy rain",
66: "Freezing Rain: Light",
67: "Freezing Rain: Heavy",
71: "Light snowfall",
73: "Moderate snowfall",
75: "Heavy snowfall",
77: "Little snow",
80: "Light rain showers",
81: "Moderate rain showers",
82: "Heavy rain showers",
85: "Snow showers slight",
86: "Snow showers heavy",
95: "Mild thunderstorm",
96: "Light Thunderstorm",
99: "Heavy Thunderstorm"
};
const currentDate = new Date().toISOString().split('T')[0];
let todayData = "Today:\n";
let futureData = "\nNext 15 Days:\n";
for (let i = 0; i < times.length; i++) {
const [date, hour] = times[i].split('T');
const weather = WEATHER_CODE_MAP[weatherCodes[i]] || "Still getting data... Check this time in a few hours.";
const temp = temperatures[i] + "°F";
if (date === currentDate) {
todayData += `${date} ${hour}: ${weather}, ${temp}\n`;
} else {
futureData += `${date} ${hour}: ${weather}, ${temp}\n`;
}
}
weatherData = todayData + futureData;
document.getElementById('output').textContent = weatherData;
} catch (error) {
document.getElementById('output').textContent = `An error occurred: ${error.message}`;
}
}
function searchWeather() {
const searchDate = document.getElementById('search-date').value.trim();
if (!searchDate) {
document.getElementById('output').textContent = "Please enter a valid date (YYYY-MM-DD).";
return;
}
const filteredData = weatherData
.split('\n')
.filter(line => line.includes(searchDate))
.join('\n');
document.getElementById('output').textContent = filteredData || `No data found for the date: ${searchDate}`;
}
function toggleHelp() {
const helpText = document.getElementById('help-text');
helpText.style.display = helpText.style.display === "none" ? "block" : "none";
}
</script>
</body>
</html>
</div>