fix: follow DuckDuckGo continuation forms for pagination
This commit is contained in:
@@ -15,6 +15,79 @@ static int response_contains(const char *response, const char *needle) {
|
||||
return response && needle && strstr(response, needle) != NULL;
|
||||
}
|
||||
|
||||
static int is_ddg_job(const ScrapeJob *job) {
|
||||
return job && job->engine && job->engine->id &&
|
||||
strcmp(job->engine->id, "ddg") == 0;
|
||||
}
|
||||
|
||||
static char *job_cache_key(const ScrapeJob *job, int page) {
|
||||
const char *scope = is_ddg_job(job) ? "ddg_pagination_v2" : job->engine->name;
|
||||
return cache_compute_key(job->query, page, scope);
|
||||
}
|
||||
|
||||
static char *ddg_request_data(ScrapeJob *job, const char *encoded_query) {
|
||||
job->current_page = 1;
|
||||
if (job->page > 1 && get_cache_ttl_search() > 0) {
|
||||
char *key = job_cache_key(job, job->page - 1);
|
||||
char *cached = NULL;
|
||||
size_t size = 0;
|
||||
char *data = NULL;
|
||||
if (key && cache_get(key, get_cache_ttl_search(), &cached, &size) == 0)
|
||||
data = build_ddg_next_page_data(cached, size);
|
||||
free(cached);
|
||||
free(key);
|
||||
if (data) {
|
||||
job->current_page = job->page;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
size_t size = strlen(encoded_query) + sizeof("q=&b=");
|
||||
char *data = malloc(size);
|
||||
if (data)
|
||||
snprintf(data, size, "q=%s&b=", encoded_query);
|
||||
return data;
|
||||
}
|
||||
|
||||
static int configure_ddg_request(ScrapeJob *job, char *url,
|
||||
const char *encoded_query) {
|
||||
char *data = ddg_request_data(job, encoded_query);
|
||||
if (!data)
|
||||
return -1;
|
||||
|
||||
char *query_start = strchr(url, '?');
|
||||
if (query_start)
|
||||
*query_start = '\0';
|
||||
|
||||
curl_easy_setopt(job->handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(job->handle, CURLOPT_USERAGENT, get_first_user_agent());
|
||||
CURLcode code = curl_easy_setopt(job->handle, CURLOPT_COPYPOSTFIELDS, data);
|
||||
free(data);
|
||||
return code == CURLE_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
static int configure_job_request(ScrapeJob *job, char *url,
|
||||
const char *encoded_query) {
|
||||
job->handle = curl_easy_init();
|
||||
if (!job->handle)
|
||||
return -1;
|
||||
|
||||
job->response.memory = (char *)malloc(INITIAL_BUFFER_SIZE);
|
||||
job->response.size = 0;
|
||||
job->response.capacity = INITIAL_BUFFER_SIZE;
|
||||
if (!job->response.memory)
|
||||
return -1;
|
||||
|
||||
struct curl_slist *headers =
|
||||
build_request_headers(job->engine->host_header, job->engine->referer);
|
||||
|
||||
configure_curl_handle(job->handle, url, &job->response, headers);
|
||||
curl_easy_setopt(job->handle, CURLOPT_PRIVATE, headers);
|
||||
if (is_ddg_job(job))
|
||||
return configure_ddg_request(job, url, encoded_query);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_startpage_job(const ScrapeJob *job) {
|
||||
return job && job->engine && strcmp(job->engine->name, "Startpage") == 0;
|
||||
}
|
||||
@@ -76,7 +149,8 @@ static void classify_job_response(ScrapeJob *job, const char *response,
|
||||
return;
|
||||
}
|
||||
|
||||
if (response_is_startpage_captcha(job, response)) {
|
||||
if (response_is_startpage_captcha(job, response) ||
|
||||
(is_ddg_job(job) && response_contains(response, "id=\"challenge-form\""))) {
|
||||
job->status = SCRAPE_STATUS_BLOCKED;
|
||||
return;
|
||||
}
|
||||
@@ -123,7 +197,7 @@ int check_cache_for_job(ScrapeJob *job) {
|
||||
if (get_cache_ttl_search() <= 0)
|
||||
return 0;
|
||||
|
||||
char *key = cache_compute_key(job->query, job->page, job->engine->name);
|
||||
char *key = job_cache_key(job, job->page);
|
||||
if (!key)
|
||||
return 0;
|
||||
|
||||
@@ -164,7 +238,7 @@ void parse_and_cache_response(ScrapeJob *job) {
|
||||
classify_job_response(job, job->response.memory, job->response.size);
|
||||
|
||||
if (job->status == SCRAPE_STATUS_OK || job->status == SCRAPE_STATUS_EMPTY) {
|
||||
char *key = cache_compute_key(job->query, job->page, job->engine->name);
|
||||
char *key = job_cache_key(job, job->current_page);
|
||||
if (key && get_cache_ttl_search() > 0)
|
||||
cache_set(key, job->response.memory, job->response.size);
|
||||
free(key);
|
||||
@@ -193,34 +267,52 @@ static void cleanup_job_handle(ScrapeJob *job, CURL *handle) {
|
||||
job->response.capacity = 0;
|
||||
}
|
||||
|
||||
void process_response(ScrapeJob *job, CURL *handle, CURLMsg *msg) {
|
||||
static int process_response(ScrapeJob *job, CURL *handle, CURLcode result) {
|
||||
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &job->http_status);
|
||||
|
||||
if (msg->data.result == CURLE_OK)
|
||||
if (result == CURLE_OK)
|
||||
parse_and_cache_response(job);
|
||||
else {
|
||||
job->results_count = 0;
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
}
|
||||
|
||||
if (is_ddg_job(job) && job->current_page < job->page &&
|
||||
(job->status == SCRAPE_STATUS_OK || job->status == SCRAPE_STATUS_EMPTY)) {
|
||||
char *data = build_ddg_next_page_data(job->response.memory,
|
||||
job->response.size);
|
||||
cleanup_job_results(job);
|
||||
if (data) {
|
||||
CURLcode code = curl_easy_setopt(handle, CURLOPT_COPYPOSTFIELDS, data);
|
||||
free(data);
|
||||
if (code == CURLE_OK) {
|
||||
job->current_page++;
|
||||
job->response.size = 0;
|
||||
job->response.memory[0] = '\0';
|
||||
job->http_status = 0;
|
||||
job->status = SCRAPE_STATUS_PENDING;
|
||||
return 1;
|
||||
}
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
} else {
|
||||
job->status = SCRAPE_STATUS_EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup_job_handle(job, handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int setup_job(ScrapeJob *job, CURLM *multi_handle) {
|
||||
if (job->handle) {
|
||||
cleanup_job_handle(job, job->handle);
|
||||
cleanup_job_handle(job, job->handle);
|
||||
if (job->handle)
|
||||
curl_easy_cleanup(job->handle);
|
||||
job->handle = NULL;
|
||||
} else if (job->response.memory) {
|
||||
free(job->response.memory);
|
||||
job->response.memory = NULL;
|
||||
job->response.size = 0;
|
||||
job->response.capacity = 0;
|
||||
}
|
||||
job->handle = NULL;
|
||||
|
||||
cleanup_job_results(job);
|
||||
job->http_status = 0;
|
||||
job->status = SCRAPE_STATUS_PENDING;
|
||||
job->current_page = job->page;
|
||||
|
||||
if (check_cache_for_job(job))
|
||||
return 0;
|
||||
@@ -249,48 +341,20 @@ int setup_job(ScrapeJob *job, CURLM *multi_handle) {
|
||||
build_search_url(job->engine->base_url, job->engine->page_param,
|
||||
job->engine->page_multiplier, job->engine->page_base,
|
||||
encoded_query, job->page);
|
||||
free(encoded_query);
|
||||
|
||||
if (!full_url) {
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
job->handle = curl_easy_init();
|
||||
if (!job->handle) {
|
||||
free(full_url);
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
job->response.memory = (char *)malloc(INITIAL_BUFFER_SIZE);
|
||||
job->response.size = 0;
|
||||
job->response.capacity = INITIAL_BUFFER_SIZE;
|
||||
if (!job->response.memory) {
|
||||
curl_easy_cleanup(job->handle);
|
||||
job->handle = NULL;
|
||||
job->response.capacity = 0;
|
||||
free(full_url);
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct curl_slist *headers =
|
||||
build_request_headers(job->engine->host_header, job->engine->referer);
|
||||
|
||||
configure_curl_handle(job->handle, full_url, &job->response, headers);
|
||||
curl_easy_setopt(job->handle, CURLOPT_PRIVATE, headers);
|
||||
int ready = full_url &&
|
||||
configure_job_request(job, full_url, encoded_query) == 0;
|
||||
|
||||
free(full_url);
|
||||
CURLMcode add_result = curl_multi_add_handle(multi_handle, job->handle);
|
||||
if (add_result != CURLM_OK) {
|
||||
cleanup_job_handle(job, job->handle);
|
||||
curl_free(encoded_query);
|
||||
if (ready && curl_multi_add_handle(multi_handle, job->handle) == CURLM_OK)
|
||||
return 0;
|
||||
|
||||
cleanup_job_handle(job, job->handle);
|
||||
if (job->handle)
|
||||
curl_easy_cleanup(job->handle);
|
||||
job->handle = NULL;
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
job->handle = NULL;
|
||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void cleanup_unfinished_jobs(CURLM *multi_handle, ScrapeJob *jobs,
|
||||
@@ -321,8 +385,14 @@ int handle_responses(CURLM *multi_handle, ScrapeJob *jobs, int num_jobs) {
|
||||
|
||||
for (int i = 0; i < num_jobs; i++) {
|
||||
if (jobs[i].handle && jobs[i].handle == handle) {
|
||||
process_response(&jobs[i], handle, msg);
|
||||
CURLcode result = msg->data.result;
|
||||
curl_multi_remove_handle(multi_handle, handle);
|
||||
if (process_response(&jobs[i], handle, result)) {
|
||||
if (curl_multi_add_handle(multi_handle, handle) == CURLM_OK)
|
||||
break;
|
||||
jobs[i].status = SCRAPE_STATUS_FETCH_ERROR;
|
||||
cleanup_job_handle(&jobs[i], handle);
|
||||
}
|
||||
curl_easy_cleanup(handle);
|
||||
jobs[i].handle = NULL;
|
||||
break;
|
||||
@@ -359,18 +429,25 @@ retry:;
|
||||
|
||||
http_delay();
|
||||
|
||||
int still_running = 0;
|
||||
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
|
||||
while (1) {
|
||||
int still_running = 0;
|
||||
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
|
||||
handle_responses(multi_handle, jobs, num_jobs);
|
||||
if (mc != CURLM_OK)
|
||||
break;
|
||||
|
||||
int active = 0;
|
||||
for (int i = 0; i < num_jobs; i++)
|
||||
active += jobs[i].handle != NULL;
|
||||
if (!active)
|
||||
break;
|
||||
|
||||
while (mc == CURLM_OK && still_running) {
|
||||
int numfds = 0;
|
||||
mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
|
||||
if (mc != CURLM_OK)
|
||||
break;
|
||||
mc = curl_multi_perform(multi_handle, &still_running);
|
||||
}
|
||||
|
||||
handle_responses(multi_handle, jobs, num_jobs);
|
||||
cleanup_unfinished_jobs(multi_handle, jobs, num_jobs);
|
||||
curl_multi_cleanup(multi_handle);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user