-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
209 lines (194 loc) · 10.3 KB
/
index.html
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon" href="assets/icon.png">
<link rel="apple-touch-icon" sizes="60x60" href="assets/icon.png">
<link rel="apple-touch-icon" sizes="144x144" href="assets/icon.png">
<link rel="icon" type="image/x-icon" href="/assets/icon.png">
<title>Open Weather</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<style>
:root {
--myPrimary: #8fe0ff;
--mySecondary: #75b4e3;
--myAccent: #54416d;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
body{
margin: 1px;
background: radial-gradient(ellipse at top, #8fe0ff, transparent),
radial-gradient(ellipse at bottom, #54416d, transparent);
}
</style>
</head>
<body>
<div class="d-flex justify-content-center align-items-center vh-100">
<div class="card border-dark text-bg-light col-12 col-md-6 col-lg-3 p-4" style="max-width: 30rem;">
<div class="card-body">
<div class="row align-items-center">
<div class="col-4">
<label>Current</label>
<h6 id="txtTemp"></h6>
</div>
<div id="iconWeather" class="col-4">
<i class="bi bi-cloud-drizzle" alt="rain cloud" style="font-size: 1.75rem; color: cornflowerblue;"></i>
</div>
<div class="col-4" id="icnSunPhase">
<i class="bi bi-sunset-fill" alt="Sunset" style="font-size: 1.5rem; color: var(--myAccent);"></i>
<label>6:00pm</label>
</div>
</div>
<div class="row align-items-center">
<div class="col-4">
<p class="mb-0" id="txtHigh">H: 38</p>
<p class="mt-0" id="txtLow">L: 26</p>
</div>
<div class="col-8 mb-4">
<i class="bi bi-moisture" alt="humidity gauge" style="font-size: 1.5rem; color: cornflowerblue;"></i>
<label class="mr-1" id="txtHumidity"></label>
</div>
</div>
<p id="txtCoH">It is colder than yesterday.</p>
<div class="row mb-1">
<p class="col-4">location: </p>
<select class="col-6" id="locationSelect" aria-label="location select for weather">
<option selected value="Cookeville" aria-label="Cookeville">
Cookeville
</option>
<option value="Knoxville" aria-label="Knoxville">Knoxville</option>
<option value="Nashville" aria-label="Nashville">Nashville</option>
<option value="Memphis" aria-label="Memphis">Memphis</option>
</select>
</div>
<a href="https://open-meteo.com/en/docs#latitude=52.52&longitude=13.41¤t=temperature_2m,relative_humidity_2m,precipitation,rain,showers,snowfall,weather_code&minutely_15=&hourly=&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=America%2FChicago&past_days=1&models=" class="mt-3">Data provided by open-mateo.com</a>
</div>
</div>
</div>
<script>
//variables to be put into html
var currentTemp = "";
var currentHumid = "";
var dailyHigh = "";
var dailyLow = "";
//get elements for each display
const txtTemp = document.getElementById("txtTemp");
const txtHumidity = document.getElementById("txtHumidity");
const txtHigh = document.getElementById("txtHigh");
const txtLow = document.getElementById("txtLow");
const icnWeather = document.getElementById("iconWeather");
const icnSun = document.getElementById("icnSunPhase");
const txtCoH = document.getElementById("txtCoH");
const localSelect = document.getElementById("locationSelect");
//whenever location is changed, update
localSelect.addEventListener('change', function(){
setLocation(localSelect.value);
})
//on load of page, load in cookeville **this could be reworked to user location
document.addEventListener("DOMContentLoaded", (event) => {
getData(36.15, -85.50);
});
//function for setting city based on value passed in, grabs data from get data and returns it
function setLocation(slcCity){
let arrCookeville = [36.15, -85.50];
let arrKnoxville = [35.96, -83.92];
let arrNashville = [36.16, -86.78];
let arrMemphis = [35.14, -90.04];
if(slcCity == "Cookeville"){
getData(arrCookeville[0],arrCookeville[1]);
}
else if(slcCity == "Knoxville"){
getData(arrKnoxville[0],arrKnoxville[1]);
}
else if(slcCity == "Nashville"){
getData(arrNashville[0],arrNashville[1]);
}
else if(slcCity == "Memphis"){
getData(arrMemphis[0],arrMemphis[1]);
}
else{
getData(arrCookeville[0],arrCookeville[1]);
}
}
//checks if today's high is cooler, similar, or hotter than yesterdays
function isColder(intTodaysTemp, intYestTemp){
if(intTodaysTemp < intYestTemp){
txtCoH.innerHTML = "It is cooler than yesterday";
}
else if(intTodaysTemp == intYestTemp){
txtCoH.innerHTML = "The temp is similar to yesterday";
}
else{
txtCoH.innerHTML = "It is warmer than yesterday";
}
}
//changes the icon based on rain or snow
function setWeatherIcon(intRain, intSnow){
if(intRain != 0){
icnWeather.innerHTML = `<i class="bi bi-cloud-drizzle" alt="Raining" style="font-size: 1.75rem; color: cornflowerblue;"></i>`;
}
else if (intSnow != 0) {
icnWeather.innerHTML = `<i class="bi bi-cloud-snow" alt="Snowing" style="font-size: 1.75rem; color: cornflowerblue;"></i>`;
} else {
icnWeather.innerHTML = `<i class="bi bi-brightness-high" alt="Clear" style="font-size: 1.75rem; color: black;"></i>`;
}
}
//function that determines when the next event is (sunset or sunrise) and displays it's time
function sunTime(isoTime, isoSunRise, isoSunSet){
dateTime = new Date(isoTime);
dateSunRise = new Date(isoSunRise);
dateSunSet = new Date(isoSunSet);
if((dateTime < dateSunRise)){
icnSun.innerHTML=`
<i class="bi bi-sunrise" alt="Sunrise" style="font-size: 1.5rem; color: var(--mySecondary);"></i>
` + `<label class="mr-1">` + dateSunRise.getHours() + `:00` + `</label>`;
}
else{
icnSun.innerHTML=`
<i class="bi bi-sunset" alt="Sunset" style="font-size: 1.5rem; color: var(--myAccent);"></i>
` + `<label>` + dateSunSet.getHours() + `:00`+ `</label>`;
}
}
//main function to get data for the page
async function getData(lat, long) {
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${long}¤t=temperature_2m,relative_humidity_2m,precipitation,rain,showers,snowfall,weather_code&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timezone=America%2FChicago&past_days=1`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
//parse the response into a json
const json = await response.json();
currentTemp = json["current"]["temperature_2m"] + "°F";
currentHumid = json["current"]["relative_humidity_2m"] + "%";
dailyHigh = "H:" + json["daily"]["temperature_2m_max"][1] + "°F";
dailyLow = "L:" + json["daily"]["temperature_2m_min"][1] + "°F";
setWeatherIcon(json["current"]["rain"],json["current"]["snowfall"]);
sunTime(json["current"]["time"],json["daily"]["sunrise"][1],json["daily"]["sunset"][1]);
isColder(json["daily"]["temperature_2m_max"][1],json["daily"]["temperature_2m_max"][0])
txtTemp.innerHTML = currentTemp;
txtHumidity.innerHTML = currentHumid;
txtHigh.innerHTML = dailyHigh;
txtLow.innerHTML = dailyLow;
} catch (error) {
console.error(error.message);
}
}
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
</html>
<!--
I used generative ai (chatgpt) to explain to me the code snippet from the assignment, along with image generation for the favicon/home icon
I also used gemini through a google search clarifying what a web widget is.
Later, I used chatgpt to debug some errors I had with my custom css and getData function
-->