Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Commit 46ada9a

Browse files
authored
Merge pull request #3 from tbhaxor/dev-filtering
Filtering process added
2 parents 697e396 + a8b9e51 commit 46ada9a

File tree

5 files changed

+148
-35
lines changed

5 files changed

+148
-35
lines changed

README.MD

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import proxify from "node-proxifyjs";
2222

2323
(async () => {
24-
let data = await proxify(); // this will return
24+
let data = await proxify(); // this will return all 300 proxies
2525
console.log(data);
2626
})();
2727
```
@@ -34,38 +34,100 @@
3434

3535
## API
3636

37-
- **fetching `n` proxies**
37+
### Filtering Parameters
3838

39-
pass an object property `count` to the function
39+
- **Fetching `n` proxies**
40+
41+
pass an object property `count` to the function, only _number_
4042

4143
```ts
42-
const proxify = require("node-proxifyjs");
44+
import proxify from "node-proxifyjs";
4345

4446
(async () => {
4547
let data = await proxify({ count: 20 }); // this will return first 20 proxies
4648
console.log(data);
4749
})();
4850
```
4951

50-
- **returns**
52+
* **Fetching `google` proxies**
53+
54+
pass an object property `google` to the function, only _boolean_
55+
56+
```ts
57+
import proxify from "node-proxifyjs";
58+
59+
(async () => {
60+
let data = await proxify({ google: true }); // pass google: false if you dont want google proxies
61+
console.log(data);
62+
})();
63+
```
64+
65+
- **Fetching `https` proxies**
66+
67+
pass an object property `https` to the function, only _boolean_
5168

5269
```ts
53-
interface ICountry {
54-
code: string; // country code
55-
name: string; // country name
56-
}
57-
58-
interface IResult {
59-
host: string; // the ip
60-
port: number; // port numbeer
61-
country: ICountry; // country from above interface
62-
type: string; // type of proxy (elite, anonymous, transparent)
63-
google: boolean; // is google
64-
https: boolean; // is https ssl signed
65-
lastChecked: string; // last checked for working
66-
}
70+
import proxify from "node-proxifyjs";
71+
72+
(async () => {
73+
let data = await proxify({ https: true }); // pass https: false if you dont want https proxies
74+
console.log(data);
75+
})();
6776
```
6877

78+
- **Fetching `country` specific proxies**
79+
80+
pass an object property `country` to the function, only `{code?: string, name?: Regrex String}`
81+
82+
**Note:** Either `code` or `name` will work, both of them at same time will not work
83+
84+
```ts
85+
import proxify from "node-proxifyjs";
86+
87+
(async () => {
88+
let data = await proxify({ country: { code: "US" } }); // pass the name property instead of code if you want to perform regexp search
89+
console.log(data);
90+
})();
91+
```
92+
93+
- **Fetching proxies by `type`**
94+
95+
pass an object property `type` to the function, only _string_
96+
97+
```ts
98+
import proxify from "node-proxifyjs";
99+
100+
(async () => {
101+
let data = await proxify({ type: "elite proxy" }); // type can be either 'transparent', 'anonymous' or 'elite proxy' only
102+
console.log(data);
103+
})();
104+
```
105+
106+
**Note:** None, one, some or all filtering predicates can be used at once
107+
108+
```ts
109+
proxify({ count: 30, country: { code: "IN" }, type: "elite proxy" });
110+
```
111+
112+
### Returns
113+
114+
```ts
115+
interface ICountry {
116+
code: string; // country code
117+
name: string; // country name
118+
}
119+
120+
interface IResult {
121+
host: string; // the ip
122+
port: number; // port numbeer
123+
country: ICountry; // country from above interface
124+
type: string; // type of proxy (elite, anonymous, transparent)
125+
google: boolean; // is google
126+
https: boolean; // is https ssl signed
127+
lastChecked: string; // last checked for working
128+
}
129+
```
130+
69131
## Contribution
70132

71133
### Rules
@@ -76,11 +138,6 @@
76138

77139
### Scope
78140

79-
- Adding more filter
80-
- filter by **country**
81-
- filter by **https**
82-
- filter by **google**
83-
- filter by **type**
84141
- Documentation
85142
- Bugs / Suggestions / Feature Requests
86143

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-proxifyjs",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"description": "Handy node.js package to find fresh and working proxies from https://free-proxy-list.net/",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,67 @@ export default function(filter: IFilter): Promise<IResult[]> {
5858
// getting the output
5959
let output: IResult[] = raw.map(format);
6060

61-
if (!filter) {
61+
// check if no filter passed
62+
if (!filter || Object.keys(filter).length == 0) {
63+
// send all results
6264
resolve(output);
6365
}
64-
// check if count is passed
65-
else if (filter.count) {
66-
// if requested for more than 300
67-
if (filter.count > 300)
68-
// reject the request
69-
reject(new Error("Proxies limit can't exceed 300"));
66+
console.log(1);
67+
// check if https flag is set or not
68+
if (filter.https !== undefined)
69+
// filter out by the user input
70+
output = output.filter(v => v.https == filter.https);
71+
72+
// check if google flag is set or not
73+
if (filter.google !== undefined)
74+
// filter out by the user input
75+
output = output.filter(v => v.google == filter.google);
76+
77+
// check if country flag is set or not
78+
if (filter.country !== undefined) {
79+
// check if country sub flag is set or not
80+
if (Object.keys(filter.country).length == 0)
81+
// send the error
82+
reject(new Error("Insufficient filter predicates"));
7083
else {
71-
// slice and send it
72-
resolve(output.slice(0, filter.count));
84+
// filter either by code
85+
if (filter.country.code !== undefined) {
86+
// filter out by the user input
87+
output = output.filter(
88+
v => v.country.code == filter.country.code
89+
);
90+
}
91+
// or by name with regex
92+
else if (filter.country.name !== undefined) {
93+
// make the regexp
94+
let r = new RegExp(filter.country.name, "i");
95+
// filter out by the user input
96+
output = output.filter(v => r.test(v.country.name));
97+
}
7398
}
99+
}
100+
101+
// check if valid type passed
102+
if (
103+
["anonymous", "elite proxy", "transparent"].includes(filter.type)
104+
) {
105+
// filter out by the user input
106+
output = output.filter(v => v.type == filter.type);
107+
}
108+
109+
// check if count is passed
110+
if (filter.count === undefined) {
111+
// send previous
112+
resolve(output);
113+
}
114+
// check if user wants more than 300
115+
else if (filter.count > 300) {
116+
// reject with the error
117+
reject(new Error("Count of proxies can not exceed 300"));
74118
} else {
119+
// slice the proxy
120+
output = output.slice(0, filter.count);
121+
// send it <3
75122
resolve(output);
76123
}
77124
});

src/interfaces.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ export interface IResult {
1313
lastChecked: string;
1414
}
1515

16+
export interface IFilterCountry {
17+
name?: string;
18+
code?: string;
19+
}
20+
1621
export interface IFilter {
1722
count?: number;
23+
https?: boolean;
24+
google?: boolean;
25+
country?: IFilterCountry;
26+
type?: "anonymous" | "elite proxy" | "transparent";
1827
}

0 commit comments

Comments
 (0)