-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (193 loc) · 7.88 KB
/
index.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
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
const app = document.querySelector('.weather-app');
const temp = document.querySelector('.temp');
const dateOutput = document.querySelector('.date');
const dayOutput = document.querySelector('.day');
const conditionOutput = document.querySelector('.condition');
const nameOutput = document.querySelector('.name');
const icon = document.querySelector('.icon');
const cloudOutput = document.querySelector('.cloud');
const humidityOutput = document.querySelector('.humidity');
const windOutput = document.querySelector('.wind');
const form = document.querySelector('.locationInput');
const search = document.querySelector('.search');
const btn = document.querySelector('.submit');
const cities = document.querySelectorAll('.city');
const API = '66a4e6336d45a6f577f46bdc0f1462c2' ;
function convertion(val){
return (val - 273).toFixed(0)
}
btn.addEventListener('click', (e) =>{
// form.addEventListener('submit' ,(e) => {
app.style.opacity = "0";
e.preventDefault();
if(search.value.length == 0){
alert('Please type in a city name');
}else{
let cityInput = search.value;
fetchWeatherData();
// search.value = "";
}
});
cities.forEach((city) => {
city.addEventListener('click' ,(e) => {
e.preventDefault();
search.value = e.target.innerHTML;
fetchWeatherData();
});
});
function fetchWeatherData(){
// fetch('./h.json')
fetch('https://api.openweathermap.org/data/2.5/weather?q='+search.value+'&appid='+API)
.then(res => res.json())
// .then(data => console.log(data))
.then(data => {
var nameval = data['name'] //egypt
var descrip = data['weather']['0']['description'] //clear sky
var tempature = convertion(data['main']['temp']) //32°C
var humidity = data['main']['humidity'] //humidity%
var feelsLike = convertion(data['main']['feels_like'] ) //feels like
var wndspd = data['wind']['speed'] //wind speed km/h
var countryCode = data['sys']['country'] //US
var sunrise = data['sys']['sunrise']
var code = data['weather']['0']['id'] ;
var sunset = data['sys']['sunset']
var iconId = data['weather']['0']['icon']
console.log(iconId);
let timeOfDay = "day";
// icon.src = 'http://openweathermap.org/img/wn/'+ iconId +'.png';
function myDay() {
var a = new Date();
var weekdays = new Array(7);
weekdays[0] = "Sunday";
weekdays[1] = "Monday";
weekdays[2] = "Tuesday";
weekdays[3] = "Wednesday";
weekdays[4] = "Thursday";
weekdays[5] = "Friday";
weekdays[6] = "Saturday";
var d = weekdays[a.getDay()];
dayOutput.innerHTML = d;
}
function myDate() {
var today = new Date();
var months = new Array(12);
months[0] = "Jan";
months[1] = "Feb";
months[2] = "March";
months[3] = "April";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10] = "Nov";
months[11] = "Dec";
// var dd = String(today.getDate()).padStart(2, '0');
// var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
var mm = months[today.getMonth()];
today = mm + ' ' + yyyy;
dateOutput.innerHTML = today;
};
if (new Date().valueOf() / 1000 < sunrise) {
console.log('Day Time');
timeOfDay = "day";
} else {
console.log('Night Time');
timeOfDay = "night";
}
nameOutput.innerHTML = nameval + ", "+ countryCode;
temp.innerHTML = tempature +"°C";
conditionOutput.innerHTML = descrip;
cloudOutput.innerHTML = feelsLike +"°C";
humidityOutput.innerHTML = humidity + "%";
windOutput.innerHTML = wndspd + "km/h";
myDay();
myDate();
//thuderstorm
if(code == 200 ||code == 201 ||code == 202 ||code == 210 ||code == 211 ||code == 212 ||code == 221||code == 230 ||code == 231||code == 232){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/thunder.jpg)';
icon.src = './icons/'+ timeOfDay +'/thunder.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
// Drizzle
else if(code == 300 ||code == 301 ||code == 302||code == 310||code == 311||code == 312||code == 313||code == 314||code == 321){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/rain.jpg)';
icon.src = './icons/'+ timeOfDay +'/rain.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
// Rain
else if(code == 500 ||code == 501 ||code == 502||code == 503||code == 504||code == 511||code == 520||code == 521||code == 522 ||code == 531){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/rain.jpg)';
icon.src = './icons/'+ timeOfDay +'/rain.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
// Snow
else if(code == 600 ||code == 601 ||code == 602||code == 611||code == 612||code == 613||code == 615||code == 616||code == 620 ||code == 621|| code ==622){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/snow.jpg)';
icon.src = './icons/'+ timeOfDay +'/snow.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
// Mist
else if(code == 701 ||code == 711 ||code == 721||code == 731||code == 741||code == 751||code == 761||code == 762||code == 771 ||code == 781){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/fog.jpg)';
icon.src = './icons/'+ timeOfDay +'/fog.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
// Clouds
else if(code == 801 ||code == 802 ||code == 804||code == 803){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/cloudy.jpg)';
icon.src = './icons/'+ timeOfDay +'/cloudy.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
//Clear
else if(code == 800 ){
app.style.backgroundImage = 'url(./img/'+ timeOfDay +'/clear.jpg)';
icon.src = './icons/'+ timeOfDay +'/clear.png';
if(timeOfDay == "night"){
btn.style.background = "#71a9d6";
}else{
btn.style.background = "#ff8b48";
}
}
//update ui
nameOutput.innerHTML = nameval + ", "+ countryCode;
temp.innerHTML = tempature +"°C";
conditionOutput.innerHTML = descrip;
cloudOutput.innerHTML = feelsLike +"°C";
humidityOutput.innerHTML = humidity + "%";
windOutput.innerHTML = wndspd + "km/h";
myDay();
myDate();
})
.catch(err => alert('Opps.., city not found'))
app.style.opacity ="1";
}
//play audio
// var sample = document.getElementById("foobar");
// sample.play();