-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
212 lines (197 loc) · 7.14 KB
/
server.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
206
207
208
209
210
211
212
require("dotenv").config();
const express = require("express");
const path = require("path");
const cors = require("cors");
const axios = require("axios");
const app = express();
const moment = require("moment");
const PORT = process.env.PORT || 8080;
const openweathermap_apikey = process.env.API_KEY;
const CORSOption = {
/* Add CORS option if you wish */
};
/* Either day, afternoon (named sunset), or night */
let timeofdayint = (hr) => {
return hr >= 0 && hr <= 6
? "night"
: hr >= 7 && hr <= 12
? "day"
: hr >= 13 && hr <= 18
? "sunset"
: hr >= 19 && hr <= 23
? "night"
: null;
};
/* Get the users current time from the UTC offset from OpenWeatherAPI */
let currTimeFromUTC = (utc_seconds) => {
const nowInUTC = new Date().getTime();
let currSec = parseInt((nowInUTC / 1000).toFixed(0)) - utc_seconds;
};
/* Weather conditions to consider: https://openweathermap.org/weather-conditions */
checkCondition = (description) => {
return description.includes("sky")
? "sky"
: description.includes("cloudy") || description.includes("clouds")
? "cloudy"
: description.includes("rain")
? "rain"
: description.includes("storm")
? "storm"
: description.includes("snow")
? "snow"
: description.includes("mist")
? "mist"
: null;
};
/* Properly format the input */
UpperCaseSentence = (input) =>
input.replace(/(?:^|\s)\S/g, function (a) {
return a.toUpperCase();
});
/* Messages to the user */
methodResponse = (input, city_null_check) => {
return input == "for"
? "We had trouble finding your postal so instead, we are showing you the weather for "
: city_null_check != "" && input == "geo"
? "According to your browsers GPS coordinates, you are located in "
: city_null_check == "" && input == "geo"
? "Weather at the moment based on your browsers GPS coordinates "
: input == "ip"
? "According to your Internet Provider, it seems you are located in "
: input == "zip"
? "The Zip You Enterered Is "
: null;
};
/* Guide to understanding OpenWeatherAPI: https://openweathermap.org/current */
function prepareData(weather_response, method) {
let time = moment(moment.utc() - weather_response["timezone"]).format("H");
let infoData = {};
infoData = {
status: 1,
weather_response: {
msg_user: methodResponse(method, weather_response["name"]),
city_name: UpperCaseSentence(weather_response["name"]),
current_temp: weather_response["main"]["temp"] + " °F",
current_feels_like: weather_response["main"]["feels_like"] + " °F",
current_weather_descp: UpperCaseSentence(
weather_response["weather"][0]["description"]
),
current_min: weather_response["main"]["temp_min"] + " °F",
current_max: weather_response["main"]["temp_max"] + " °F",
current_wind_speed: weather_response["wind"]["speed"] + " m/h",
weather_icon: `https://openweathermap.org/img/w/${weather_response["weather"][0]["icon"]}.png`,
},
};
const jsonFile = require("./public/media/themes.json");
let cssAdd =
jsonFile[timeofdayint(time)][
checkCondition(weather_response["weather"][0]["description"])
];
infoData["css"] = cssAdd;
infoData["api"] = {
time: timeofdayint(time),
conditions: checkCondition(weather_response["weather"][0]["description"]),
};
return infoData;
}
app.get("/api/getWeather", cors(), async (req, res) => {
/* We got a zip code as a parameter */
if (req.query["zip"]) {
if (
req.query["countrySelect"].length !== 2 &&
!/^[a-zA-Z]*$/g.test(req.query["countrySelect"]) &&
req.query["zip"].length <= 5
) {
res.send({ status: 0, message: "Error on retrieving country code" });
} else {
try {
console.log(
`Attempting to get data from zip code: ${req.query["zip"]} in ${req.query["countrySelect"]}`
);
const getResp = await axios
.get(
`https://api.openweathermap.org/data/2.5/weather?zip=${req.query["zip"]},${req.query["countrySelect"]}&units=imperial&appid=${openweathermap_apikey}`
)
.then((resp) => resp.data);
res.send(prepareData(getResp, "zip"));
} catch (e) {
console.error(`There was an error attempting to fetch the data`, e);
const getResp = await axios
.get(
`https://api.openweathermap.org/data/2.5/weather?zip=10010,us&units=imperial&appid=${openweathermap_apikey}`
)
.then((resp) => resp.data);
res.send(prepareData(getResp, "for"));
}
}
} else if (req.query["ip"]) {
/* We got an IP as a parameter */
let methodSend = "ip";
if (
!req.query["ip"].match(
"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
)
) {
res.send({
status: 0,
message: "Invalid IP Input (Only IPv4 IP addresses are supported)",
});
} else {
axios
.get(`http://ip-api.com/json/${req.query["ip"]}`)
.then((resp) => resp.data)
.catch((error) => console.error("IP Fetch Failed: " + error))
.then((data) =>
axios.get(
`https://api.openweathermap.org/data/2.5/weather?zip=${data["zip"]},${data["countryCode"]}&units=imperial&appid=${openweathermap_apikey}`
)
)
.catch((error) => {
//If we run into trouble getting the users location, just fetch New York City as a default
methodSend = "for";
return axios.get(
`https://api.openweathermap.org/data/2.5/weather?zip=10010,us&units=imperial&appid=${openweathermap_apikey}`
);
})
.then((resp) => resp.data)
.then((data) => res.send(prepareData(data, methodSend)))
.catch((error) => console.error(error));
}
} else if (req.query["geoip"]) {
/* We got a geolocation as a parameter */
regex_lat = /^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?)$/;
regex_long = /^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$/;
let geosplit = req.query["geoip"].split(",");
if (geosplit[0].match(regex_lat) && geosplit[1].match(regex_long)) {
let geosplit = req.query["geoip"].split(",");
try {
const getResp = await axios
.get(
`https://api.openweathermap.org/data/2.5/weather?lat=${geosplit[0]}&lon=${geosplit[1]}&units=imperial&appid=${openweathermap_apikey}`
)
.then((resp) => resp.data);
res.send(prepareData(getResp, "geo"));
} catch (e) {
console.error(e);
res.send({ status: 0, message: e });
}
} else {
res.send({ status: 0, message: "Invalid Coordinates" });
}
}
});
/* Weather Icon needs direct pathing */
app.get("/favicon.ico", cors(), (req, res) => {
res.sendFile(path.resolve(__dirname, "public", "media", "weather_icon.ico"));
});
if (openweathermap_apikey !== undefined) {
/* Set a static folder to serve */
app.use(cors(), express.static(path.join(__dirname, "public")));
app.listen(PORT, () =>
console.log(`OpenWeatherPanel Server started on port: ${PORT}`)
);
} else {
console.error(
"Please enter your OpenWeatherMap API Key in the config.json file"
);
}