-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
156 lines (145 loc) · 4.47 KB
/
script.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
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
let lat = null;
let long = null;
function todaydate() {
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
let today = new Date();
const day = days[today.getDay()];
const dd = String(today.getDate()).padStart(2, "0");
const mm = months[today.getMonth()];
const yyyy = today.getFullYear();
return `${day} | ${dd} ${mm}, ${yyyy}`;
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(updatePosition);
}
}
async function updatePosition(pos) {
if (pos) {
lat = pos.coords.latitude;
long = pos.coords.longitude;
}
setWeatherFromMyLocation();
}
async function setWeatherFromMyLocation() {
let currentLocation = `lat=${lat}&lon=${long}`;
let val = await fetch(`https://backend-wthr.vercel.app/${currentLocation}`);
let data = await val.json();
updateweather(data);
}
async function setWeatherFromCity() {
let city = `q=${document.getElementById("search-bar").value.toLowerCase()}`;
let val = await fetch(`https://backend-wthr.vercel.app/${city}`);
let data = await val.json();
if (data?.cod == "404") {
document.getElementById("data").style.visibility = "visible";
document.getElementById("headline").innerHTML = `${data?.message}`;
} else {
updateweather(data);
}
}
function time(timedata) {
const utcTimestamp = timedata;
const utcDate = new Date(utcTimestamp * 1000);
const indianDate = utcDate.toLocaleString("en-IN");
const time = indianDate.split(",")[1].trim();
return time;
}
function setHeadline(city) {
const headlines = [
`Weather predicted in ${city}:Says Report`,
`${city} prepares for Predicted Weather Shifts`,
`${city}'s Forecast: Daily Weather Update`,
`Experts anticipate varied weather for ${city}`,
`${city}'s Weather Trends: Experts weigh in`,
];
let hline = headlines[Math.floor(Math.random() * 5)];
return hline;
}
function updateweather(data) {
document.getElementById("data").style.visibility = "visible";
document.getElementById("headline").innerHTML = setHeadline(data?.name);
document.getElementById("cityname2").innerHTML = `${data?.name}`; //city name
document.getElementById("main").innerHTML = `${data?.weather?.[0]?.main}`;
document.getElementById(
"main-img"
).src = `https://openweathermap.org/img/w/${data?.weather?.[0]?.icon}.png`;
document.getElementById("main-temp").innerHTML = `${(
data?.main?.temp - 273
).toFixed(1)}°C`;
document.getElementById(
"windspeed"
).innerHTML = `${data?.wind?.speed} m/s`;
document.getElementById(
"windspeed2"
).innerHTML = `${data?.wind?.speed} m/s`;
document.getElementById(
"humidity"
).innerHTML = `${data?.main?.humidity} %`;
document.getElementById(
"clouds"
).innerHTML = `${data?.clouds?.all} %`;
document.getElementById("mintemp").innerHTML = `${(
data?.main.temp_min - 273
).toFixed(0)}`;
document.getElementById("maxtemp").innerHTML = `${(
data?.main.temp_max - 273
).toFixed(0)}`;
document.getElementById(
"description"
).innerHTML = `${data?.weather?.[0]?.description}`;
document.getElementById(
"description2"
).innerHTML = `${data?.weather?.[0]?.description}`;
document.getElementById("feelslike").innerHTML = `${(
data?.main?.feels_like - 273
).toFixed(0)}°C`;
document.getElementById("pressure").innerHTML = `${data?.main?.pressure}`;
document.getElementById("ground").innerHTML = `${data?.main?.grnd_level}`;
document.getElementById("winddegree").innerHTML = `${data?.wind?.deg}`;
document.getElementById("sunrise").innerHTML = `${time(
data?.sys["sunrise"]
)}`;
document.getElementById("sunset").innerHTML = `${time(data?.sys["sunset"])}`;
}
document.getElementById("today").innerHTML = `${todaydate()}`;
document
.getElementById("currentlocation")
.addEventListener("click", getLocation);
document.getElementById("search-icon").addEventListener("click", function () {
if (document.getElementById("search-bar").value !== "") {
setWeatherFromCity();
}
});
document
.getElementById("search-bar")
.addEventListener("keyup", function (event) {
if (
event.key === "Enter" &&
document.getElementById("search-bar").value !== ""
) {
setWeatherFromCity();
}
});