-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
236 lines (172 loc) · 6 KB
/
Copy pathserver.js
File metadata and controls
236 lines (172 loc) · 6 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//all ejs variables
// ejs_temp
// ejs_city
// ejs_time_date
// ejs_cloud_percent
// ejs_feels_like
// ejs_pressure
// ejs_visibility
// ejs_uv
// ejs_humidity
// ejs_dew
// ejs_wind_speed
// ejs_wind_degree
// ejs_wind_gust
//all variables
let city_name;
let country_code;
let country;
let latitude=1;
let longitude=1;
//api data
let api_temp;
let api_city;
let api_time_date;
let api_cloud_percent;
let api_feels_like;
let api_pressure;
let api_visibility;
let api_uv;
let api_humidity;
let api_dew;
let api_wind_speed;
let api_wind_degree;
let api_wind_gust;
let forecast_date = [];
let forecast_temp = [];
let forecast_status = [];
let app_id = "a75fd0ffb10a00e19a40a008a31f4398";
//server start----------------->
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
geocode("kota", get_data);
app.get('/', (req, res)=>{
res.render("home", {
ejs_temp: api_temp,
ejs_city: api_city,
ejs_time_date: api_time_date,
ejs_cloud_percent: api_cloud_percent,
ejs_feels_like: api_feels_like,
ejs_pressure: api_pressure,
ejs_visibility: api_visibility,
ejs_uv: api_uv,
ejs_humidity: api_humidity,
ejs_dew: api_dew,
ejs_wind_speed: api_wind_speed,
ejs_wind_degree: api_wind_degree,
ejs_wind_gust: api_wind_gust,
forecast_time: forecast_date,
forecast_values: forecast_temp,
forecast_type: forecast_status
});
})
app.post('/', (req, res)=>{
console.log("got the details");
let city = req.body.city_search;
geocode(city, get_data);
setTimeout(() => {
res.redirect("/");
}, 1200);
})
//Function to geocode
function geocode(city, callback){
url_geocoding = "https://api.openweathermap.org/geo/1.0/direct?q=" + city + "&appid=" + app_id + "&exclude=local_names"
https.get(url_geocoding, (response)=>{
response.on("data", (data)=>{
var values = JSON.parse(data);
if(values.cod){
console.log("found error");
}
else{
city_name = values[0].name;
country_code = values[0].country;
country = values[0].country;
latitude = values[0].lat;
longitude = values[0].lon;
}
callback();
})
})
}
//function to get all useful data
function get_data(){
url_data = "https://api.openweathermap.org/data/2.5/onecall?lat=" +latitude+ "&lon=" +longitude+ "&appid=" + app_id + "&units=metric&exclude=hourly,minutely";
https.get(url_data, (response)=>{
response.on("data", (data)=>{
let info = JSON.parse(data);
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const d = new Date();
const nDate = new Date().toLocaleTimeString('en-US', {
timeZone: 'Asia/Calcutta', hours:'long'
});
api_temp = Math.round(info.current.temp);
api_city = city_name + "," +country_code;
api_time_date = nDate.slice(0, -6) +" " + nDate.slice(-2) + " - "+ d.getDate()+ "th" + " " + month[d.getMonth()] + " " + d.getFullYear();
api_cloud_percent = info.current.clouds + "% cloudiness";
api_feels_like = "Feels like " + info.current.feels_like + "°C. " + info.current.weather[0].description +".";
api_pressure = info.current.pressure;
api_visibility = info.current.visibility/1000;
api_uv = info.current.uvi;
api_humidity = info.current.humidity;
api_dew = info.current.dew_point;
api_wind_speed = info.current.wind_speed;
api_wind_degree = info.current.wind_deg;
api_wind_gust = info.current.wind_gust + " m/s";
for(var i=1; i<=8; i++){
let week = (d.getDay() + i)%7;
let m = d.getMonth();
let t = d.getDate();
if(m === 0){
t = (t+i)%31;
if(t === 0){
t=31;
}
}
else if(m===1){
t = (t+i)%28
}
else if(m%2 === 0){
t = (t+i)%30;
if(t === 0){
t=30;
}
}
else{
t = (t+i)%31;
if(t === 0){
t=31;
}
}
if(t < d.getDate()){
m++;
}
let day = days[week] + ", " + month[m] +" "+ t;
let temp_day = Math.round(info.daily[i-1].temp.day);
let temp_night = Math.round(info.daily[i-1].temp.night);
let temp = temp_day + " / " + temp_night+"°C";
let stats = info.daily[i-1].weather[0].description;
forecast_date.push(day);
forecast_temp.push(temp);
forecast_status.push(stats);
}
})
})
}
// switch for remote----
// process.env.PORT
// 3000
app.listen(process.env.PORT, ()=>{
console.log("listening to port 3000");
})
// API
// https://api.openweathermap.org/data/2.5/onecall?lat=33.44&lon=-94.04&appid=a75fd0ffb10a00e19a40a008a31f4398&units=metric&exclude=hourly,minutely
// Geo coding
// https://api.openweathermap.org/geo/1.0/direct?q=London&appid=a75fd0ffb10a00e19a40a008a31f4398&exclude=local_names
//maps
// https://tile.openweathermap.org/map/wind_new/1/33.44/-94.04.png?appid=a75fd0ffb10a00e19a40a008a31f4398