-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
107 lines (97 loc) · 3.48 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
const Apify = require('apify');
const request = require('request-promise');
class Anticaptcha {
constructor(clientKey) {
this.clientKey = clientKey;
}
async createTask(task) {
let opt = {
method: 'POST',
uri: 'http://api.anti-captcha.com/createTask',
body: {
task,
clientKey: this.clientKey,
},
json: true
};
const response = await request(opt);
if(response.errorId > 0) throw response.errorDescription;
return response.taskId;
}
async getTaskResult(taskId) {
const opt = {
method: 'POST',
uri: 'http://api.anti-captcha.com/getTaskResult',
body: {
taskId,
clientKey: this.clientKey,
},
json: true
};
const response = await request(opt);
if(response.errorId > 0) throw response.errorDescription;
return response;
}
async waitForTaskResult(taskId, timeout) {
return new Promise((resolve, reject) => {
const startedAt = new Date();
const waitLoop = () => {
if ((new Date() - startedAt) > timeout) {
reject(new Error("Timeout before condition pass"));
}
this.getTaskResult(taskId)
.then((response) => {
if (response.errorId !== 0) {
reject(new Error(response.errorCode, response.errorDescription));
} else {
console.log(response);
if (response.status === 'ready') {
resolve(response);
} else {
setTimeout(waitLoop, 1000);
}
}
})
.catch((e) => reject(e));
};
waitLoop();
});
}
}
Apify.main(async () => {
const input = await Apify.getValue('INPUT');
if(!input.key){
console.log('ERROR: missing "key" attribute in INPUT');
return null;
}
if(!input.webUrl){
console.log('ERROR: missing "webUrl" attribute in INPUT');
return null;
}
if(!input.siteKey){
console.log('ERROR: missing "siteKey" attribute in INPUT');
return null;
}
console.log("Solving re-captcha with Anticaptcha: " + input.webUrl);
const hasProxy = input.proxyType || input.proxyAddress || input.proxyPort ||
input.proxyLogin || input.proxyPassword || input.userAgent || input.cookies;
const anticaptcha = new Anticaptcha(input.key);
const task = {
"type": hasProxy ? "NoCaptchaTask" : "NoCaptchaTaskProxyless",
"websiteURL": input.webUrl,
"websiteKey": input.siteKey
};
if(hasProxy){
task.proxyType = input.proxyType || '';
task.proxyAddress = input.proxyAddress || '';
task.proxyPort = input.proxyPort || '';
task.proxyLogin = input.proxyLogin || '';
task.proxyPassword = input.proxyPassword || '';
task.userAgent = input.userAgent || '';
task.cookies = input.cookies || '';
}
const taskId = await anticaptcha.createTask(task);
const result = await anticaptcha.waitForTaskResult(taskId, 600000);
await Apify.setValue('OUTPUT', result.solution.gRecaptchaResponse);
console.log('g-recaptcha-response: ', result.solution.gRecaptchaResponse);
});