-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
75 lines (67 loc) · 2.44 KB
/
weather.js
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
const inputField = document.querySelector('#input');
const submit = document.querySelector('#submit');
const responseField = document.querySelector('#responseField');
const image = document.querySelector('.image');
const url = 'https://cors-anywhere.herokuapp.com/http://api.openweathermap.org/data/2.5/weather'
const apiKey = '&appid=321f7bacdcf9dcb2c33b175133b1cac0';
const query = "?q=";
async function getWeather() {
try {
const city = inputField.value;
const endpoint = '' + url + query + city + apiKey;
const response = await fetch(endpoint, {mode: 'cors'});
const jsonResponse = await response.json();
if (response.ok) {
renderResponse(jsonResponse);
} else {
renderError(jsonResponse);
}
} catch (err) {
responseField.innterHtml = `<div>${err}</div>`;
}
}
const displayWeather = (event) => {
event.preventDefault();
while(responseField.firstChild){
responseField.removeChild(responseField.firstChild);
}
getWeather();
}
function renderResponse(response) {
try {
let offset = response.timezone;
let time = getTime(offset);
let timeHours = time.getHours();
let sunrise = getSunTime(response.sys.sunrise + offset);
let sunset = getSunTime(response.sys.sunset + offset);
if (timeHours >= sunrise.getHours() && timeHours <= 17) {
image.style.backgroundImage = "url('images/day.jpg')";
} else if (timeHours > 17 && timeHours <= sunset.getHours()){
image.style.backgroundImage = "url('images/evening.jpg')";
} else {
image.style.backgroundImage = "url('images/night.jpg')";
}
responseField.innerHTML = `<div>Temperature in ${response.name} is ${convertTemp(response.main.temp)} \xB0F. <br>
The time is: ${time.toLocaleTimeString()}<img src=https://openweathermap.org/img/wn/${response.weather[0].icon}@4x.png width=200px height=200px></div>`;
} catch (error) {
responseField.innerHTML = `<div>${error}</div>`;
}
}
function renderError(error) {
responseField.innerHTML = `<p>Error: ${error.message}</p>`;
}
function convertTemp(temp) {
return Math.round((temp - 273.15) * 9/5 + 32);
}
function getTime(utcOffset) {
let date = new Date(Date.now());
let offset = date.getTimezoneOffset() * 60000;
let utcDate = new Date(Date.now() + offset + utcOffset * 1000);
return utcDate;
}
function getSunTime(utcTime) {
let time = new Date()
let offset = time.getTimezoneOffset() * 60000;
return new Date(utcTime * 1000 + offset);
}
submit.addEventListener('click', displayWeather);