-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.js
More file actions
92 lines (92 loc) · 3.67 KB
/
processor.js
File metadata and controls
92 lines (92 loc) · 3.67 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
var vk = require('./patches/vksdk.js');
var compiler = require('./compiler.js');
var compressor = require('./compressor.js');
exports.vk = vk;
exports.create = function (settings) {
return {
client: function () {
if (undefined != settings.password) {
var client = new vk({
appId: settings.applicationIdentifier,
appSecret: settings.applicationSecret,
mode: 'oauth',
secure: true
});
client.on('serverTokenReady', function (token) {
client.setToken(token.access_token);
console.log('VKontakte.Api.serverTokenReady: ' + client.getToken());
client.ready = true;
});
client.requestServerToken(settings.userName, settings.password);
return client;
} else if (undefined != settings.accessToken) {
var client = new vk({
appId: null,
appSecret: null,
mode: 'oauth',
secure: true
});
client.setToken(settings.accessToken);
client.ready = true;
return client;
} else {
return null;
}
}(),
process: function (block, callback, tokenReplacements, captcha) {
var self = this;
var interval = setInterval(function () {
if (self.ready()) {
// Release interval as soon as possible to avoid doubled
// execution
clearInterval(interval);
// Compile the code block to VKScript
var code = compiler.compile(block);
if (undefined != tokenReplacements) {
code = compressor.compressReplacements(tokenReplacements, code);
}
// console.log(compiler.compileCString(code));
self.client.on('execute', function (result) {
if (undefined !== callback) {
callback(result);
}
});
var arguments = {
code: code
};
if (undefined != captcha) {
arguments.captcha_sid = captcha.id;
arguments.captcha_key = captcha.text;
}
self.client.request('execute', arguments, 'execute');
}
}, 100);
},
// Whether there was an error in the given response
checkErrorResponse: function (response) {
if (undefined != response.error) {
return true;
}
return false;
},
// Try to process error response depending on the error code
processErrorResponse: function (response, callback) {
var error = response.error;
var errorParameter = {};
errorParameter.type = error.error_code;
if (14 == error.error_code) {
// Server has signaled that CAPTCHA is needed
errorParameter.id = error.captcha_sid;
errorParameter.uri = error.captcha_img;
} else if (5 == response.error_code) {
// Server has signaled of an invalid session
}
callback(errorParameter);
},
ready: function () {
if (undefined !== this.client && undefined !== this.client.ready) {
return this.client.ready;
}
}
};
};