-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proxy.js
132 lines (126 loc) · 4.17 KB
/
Proxy.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
import axios from "axios";
import { load } from "cheerio";
class Proxy {
url =
"https://www.free-proxy-list.com/?search=1&page={page}&port=&type%5B%5D=http&type%5B%5D=https&speed%5B%5D=2&speed%5B%5D=3&connect_time%5B%5D=2&connect_time%5B%5D=3&up_time=0";
fetchProxies = async (page) => {
try {
let url = this.url.replace("{page}", page);
const res = await axios.get(url);
const $ = load(res.text);
const proxyItems = [];
let trs = $(
"div.container > div.content-wrapper > div.section > div.table-responsive > table > tbody > tr"
);
trs.each(function (index, item) {
let children = $(item).find("td:not(.report-cell)");
let proxyItem = {};
const countryName = $(children[2])
.text()
.replace(/[\t\n]/g, "");
proxyItem.ip = $(children[0]).text();
proxyItem.port = $(children[1]).text();
proxyItem.country = countryName;
proxyItem.protocol = $(children[7]).text();
proxyItem.connect_time = $(children[5])
.text()
.replace(/[\t\n]/g, "");
proxyItem.up_time = $(children[6])
.text()
.replace(/[\t\n]/g, "");
proxyItem.anon = $(children[7]).text();
proxyItem.last_update = $(children[9]).text();
proxyItem.speed_download = $(children[4])
.text()
.replace(/[\t\n]/g, "");
proxyItem.url = `${proxyItem.protocol}://${proxyItem.ip}:${proxyItem.port}`;
proxyItems.push(proxyItem);
});
} catch (err) {
console.log(err);
}
return proxyItems;
};
fetchProxies = async (url, page) => {
const proxyItems = [];
try {
const res = await axios.get(url);
const $ = load(res.data);
let trs = $(
"div.container > div.content-wrapper > div.section > div.table-responsive > table > tbody > tr"
);
trs.each(function (index, item) {
let children = $(item).find("td:not(.report-cell)");
let proxyItem = {};
const countryName = $(children[2])
.text()
.replace(/[\t\n]/g, "");
proxyItem.ip = $(children[0]).text();
proxyItem.port = $(children[1]).text();
proxyItem.country = countryName;
proxyItem.protocol = $(children[7]).text();
proxyItem.connect_time = $(children[5])
.text()
.replace(/[\t\n]/g, "");
proxyItem.up_time = $(children[6])
.text()
.replace(/[\t\n]/g, "");
proxyItem.anon = $(children[8]).text();
proxyItem.last_update = $(children[9]).text();
proxyItem.speed_download = $(children[4])
.text()
.replace(/[\t\n]/g, "");
proxyItem.url = `${proxyItem.protocol}://${proxyItem.ip}:${proxyItem.port}`;
proxyItems.push(proxyItem);
});
} catch (err) {
console.log(err);
}
return proxyItems;
};
get = (site) => {
let url = site || this.url;
url = url.replace("{page}", 1);
return new Promise((resolve, reject) => {
axios
.get(url)
.then((res) => {
let $ = load(res.data);
let pagerRaw = $("ul.pagination.content-list-pager > li.pager-item");
let maxPageNum = pagerRaw.length - 4;
let responseArray = [];
for (let i = 1; i < maxPageNum + 1; i++)
responseArray.push(this.fetchProxies(url, i));
Promise.all(responseArray)
.then(function (responses) {
let results = [];
for (let i = 0; i < responses.length; i++)
results = results.concat(responses[i]);
return results;
})
.then(function (results) {
resolve(results);
})
.catch(function (err) {
reject(err);
});
})
.catch(function (err) {
reject(err);
});
});
};
getByCountry = (countryName) => {
return new Promise((resolve, reject) => {
const url = this.url + `&country=${countryName}`;
this.get(url)
.then(function (res) {
resolve(res);
})
.catch(function (err) {
reject(err);
});
});
};
}
export default Proxy;