-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (105 loc) · 3.06 KB
/
app.js
File metadata and controls
133 lines (105 loc) · 3.06 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
const express = require("express");
const https = require('https');
const axios = require('axios');
const fs = require('fs');
const { HttpsProxyAgent } = require('https-proxy-agent');
// const fetch = require('node-fetch');
// import fetch from 'node-fetch'
const app = express();
const port = process.env.PORT || 3001;
// const httpsServer = https.createServer(credentials, app);
function generateRequestId() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
const httpsAgent = new https.Agent({
rejectUnauthorized: false, // add this if you want to ignore unauthorized SSL certificates
secureProtocol: 'TLSv1_2_method' // or 'TLSv1_3_method' based on your requirements
});
app.get("/geocoding", async (req, res) => {
const query = req.query.query;
const token = req.query.token;
const proxy = req.query.proxy;
if (!query || !token) {
return res.json({
type: 'error',
status: 403,
message: 'query, token and proxy are required'
})
}
try {
const axiosInstance = axios.create({
httpsAgent: proxy ? new HttpsProxyAgent(proxy) : undefined
});
console.log('proxy', proxy ? proxy : ' -x- not proxy -x-');
const url = 'https://www.google.com/search'
// const url = 'https://api.ipify.org'
const params = {
gl: "dz",
tbm: "map",
q: query,
nfpr: 1,
pb: token
}
const response = await axiosInstance.get(url, { params })
const requestId = generateRequestId()
if (response.status === 200) {
const content = response.data.replace(')]}\'\n', '');
const data = JSON.parse(content)
const item = data[0][1]
const result = []
let index = 0
// eslint-disable-next-line no-plusplus
for (let i = 0; i < item.length; i++) {
let adr = {}
try {
const element = item[i][14]
if (element[30] === 'Africa/Algiers') {
adr = {
'query': query,
'request_id': requestId,
'index': index,
"coordinate": [element[9][2], element[9][3]],
'data_0': element[0],
'data_1': element[1],
'data_2': element[2],
'data_3': element[10],
'data_4': element[11],
'data_5': element[13],
'data_6': element[14],
'data_7': element[18],
}
index += 1
}
} catch (error) { }
if (Object.keys(adr).length > 0) {
result.push(adr)
}
}
return res.json({
type: "success",
data: result
})
}
return res.json({
type: 'error',
status: response.status,
message: response.data
})
} catch (error) {
return res.json({
type: 'error',
status: 500,
message: error
})
}
});
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
server.keepAliveTimeout = 120 * 1000;
server.headersTimeout = 120 * 1000;