forked from canbax/namaz-vakti-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
144 lines (132 loc) · 3.88 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
const express = require('express');
const app = express();
const got = require('got');
const static_data = require('./static_data');
const baseUrl = 'http://namazvakitleri.diyanet.gov.tr/tr-TR/';
/** use this function like `app.use(allowOrigion4All);` for an express app
* Make API accessiable for all clients. Not for only clients from a specific domain.
*/
function allowOrigin4All(_, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
};
/** get a list of countries
* @param {} _
* @param {} res
*/
const getCountryList = (_, res) => {
res.send(static_data.COUNTRIES);
};
/** get a list of cities/states for given country.
* @param {} req
* @param {} res
*/
const getCityList = (req, res) => {
const id = req.query.country;
if (id == 2) {
res.send(static_data.TR_CITIES);
} else {
const url = baseUrl + '/home/GetRegList?ChangeType=country&CountryId=' + id;
got(url).then(response => {
res.send(JSON.parse(response.body).StateList);
}).catch(error => {
console.log(error);
res.send(error);
});
}
};
/** get a list of regions/districts for given city/state.
* @param {} req
* @param {} res
*/
const getRegionsList = (req, res) => {
const country = req.query.country;
const city = req.query.city;
const url = baseUrl + `/home/GetRegList?ChangeType=state&CountryId=${country}&StateId=${city}`;
got(url).then(response => {
res.send(JSON.parse(response.body).StateRegionList);
}).catch(error => {
console.log(error);
res.send(error);
});
};
/** get times data for a region/district, returns an string[][].
* @param {} req
* @param {} res
*/
const getTimeData4Region = (req, res) => {
const url = baseUrl + req.query.region;
got(url).then(response => {
const t = findTableWithMonthInfo(response.body);
const d = getDataFromTable(t);
res.send(d);
}).catch(error => {
console.log(error);
res.send(error);
});
};
/** returns values inside <td> as string[][]
* @param {string} str HTML table as string
*/
function getDataFromTable(str) {
const arr = [];
str = str.replace(/<\/tr>/g, '');
str = str.replace(/<\/td>/g, '');
str = str.replace(/<tbody>/g, '');
str = str.replace(/<\/tbody>/g, '');
const emptyFilterFn = (x) => { x = x.trim(); return x && x.length > 0; };
const rows = str.split('<tr>').filter(emptyFilterFn);
for (const row of rows) {
if (!row || row.length < 1) {
continue;
}
const cols = row.split('<td>');
arr.push(cols.filter(emptyFilterFn).map(x => x.trim()));
}
return arr;
}
/** find a table that contains 28-31 rows to find monthly data
* @param {} str webpage as HTML string
*/
function findTableWithMonthInfo(str) {
let idx = 0;
while (true) {
const found = findTable(str.substr(idx));
if (!found) {
return null;
}
const cnt = countRowsInTable(found.str);
// this means the table have rows for each day in a month
if (cnt < 32 && cnt > 27) {
return found.str;
}
idx = found.lastIdx;
}
}
/** count number of rows in table
* @param {} table HTML table as string
*/
function countRowsInTable(table) {
return (table.match(/<tr>/g) || []).length;
}
/** find the substring for table inside 'str'
* @param {} str webpage as HTML string
*/
function findTable(str) {
const t1 = str.indexOf('<tbody');
const t2 = str.indexOf('/tbody>');
if (t1 < 0 || t2 < 0) {
return null;
}
return { str: str.substring(t1, t2 - 1), lastIdx: t2 + 7 };
}
app.use(allowOrigin4All);
app.use(express.static('public'));
console.log('__dirname: ', __dirname);
app.get('/countries', getCountryList);
app.get('/cities', getCityList);
app.get('/regions', getRegionsList);
app.get('/data', getTimeData4Region);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log('namaz vakti API listening on 3000'));