Skip to content

Option for alternative polling callback #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion api2captcha.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdexcept>
#include <thread>
#include <vector>
#include <functional>

namespace api2captcha
{
Expand Down Expand Up @@ -372,6 +373,7 @@ namespace api2captcha
int polling_interval_ = 10; // sec
bool last_captcha_has_callback_ = false;
http_client_i *http_ = nullptr;
std::function<void(const std::chrono::seconds)> polling_callback_ = nullptr;

void check_http_response(const std::string &r)
{
Expand Down Expand Up @@ -469,6 +471,7 @@ namespace api2captcha
void set_default_timeout(const int sec) { default_timeout_ = sec; }
void set_recaptcha_timeout(const int sec) { recaptcha_timeout_ = sec; }
void set_polling_interval(const int sec) { polling_interval_ = sec; }
void set_polling_callback(std::function<void(const std::chrono::seconds)>&& callback) { polling_callback_ = std::move(callback); }

void solve(captcha_t &captcha)
{
Expand Down Expand Up @@ -514,7 +517,15 @@ namespace api2captcha
double interval = 0;
do
{
std::this_thread::sleep_for(std::chrono::seconds(polling_interval));
const auto& polling_interval_seconds = std::chrono::seconds(polling_interval);
if (polling_callback_)
{
polling_callback_(polling_interval_seconds);
}
else
{
std::this_thread::sleep_for(polling_interval_seconds);
}

try
{
Expand Down Expand Up @@ -629,6 +640,7 @@ namespace api2captcha
void set_default_timeout(const int sec) { return i_->set_default_timeout(sec); }
void set_recaptcha_timeout(const int sec) { return i_->set_recaptcha_timeout(sec); }
void set_polling_interval(const int sec) { return i_->set_polling_interval(sec); }
void set_polling_callback(std::function<void(const std::chrono::seconds)>&& callback) { i_->set_polling_callback(std::move(callback)); }

void solve(captcha_t &captcha) { return i_->solve(captcha); }

Expand Down