-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
190 lines (149 loc) · 4.33 KB
/
main.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
// IN THIS PLACE YOU MANUALLY SWITCH BUILD PLATFORM OF FILE
const _$ANDROID_BUILD = false;
////////////////////////////////////////////////////////
let back;
let __EXPRESS_PORT;
if (_$ANDROID_BUILD){
back = require('androidjs').back;
__EXPRESS_PORT = 4000;
} else {
__EXPRESS_PORT = 3000;
}
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch'); // This is required for lower version of Node, which is in AndroidJS
const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors());
let bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({
extended: true
})) // for parsing application/x-www-form-urlencoded
app.listen(__EXPRESS_PORT, function () {
console.log('Listening on', JSON.stringify(this.address(), null, 2));
});
app.set('views', __dirname + '/views');
app.use("/lib", express.static(__dirname + "/lib"));
app.use("/assets", express.static(__dirname + "/assets"));
// root
app.get("/", (req, res) => {
res.sendFile(__dirname + "/views/index.html");
});
/// SECTION - METEO API
/////////////////////////////////
app.get("/getxy", (req, res) => {
coordsToXY(req.query.lat, req.query.lon, "2024052218", function (result) {
res.send(result);
console.log(result);
});
});
function coordsToXY(N, E, date = "2024052218", cb_res) {
var url = `https://old.meteo.pl/um/php/mgram_search.php?NALL=${N}&EALL=${E}&lang=pl`;
fetch(url, {
method: 'HEAD',
redirect: 'manual'
})
.then(response => {
if (response.status >= 300 && response.status < 400) {
let redirectURL = response.headers.get('location');
let xyString = redirectURL.match(/&row=\d{2,3}&col=\d{2,3}/);
console.log(redirectURL);
cb_res(xyString[0]);
} else {
console.log('Response status:', response.status, url);
console.log('Response did not redirect:', response);
cb_res("err");
throw new Error('Response did not redirect, possibly wrong coordinates');
}
})
.catch(error => {
console.error('Error during fetch:', error);
});
}
/// SECTION - CONFIG FAVS FILE
/////////////////////////////////
app.get("/getcities", (req, res) => {
// in android build front js file (index.js) sends path to userData directory on android
// in web app it must be patched for security reason.
var p;
if (_$ANDROID_BUILD ) {
p = req.query.path;
} else {
p = './';
}
let file_path = path.join(p, 'cities.json');
let data = readCities(file_path);
res.json(data);
});
app.get("/addcity", (req, res) => {
let err = "";
try {
var p;
if (_$ANDROID_BUILD ) {
p = req.query.path;
} else {
p = './';
}
let file_path = path.join(p, 'cities.json');
let n = req.query.name;
let str = req.query.col_str;
addCityToFile(file_path, n, str);
} catch (error) {
err = error;
console.error(err);
}
res.json({
"status": "ok",
"err": err
});
});
app.get("/removecity", (req, res) => {
let err = "";
try {
var p;
if (_$ANDROID_BUILD ) {
p = req.query.path;
} else {
p = './';
}
let file_path = path.join(p, 'cities.json');
let n = req.query.name;
let str = req.query.col_str;
removeCityFromFile(file_path, n, str);
} catch (error) {
err = error;
console.error(err);
}
res.json({
"status": "ok",
"err": err
});
});
function readCities(file) {
if (!fs.existsSync(file)) {
return [];
}
const data = fs.readFileSync(file, 'utf8');
return JSON.parse(data);
}
function addCityToFile(file, cname, col_str) {
console.log("AddCityTofile: ", cname, col_str);
let list = readCities(file);
list.push({
"name": cname,
"str": col_str
});
fs.writeFileSync(file, JSON.stringify(list), 'utf8');
}
function removeCityFromFile(file, cname, col_str) {
let list = readCities(file);
list = list.filter(list => {
return (list.name !== cname) && (list.name !== col_str);
});
fs.writeFileSync(file, JSON.stringify(list), 'utf8');
}