-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (67 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
92 lines (67 loc) · 3.04 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
let currCity = "London";
let units = "metric";
const city = document.querySelector(".weather__city");
const datetime = document.querySelector(".weather__datetime");
const weather__forecast = document.querySelector('.weather__forecast');
const weather__temperature = document.querySelector(".weather__temperature");
const weather__icon = document.querySelector(".weather__icon");
const weather__minmax = document.querySelector(".weather__minmax")
const weather__realfeel = document.querySelector('.weather__realfeel');
const weather__humidity = document.querySelector('.weather__humidity');
const weather__wind = document.querySelector('.weather__wind');
const weather__pressure = document.querySelector('.weather__pressure');
document.querySelector(".weather__search").addEventListener('submit', e => {
let search = document.querySelector(".weather__searchform");
e.preventDefault();
currCity = search.value;
getWeather();
search.value = ""
})
document.querySelector(".weather_unit_celsius").addEventListener('click', () => {
if(units !== "metric"){
units = "metric"
getWeather()
}
})
document.querySelector(".weather_unit_farenheit").addEventListener('click', () => {
if(units !== "imperial"){
units = "imperial"
getWeather()
}
})
function convertTimeStamp(timestamp, timezone){
const convertTimezone = timezone / 3600;
const date = new Date(timestamp * 1000);
const options = {
weekday: "long",
day: "numeric",
month: "long",
year: "numeric",
hour: "numeric",
minute: "numeric",
timeZone: `Etc/GMT${convertTimezone >= 0 ? "-" : "+"}${Math.abs(convertTimezone)}`,
hour12: true,
}
return date.toLocaleString("en-US", options)
}
function convertCountryCode(country){
let regionNames = new Intl.DisplayNames(["en"], {type: "region"});
return regionNames.of(country)
}
function getWeather(){
const API_KEY = '41157e473043afed04716ebdf9df7868'
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${currCity}&appid=${API_KEY}&units=${units}`).then(res => res.json()).then(data => {
console.log(data)
city.innerHTML = `${data.name}, ${convertCountryCode(data.sys.country)}`
datetime.innerHTML = convertTimeStamp(data.dt, data.timezone);
weather__forecast.innerHTML = `<p>${data.weather[0].main}`
weather__temperature.innerHTML = `${data.main.temp.toFixed()}°`
weather__icon.innerHTML = ` <img src="http://openweathermap.org/img/wn/${data.weather[0].icon}@4x.png" />`
weather__minmax.innerHTML = `<p>Min: ${data.main.temp_min.toFixed()}°</p><p>Max: ${data.main.temp_max.toFixed()}°</p>`
weather__realfeel.innerHTML = `${data.main.feels_like.toFixed()}°`
weather__humidity.innerHTML = `${data.main.humidity}%`
weather__wind.innerHTML = `${data.wind.speed} ${units === "imperial" ? "mph": "m/s"}`
weather__pressure.innerHTML = `${data.main.pressure} hPa`
})
}
document.body.addEventListener('load', getWeather())