Answer the question
In order to leave comments, you need to log in
I wrote a function, but is it good?
async function img_send(img) {
return new Promise(solve => {
request.get({
url: img
}, (a, response, body) => {
data = Buffer.from(body).toString('base64');
request.post({
url: `http://anti-captcha.com/in.php`,
qs: {
key: CAPTCHA_KEY,
body: data
},
}, (a, b, result) => {
solve(result.toString())
})
})
})
}
async function captcha_handler(img) {
return new Promise(async (captched) => {
let captcha_send_counter = 0
let captcha_sended = await new Promise(solved => {
let img_sending = setInterval(async () => {
let request = await img_send(img)
console.log('В ответ на капчу:', request)
if (!request.includes('OK')) {
captcha_send_counter++;
console.log(captcha_send_counter)
if (captcha_send_counter > 30) {
console.log(colors.red('Обработка капчи отменена из-за слишком долгого ожидания'))
CAPTCHA_IN_PROGRESS = false;
captched(false)
clearInterval(img_sending)
}
} else {
solved(request)
clearInterval(img_sending)
}
}, 1000)
})
captcha_sended = captcha_sended.split('|')[1]
let waiting_limit = 0;
let answer_waiting = setInterval(() => {
waiting_limit++;
if (waiting_limit > 11) {
CAPTCHA_IN_PROGRESS = false;
captched(false)
clearInterval(answer_waiting)
}
request.post({
url: `https://anti-captcha.com/res.php?key=${CAPTCHA_KEY}&action=get&id=${captcha_sended}`,
}, (a, b, result) => {
result = result.toString()
console.log('Результат решения капчи: ', colors.blue(result))
if (result.includes('OK')) {
captched(result.split('|')[1])
clearInterval(answer_waiting)
}
})
}, 2000)
})
}
Answer the question
In order to leave comments, you need to log in
The first point is that you do not need to return a promise from an async function, the javascript will do it for you.
And also, a simple tip - take out the numbers all the numbers in the named constants, so you get rid of the magic numbers.
They can be used in place of 11, 30, and the amount of time you send in setIntervals.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question