Skip to content

Commit

Permalink
Some fixes and improves
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamikadze4GAME committed Nov 26, 2018
1 parent 361c35b commit add2b47
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 134 deletions.
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict';

const Tasks = require('./lib/tasks');
const Anticaptcha = require('./lib/anticaptcha');
const Proxy = require('./lib/proxy');
const Tasks = require('./lib/tasks');
const FormBuilder = require('./lib/formBuilder');
const Errors = require('./lib/errors');

module.exports = require('./lib/anticaptcha');

module.exports.Proxy = require('./lib/proxy');

module.exports.FormBuilder = require('./lib/formBuilder');
module.exports = Anticaptcha;
module.exports.Anticaptcha = Anticaptcha;
module.exports.Proxy = Proxy;
module.exports.Errors = Errors;
module.exports.FormBuilder = FormBuilder;

Object.keys(Tasks).forEach(task => {
module.exports[task] = Tasks[task];
Expand Down
183 changes: 113 additions & 70 deletions lib/anticaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const WAIT_TIME = [5000, 2000]; // 5s, 2s

class Anticaptcha {
constructor(key, proxy) {
this._key = key;
this._proxy = new Proxy(proxy);
this._rp = rp.defaults({ json: true });
this._key = key;
this._rp = rp.defaults({ json: true });
this.setProxy(proxy);
}

get key() { return this._key; }
Expand All @@ -38,6 +38,14 @@ class Anticaptcha {
data.clientKey = this.key;
}

if(typeof method !== 'string') {
return Promise.reject(new TypeError('Method must be a string'))
}

if(method[0] !== '/') {
method = '/' + method;
}

return this._rp({
method: 'POST',
url: API_URL + method,
Expand All @@ -63,108 +71,143 @@ class Anticaptcha {
}

getBalance() {
return this.method('/getBalance', {}, true)
return this.method('getBalance', {}, true)
.then(res => {
return res.balance;
});
}

getQueueStats(queueId) {
return this.method('/getQueueStats', {queueId}, false);
return this.method('getQueueStats', {queueId}, false);
}

createTask(opts = {/*task:{}, ?softId, ?languagePool, ?callbackUrl*/}) {
let data = {};

return this.method('/createTask', opts/*data*/, true);
reportIncorrectImageCaptcha(taskId) {
return this.method('reportIncorrectImageCaptcha', {taskId}, true);
}

getTaskResult(taskId) {
return this.method('/getTaskResult', {taskId, isExtended: true}, true);
test(data) {
return this.method('test', data, false);
}

reportIncorrectImageCaptcha(taskId) {
return this.method('/reportIncorrectImageCaptcha', {taskId}, true);
}
createTask(task = {}, opts = {/*?softId, ?languagePool, ?callbackUrl*/}) {
let data = { task: null };
let taskId;

test(data) {
return this.method('/test', data, false);
return Promise.resolve()
.then(_ => {
if(task instanceof Task) {
data.task = task.toJSON();
}
else {
data.task = Object.assign({}, task);
}

// Set default proxy
if(data.task.proxy === true) {
if(!this.getProxy()) {
throw new Error('No default proxy');
}
data.task.proxy = this.getProxy();
}

// Override alias proxy
if(data.task.proxy) {
Object.assign(data.task, data.task.proxy.toJSON());
delete data.task.proxy;
}

// Extra params
if(typeof opts.softId !== 'undefined') {
data.softId = opts.softId;
}
if(typeof opts.languagePool !== 'undefined') {
data.languagePool = opts.languagePool;
}
if(typeof opts.callbackUrl !== 'undefined') {
data.callbackUrl = opts.callbackUrl;
}

// Sending task
return this.method('createTask', data, true);
})
.then(res => {
if(typeof res.taskId === 'undefined') {
let err = new Error('Unexpected response');
err.response = res;
throw err;
}

return res.taskId;
})
;
}

getTaskResult2(taskId, opts = {/*waitTime, step = 0*/}) {
getTaskResult(taskId, opts = {/*extended:false, wait: false, waitTime, _step = 0*/}) {
let waitTime;

// One time request
if(!opts || !opts.wait) {
return this.method('getTaskResult', { taskId, isExtended: true }, true)
.then(res => {
if(!opts.extended) {
return res.solution;
}
return res;
})
;
}

waitTime = WAIT_TIME.slice();
if(!Array.isArray(opts.waitTime)) {
opts.waitTime = [opts.waitTime, WAIT_TIME[1]];
if(!opts.waitTime[0]) {
opts.waitTime[0] = WAIT_TIME[0];
}
opts.waitTime = [opts.waitTime];
}
if(opts.waitTime[0]) {
waitTime[0] = opts.waitTime[0];
}
if(opts.waitTime[1]) {
waitTime[1] = opts.waitTime[1];
}
waitTime = opts.waitTime || WAIT_TIME;

return this.getTaskResult(taskId)
return this.getTaskResult(taskId, { extended: true })
.then(res => {
if(!res) return res;

if(res.status === 'processing') {
return Promise.delay(waitTime[(opts.step === 0 ? 0 : 1)])
return Promise.delay(waitTime[(opts._step === 1 ? 1 : 0)])
.then(_ => {
return this.getTaskResult2(taskId, { waitTime, step:1 });
return this.getTaskResult(taskId, {
wait: true,
waitTime,
extended: !!opts.extended,
_step: 1
});
})
;
}

// status === 'ready' or else
if(!opts.extended) {
return res.solution;
}

delete res.status;
return res;
})
;
}

solve(task, opts = {/*?softId, ?languagePool, ?callbackUrl*/}) {
console.log('solving tesk', task);
let data = { task: null };
let taskId;

return Promise.resolve()
.then(_ => {
if(!(task instanceof Task)) {
throw new TypeError('Task must be instance of Task class.');
}

data.task = task.toJSON();

if(data.task.proxy === true) {
data.task.proxy = this.getProxy();
}
}

if(data.task.proxy) {
Object.assign(data.task, data.task.proxy.toJSON());
delete data.task.proxy;
}

// Extra params
if(typeof opts.softId !== 'undefined') {
data.softId = opts.softId;
}
if(typeof opts.languagePool !== 'undefined') {
data.languagePool = opts.languagePool;
}
if(typeof opts.callbackUrl !== 'undefined') {
data.callbackUrl = opts.callbackUrl;
}
console.log('data ', data);
// Sending task
return this.createTask(data)
})
.then(res => {
console.log('createTask res', res);
if(typeof res.taskId === 'undefined') {
let err = new Error('Unexpected response');
err.response = res;
throw err;
}

taskId = res.taskId;
solve(task = {}, opts = {/*?softId, ?languagePool, ?callbackUrl, extended: false*/}) {
let taskId;

return this.getTaskResult2(taskId, { waitTime: task.waitTime });
return this.createTask(task, opts)
.then(taskId => {
return this.getTaskResult(taskId, {
extended: opts.extended,
wait: true,
waitTime: task.waitTime
});
})
;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const VALID_PROXY_TYPES = ['http', 'socks4', 'socks5'];

class Proxy {
constructor(data = {}) {
constructor(data = {/* type, address, port, ?login, ?password, agent, ?cookies */}) {
if(data instanceof Proxy) {
data = data.get();
}
Expand All @@ -16,13 +16,9 @@ class Proxy {
}

if(typeof data !== 'undefined') {
this._data.type = data.type || VALID_PROXY_TYPES[0];
this._data.address = data.address || null;
this._data.port = data.port || null;
this._data.login = data.login || null;
this._data.password = data.password || null;
this._data.agent = data.agent || null;
this._data.cookies = data.cookies || null;
['type', 'address', 'port', 'login', 'password', 'agent', 'cookies'].forEach(key => {
this._data[key] = typeof data[key] !== 'undefined' ? data[key] : this._data[key];
});
}
else {
// Nothing...
Expand Down Expand Up @@ -74,6 +70,10 @@ class Proxy {
throw new Error('No proxy port');
}

if(typeof proxyData.agent === 'undefined') {
throw new Error('No agent');
}

return true;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/tasks/customCaptchaTask.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const clone = require('clone');

const Task = require('./task');
const FormBuilder = require('../formBuilder');

Expand All @@ -26,6 +28,16 @@ class CustomCaptchaTask extends Task {
forms : opts.forms
});
}

static parseResult(data = {}) {
return {
taskId : data.taskId,
imageUrl : data.imageUrl,
assignment: data.assignment,
status : data.status,
answers : clone(data.answers)
};
}
}

module.exports = CustomCaptchaTask;
2 changes: 1 addition & 1 deletion lib/tasks/funCaptchaTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FunCaptchaTask extends Task {
});
}

parseResult(data) {
static parseResult(data = {}) {
return data.token;
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/geeTestTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class GeeTestTask extends Task {
});
}

static parseResult(data = {}) {
return {
challenge: data.challenge,
validate: data.validate,
seccode: data.seccode
};
}
}

module.exports = GeeTestTask;
7 changes: 7 additions & 0 deletions lib/tasks/imageToTextTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class ImageToTextTask extends Task {
comment : data.comment
});
}

static parseResult(data = {}) {
return {
text: data.text,
url: data.url
};
}
}

module.exports = ImageToTextTask;
3 changes: 1 addition & 2 deletions lib/tasks/noCaptchaTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class NoCaptchaTask extends Task {
});
}

// TODO: gRecaptchaResponseMD5? isExtended
parseResult(data) {
static parseResult(data = {}) {
return data.gRecaptchaResponse;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/squareNetTextTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SquareNetTextTask extends Task {
});
}

parseResult(data) {
static parseResult(data = {}) {
return data.cellNumbers;
}
}
Expand Down
Loading

0 comments on commit add2b47

Please sign in to comment.