-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (63 loc) · 2.21 KB
/
index.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
const https = require('https');
const axios = require('axios');
/**
* @name - Generate Captcha
* @param {{width: number|undefined, height: number|undefined, length: number|undefined, circles: number|undefined}} options - Options to be parsed to the API.
* @param {number} options.width - Width of image in px.
* @param {number} options.height - Height of image in px.
* @param {number} options.circles - Number of circles in image.
* @param {number} options.length - Number of characters in captcha.
* @returns {Promise<{captcha: string, uuid: string, ts: string}>}
*/
function _generate(options) {
return new Promise(function (resolve, reject) {
if (!options) options = {};
let query = [];
let j = 0;
for (const i in options) {
if (j === 0) query.push(`?${i}=${options[i]}`)
else query.push(`&${i}=${options[i]}`);
j++;
}
https.get(`https://captcha-api.akshit.me/v2/generate${query.join('')}`, function (res) {
let str = '';
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
if (res.statusCode !== 200) reject(JSON.parse(str).message);
try {
resolve(JSON.parse(str));
} catch (e) {
reject('Error trying to parse response from server.');
}
});
});
});
}
/**
*
* @name - Verify Captcha
* @param {{uuid: string|undefined, captcha: string|undefined}} options - Options to be parsed to the API.
* @param {number} options.uuid - Captcha UUID.
* @param {number} options.captcha - Solved CAPTCHA image.
* @returns {Promise<string>}
*/
function _verify(options) {
return new Promise(function (resolve, reject) {
axios.post('https://captcha-api.akshit.me/v2/verify', {
uuid: options.uuid,
captcha: options.captcha
})
.then(function (response) {
resolve(response.data.message);
})
.catch(function (error) {
reject(error.response.data.message);
});
});
}
module.exports = {
generate: _generate,
verify: _verify
}