fixed the search part of the search engine
Changed http 2 request to yahoo to a http 1.1 for proper support Solved anubis for startpage Ddg blocked via capcha, not gonna implement a capcha solver for ts lol
This commit is contained in:
@@ -31,7 +31,7 @@ port = 8087
|
|||||||
[engines]
|
[engines]
|
||||||
# Use * for all engines, or specify comma-separated list (e.g., ddg,yahoo)
|
# Use * for all engines, or specify comma-separated list (e.g., ddg,yahoo)
|
||||||
# Use *,-engine to exclude specific engines (e.g., *,-startpage)
|
# Use *,-engine to exclude specific engines (e.g., *,-startpage)
|
||||||
# Available engines: ddg, startpage, yahoo, mojeek, yacy
|
# Available engines: ddg, startpage, yahoo, mojeek, bing, yacy
|
||||||
engines="*"
|
engines="*"
|
||||||
# YaCy instance to use for the yacy engine (optional, default: http://127.0.0.1:8090)
|
# YaCy instance to use for the yacy engine (optional, default: http://127.0.0.1:8090)
|
||||||
# Requires "yacy" to be in the engines list above.
|
# Requires "yacy" to be in the engines list above.
|
||||||
|
|||||||
@@ -6,10 +6,12 @@
|
|||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <libxml/HTMLparser.h>
|
#include <libxml/HTMLparser.h>
|
||||||
#include <libxml/parser.h>
|
#include <libxml/parser.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
static int response_contains(const char *response, const char *needle) {
|
static int response_contains(const char *response, const char *needle) {
|
||||||
return response && needle && strstr(response, needle) != NULL;
|
return response && needle && strstr(response, needle) != NULL;
|
||||||
@@ -20,51 +22,82 @@ static int is_ddg_job(const ScrapeJob *job) {
|
|||||||
strcmp(job->engine->id, "ddg") == 0;
|
strcmp(job->engine->id, "ddg") == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int is_startpage_job(const ScrapeJob *job) {
|
||||||
|
return job && job->engine && job->engine->id &&
|
||||||
|
strcmp(job->engine->id, "startpage") == 0;
|
||||||
|
}
|
||||||
|
|
||||||
static char *job_cache_key(const ScrapeJob *job, int page) {
|
static char *job_cache_key(const ScrapeJob *job, int page) {
|
||||||
const char *scope = is_ddg_job(job) ? "ddg_pagination_v2" : job->engine->name;
|
const char *scope = is_ddg_job(job) ? "ddg_pagination_v2" : job->engine->name;
|
||||||
return cache_compute_key(job->query, page, scope);
|
return cache_compute_key(job->query, page, scope);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *ddg_request_data(ScrapeJob *job, const char *encoded_query) {
|
static char *extract_ddg_vqd(const char *html) {
|
||||||
job->current_page = 1;
|
if (!html)
|
||||||
if (job->page > 1 && get_cache_ttl_search() > 0) {
|
return NULL;
|
||||||
char *key = job_cache_key(job, job->page - 1);
|
|
||||||
char *cached = NULL;
|
const char *start = strstr(html, "vqd=\"");
|
||||||
size_t size = 0;
|
if (start) {
|
||||||
char *data = NULL;
|
start += sizeof("vqd=\"") - 1;
|
||||||
if (key && cache_get(key, get_cache_ttl_search(), &cached, &size) == 0)
|
} else {
|
||||||
data = build_ddg_next_page_data(cached, size);
|
start = strstr(html, "vqd=");
|
||||||
free(cached);
|
if (!start)
|
||||||
free(key);
|
return NULL;
|
||||||
if (data) {
|
start += sizeof("vqd=") - 1;
|
||||||
job->current_page = job->page;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size = strlen(encoded_query) + sizeof("q=&b=");
|
const char *end = start;
|
||||||
char *data = malloc(size);
|
while (*end && *end != '"' && *end != '\'' && *end != '&' && *end != ';' &&
|
||||||
if (data)
|
*end != ' ' && *end != '\n' && *end != '\r')
|
||||||
snprintf(data, size, "q=%s&b=", encoded_query);
|
end++;
|
||||||
return data;
|
|
||||||
|
if (end == start)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t length = (size_t)(end - start);
|
||||||
|
char *vqd = malloc(length + 1);
|
||||||
|
if (!vqd)
|
||||||
|
return NULL;
|
||||||
|
memcpy(vqd, start, length);
|
||||||
|
vqd[length] = '\0';
|
||||||
|
return vqd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int configure_ddg_request(ScrapeJob *job, char *url,
|
static char *fetch_ddg_vqd(const char *encoded_query) {
|
||||||
const char *encoded_query) {
|
CURL *handle = curl_easy_init();
|
||||||
char *data = ddg_request_data(job, encoded_query);
|
if (!handle)
|
||||||
if (!data)
|
return NULL;
|
||||||
return -1;
|
|
||||||
|
|
||||||
char *query_start = strchr(url, '?');
|
MemoryBuffer response = {0};
|
||||||
if (query_start)
|
size_t url_size =
|
||||||
*query_start = '\0';
|
strlen(encoded_query) + sizeof("https://duckduckgo.com/?q=&ia=web");
|
||||||
|
char *url = malloc(url_size);
|
||||||
curl_easy_setopt(job->handle, CURLOPT_URL, url);
|
if (!url) {
|
||||||
curl_easy_setopt(job->handle, CURLOPT_USERAGENT, get_first_user_agent());
|
curl_easy_cleanup(handle);
|
||||||
CURLcode code = curl_easy_setopt(job->handle, CURLOPT_COPYPOSTFIELDS, data);
|
return NULL;
|
||||||
free(data);
|
|
||||||
return code == CURLE_OK ? 0 : -1;
|
|
||||||
}
|
}
|
||||||
|
snprintf(url, url_size, "https://duckduckgo.com/?q=%s&ia=web",
|
||||||
|
encoded_query);
|
||||||
|
|
||||||
|
struct curl_slist *headers =
|
||||||
|
build_request_headers("duckduckgo.com", "https://duckduckgo.com/");
|
||||||
|
configure_curl_handle(handle, url, &response, headers);
|
||||||
|
curl_easy_setopt(handle, CURLOPT_USERAGENT, get_first_user_agent());
|
||||||
|
|
||||||
|
char *vqd = NULL;
|
||||||
|
if (curl_easy_perform(handle) == CURLE_OK)
|
||||||
|
vqd = extract_ddg_vqd(response.memory);
|
||||||
|
|
||||||
|
free(response.memory);
|
||||||
|
free(url);
|
||||||
|
curl_slist_free_all(headers);
|
||||||
|
curl_easy_cleanup(handle);
|
||||||
|
return vqd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int perform_ddg_request(ScrapeJob *job, const char *url,
|
||||||
|
const char *encoded_query);
|
||||||
|
static int perform_startpage_request(ScrapeJob *job, const char *url);
|
||||||
|
|
||||||
static int configure_job_request(ScrapeJob *job, char *url,
|
static int configure_job_request(ScrapeJob *job, char *url,
|
||||||
const char *encoded_query) {
|
const char *encoded_query) {
|
||||||
@@ -84,14 +117,12 @@ static int configure_job_request(ScrapeJob *job, char *url,
|
|||||||
configure_curl_handle(job->handle, url, &job->response, headers);
|
configure_curl_handle(job->handle, url, &job->response, headers);
|
||||||
curl_easy_setopt(job->handle, CURLOPT_PRIVATE, headers);
|
curl_easy_setopt(job->handle, CURLOPT_PRIVATE, headers);
|
||||||
if (is_ddg_job(job))
|
if (is_ddg_job(job))
|
||||||
return configure_ddg_request(job, url, encoded_query);
|
return perform_ddg_request(job, url, encoded_query);
|
||||||
|
if (is_startpage_job(job))
|
||||||
|
return perform_startpage_request(job, url);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int is_startpage_job(const ScrapeJob *job) {
|
|
||||||
return job && job->engine && strcmp(job->engine->name, "Startpage") == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int response_is_startpage_captcha(const ScrapeJob *job,
|
static int response_is_startpage_captcha(const ScrapeJob *job,
|
||||||
const char *response) {
|
const char *response) {
|
||||||
if (!is_startpage_job(job))
|
if (!is_startpage_job(job))
|
||||||
@@ -111,7 +142,9 @@ static int response_looks_like_results_page(const ScrapeJob *job,
|
|||||||
|
|
||||||
if (strcmp(job->engine->name, "DuckDuckGo Lite") == 0) {
|
if (strcmp(job->engine->name, "DuckDuckGo Lite") == 0) {
|
||||||
return response_contains(response, "result-link") ||
|
return response_contains(response, "result-link") ||
|
||||||
response_contains(response, "result-snippet");
|
response_contains(response, "result-snippet") ||
|
||||||
|
response_contains(response, "result__a") ||
|
||||||
|
response_contains(response, "result__snippet");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(job->engine->name, "Startpage") == 0) {
|
if (strcmp(job->engine->name, "Startpage") == 0) {
|
||||||
@@ -132,6 +165,10 @@ static int response_looks_like_results_page(const ScrapeJob *job,
|
|||||||
response_contains(response, "Mojeek Search");
|
response_contains(response, "Mojeek Search");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strcmp(job->engine->name, "Bing") == 0) {
|
||||||
|
return response_contains(response, "<item>");
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(job->engine->name, "YaCy") == 0) {
|
if (strcmp(job->engine->name, "YaCy") == 0) {
|
||||||
return response_contains(response, "<item") ||
|
return response_contains(response, "<item") ||
|
||||||
response_contains(response, "<rss");
|
response_contains(response, "<rss");
|
||||||
@@ -267,6 +304,299 @@ static void cleanup_job_handle(ScrapeJob *job, CURL *handle) {
|
|||||||
job->response.capacity = 0;
|
job->response.capacity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int ddg_response_is_challenge(const char *response) {
|
||||||
|
return response_contains(response, "id=\"challenge-form\"") ||
|
||||||
|
response_contains(response, "challenge-form") ||
|
||||||
|
response_contains(response, "anomaly");
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *build_ddg_endpoint_url(const char *base_url,
|
||||||
|
const char *query_part,
|
||||||
|
const char *escaped_vqd) {
|
||||||
|
size_t size =
|
||||||
|
strlen(base_url) + strlen(query_part) + strlen(escaped_vqd) + 16;
|
||||||
|
char *final_url = malloc(size);
|
||||||
|
if (!final_url)
|
||||||
|
return NULL;
|
||||||
|
snprintf(final_url, size, "%s%s&vqd=%s", base_url, query_part, escaped_vqd);
|
||||||
|
return final_url;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int perform_ddg_request(ScrapeJob *job, const char *url,
|
||||||
|
const char *encoded_query) {
|
||||||
|
const int max_attempts = 3;
|
||||||
|
int completed = 0;
|
||||||
|
|
||||||
|
const char *query_part = strchr(url, '?');
|
||||||
|
if (!query_part)
|
||||||
|
query_part = "";
|
||||||
|
|
||||||
|
for (int attempt = 0; attempt < max_attempts && !completed; attempt++) {
|
||||||
|
char *vqd = fetch_ddg_vqd(encoded_query);
|
||||||
|
if (!vqd) {
|
||||||
|
if (attempt + 1 < max_attempts)
|
||||||
|
usleep(1000000);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *escaped_vqd = curl_easy_escape(NULL, vqd, 0);
|
||||||
|
free(vqd);
|
||||||
|
if (!escaped_vqd)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (int endpoint = 0; endpoint < 2 && !completed; endpoint++) {
|
||||||
|
char *final_url;
|
||||||
|
if (endpoint == 0) {
|
||||||
|
size_t size = strlen(url) + strlen(escaped_vqd) + sizeof("&vqd=");
|
||||||
|
final_url = malloc(size);
|
||||||
|
if (final_url)
|
||||||
|
snprintf(final_url, size, "%s&vqd=%s", url, escaped_vqd);
|
||||||
|
} else {
|
||||||
|
final_url = build_ddg_endpoint_url(
|
||||||
|
"https://duckduckgo.com/html/", query_part, escaped_vqd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!final_url)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
curl_easy_setopt(job->handle, CURLOPT_URL, final_url);
|
||||||
|
curl_easy_setopt(job->handle, CURLOPT_USERAGENT, get_first_user_agent());
|
||||||
|
free(final_url);
|
||||||
|
|
||||||
|
job->response.size = 0;
|
||||||
|
if (job->response.memory)
|
||||||
|
job->response.memory[0] = '\0';
|
||||||
|
|
||||||
|
CURLcode result = curl_easy_perform(job->handle);
|
||||||
|
if (result != CURLE_OK)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
curl_easy_getinfo(job->handle, CURLINFO_RESPONSE_CODE,
|
||||||
|
&job->http_status);
|
||||||
|
if (ddg_response_is_challenge(job->response.memory))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
parse_and_cache_response(job);
|
||||||
|
completed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_free(escaped_vqd);
|
||||||
|
if (!completed && attempt + 1 < max_attempts)
|
||||||
|
usleep(1000000);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!completed)
|
||||||
|
parse_and_cache_response(job);
|
||||||
|
|
||||||
|
cleanup_job_handle(job, job->handle);
|
||||||
|
curl_easy_cleanup(job->handle);
|
||||||
|
job->handle = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *json_field_start(const char *json, const char *anchor,
|
||||||
|
const char *field) {
|
||||||
|
if (!json || !field)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
const char *scope = anchor ? strstr(json, anchor) : json;
|
||||||
|
if (!scope)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char pattern[64];
|
||||||
|
int written = snprintf(pattern, sizeof(pattern), "\"%s\":", field);
|
||||||
|
if (written < 0 || (size_t)written >= sizeof(pattern))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return strstr(scope, pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *json_string_after(const char *json, const char *anchor,
|
||||||
|
const char *field) {
|
||||||
|
const char *start = json_field_start(json, anchor, field);
|
||||||
|
if (!start)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
start += strlen(field) + 3;
|
||||||
|
while (*start == ' ' || *start == '\t')
|
||||||
|
start++;
|
||||||
|
if (*start != '"')
|
||||||
|
return NULL;
|
||||||
|
start++;
|
||||||
|
|
||||||
|
const char *end = strchr(start, '"');
|
||||||
|
if (!end)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t length = (size_t)(end - start);
|
||||||
|
char *value = malloc(length + 1);
|
||||||
|
if (!value)
|
||||||
|
return NULL;
|
||||||
|
memcpy(value, start, length);
|
||||||
|
value[length] = '\0';
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int json_int_after(const char *json, const char *anchor,
|
||||||
|
const char *field, int *out) {
|
||||||
|
const char *start = json_field_start(json, anchor, field);
|
||||||
|
if (!start)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
start += strlen(field) + 3;
|
||||||
|
while (*start == ' ' || *start == '\t')
|
||||||
|
start++;
|
||||||
|
|
||||||
|
char *end = NULL;
|
||||||
|
long value = strtol(start, &end, 10);
|
||||||
|
if (end == start)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
*out = (int)value;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int hash_meets_difficulty(const unsigned char *digest,
|
||||||
|
int difficulty) {
|
||||||
|
if (difficulty < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
int full_bytes = difficulty / 2;
|
||||||
|
if (full_bytes > SHA256_DIGEST_LENGTH)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < full_bytes; i++) {
|
||||||
|
if (digest[i] != 0)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((difficulty % 2) != 0 && full_bytes < SHA256_DIGEST_LENGTH &&
|
||||||
|
(digest[full_bytes] >> 4) != 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *solve_anubis_challenge(const char *random_data, int difficulty,
|
||||||
|
char *nonce_out, size_t nonce_size,
|
||||||
|
long *elapsed_ms) {
|
||||||
|
struct timespec started, finished;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &started);
|
||||||
|
|
||||||
|
size_t random_length = strlen(random_data);
|
||||||
|
char *input = malloc(random_length + 32);
|
||||||
|
if (!input)
|
||||||
|
return NULL;
|
||||||
|
memcpy(input, random_data, random_length);
|
||||||
|
|
||||||
|
unsigned char digest[SHA256_DIGEST_LENGTH];
|
||||||
|
for (unsigned long long nonce = 0; nonce < 50000000ULL; nonce++) {
|
||||||
|
int written = snprintf(input + random_length, 32, "%llu", nonce);
|
||||||
|
if (written < 0 || written >= 32) {
|
||||||
|
free(input);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHA256((const unsigned char *)input, random_length + (size_t)written,
|
||||||
|
digest);
|
||||||
|
if (!hash_meets_difficulty(digest, difficulty))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
char *hex = malloc(SHA256_DIGEST_LENGTH * 2 + 1);
|
||||||
|
if (!hex) {
|
||||||
|
free(input);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
||||||
|
snprintf(hex + i * 2, 3, "%02x", digest[i]);
|
||||||
|
hex[SHA256_DIGEST_LENGTH * 2] = '\0';
|
||||||
|
snprintf(nonce_out, nonce_size, "%llu", nonce);
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &finished);
|
||||||
|
*elapsed_ms = (finished.tv_sec - started.tv_sec) * 1000 +
|
||||||
|
(finished.tv_nsec - started.tv_nsec) / 1000000;
|
||||||
|
if (*elapsed_ms < 1)
|
||||||
|
*elapsed_ms = 1;
|
||||||
|
|
||||||
|
free(input);
|
||||||
|
return hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(input);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int perform_startpage_request(ScrapeJob *job, const char *url) {
|
||||||
|
CURLcode result = curl_easy_perform(job->handle);
|
||||||
|
if (result != CURLE_OK) {
|
||||||
|
job->results_count = 0;
|
||||||
|
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
||||||
|
} else {
|
||||||
|
curl_easy_getinfo(job->handle, CURLINFO_RESPONSE_CODE, &job->http_status);
|
||||||
|
|
||||||
|
if (response_contains(job->response.memory, "anubis_challenge")) {
|
||||||
|
char *algorithm =
|
||||||
|
json_string_after(job->response.memory, "\"rules\":", "algorithm");
|
||||||
|
char *random_data = json_string_after(job->response.memory,
|
||||||
|
"\"challenge\":", "randomData");
|
||||||
|
char *challenge_id =
|
||||||
|
json_string_after(job->response.memory, "\"challenge\":", "id");
|
||||||
|
int difficulty = 0;
|
||||||
|
int has_difficulty = json_int_after(job->response.memory, "\"rules\":",
|
||||||
|
"difficulty", &difficulty);
|
||||||
|
|
||||||
|
if (algorithm && strcmp(algorithm, "fast") == 0 && random_data &&
|
||||||
|
challenge_id && has_difficulty && difficulty >= 0) {
|
||||||
|
char nonce[32];
|
||||||
|
long elapsed_ms = 1;
|
||||||
|
char *response_hash = solve_anubis_challenge(
|
||||||
|
random_data, difficulty, nonce, sizeof(nonce), &elapsed_ms);
|
||||||
|
|
||||||
|
if (response_hash) {
|
||||||
|
char *escaped_redir = curl_easy_escape(NULL, url, 0);
|
||||||
|
if (escaped_redir) {
|
||||||
|
size_t pass_url_size = strlen(challenge_id) +
|
||||||
|
strlen(response_hash) + strlen(nonce) +
|
||||||
|
strlen(escaped_redir) + 160;
|
||||||
|
char *pass_url = malloc(pass_url_size);
|
||||||
|
if (pass_url) {
|
||||||
|
snprintf(
|
||||||
|
pass_url, pass_url_size,
|
||||||
|
"https://www.startpage.com/.within.website/x/cmd/anubis/api/"
|
||||||
|
"pass-challenge?id=%s&response=%s&nonce=%s&redir=%s&"
|
||||||
|
"elapsedTime=%ld",
|
||||||
|
challenge_id, response_hash, nonce, escaped_redir,
|
||||||
|
elapsed_ms);
|
||||||
|
|
||||||
|
job->response.size = 0;
|
||||||
|
if (job->response.memory)
|
||||||
|
job->response.memory[0] = '\0';
|
||||||
|
curl_easy_setopt(job->handle, CURLOPT_URL, pass_url);
|
||||||
|
if (curl_easy_perform(job->handle) == CURLE_OK)
|
||||||
|
curl_easy_getinfo(job->handle, CURLINFO_RESPONSE_CODE,
|
||||||
|
&job->http_status);
|
||||||
|
free(pass_url);
|
||||||
|
}
|
||||||
|
curl_free(escaped_redir);
|
||||||
|
}
|
||||||
|
free(response_hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(algorithm);
|
||||||
|
free(random_data);
|
||||||
|
free(challenge_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_and_cache_response(job);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup_job_handle(job, job->handle);
|
||||||
|
curl_easy_cleanup(job->handle);
|
||||||
|
job->handle = NULL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int process_response(ScrapeJob *job, CURL *handle, CURLcode result) {
|
static int process_response(ScrapeJob *job, CURL *handle, CURLcode result) {
|
||||||
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &job->http_status);
|
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &job->http_status);
|
||||||
|
|
||||||
@@ -277,28 +607,6 @@ static int process_response(ScrapeJob *job, CURL *handle, CURLcode result) {
|
|||||||
job->status = SCRAPE_STATUS_FETCH_ERROR;
|
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);
|
cleanup_job_handle(job, handle);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -341,12 +649,19 @@ int setup_job(ScrapeJob *job, CURLM *multi_handle) {
|
|||||||
build_search_url(job->engine->base_url, job->engine->page_param,
|
build_search_url(job->engine->base_url, job->engine->page_param,
|
||||||
job->engine->page_multiplier, job->engine->page_base,
|
job->engine->page_multiplier, job->engine->page_base,
|
||||||
encoded_query, job->page);
|
encoded_query, job->page);
|
||||||
int ready = full_url &&
|
int configured =
|
||||||
configure_job_request(job, full_url, encoded_query) == 0;
|
full_url ? configure_job_request(job, full_url, encoded_query) : -1;
|
||||||
|
|
||||||
free(full_url);
|
free(full_url);
|
||||||
curl_free(encoded_query);
|
curl_free(encoded_query);
|
||||||
if (ready && curl_multi_add_handle(multi_handle, job->handle) == CURLM_OK)
|
|
||||||
|
/* A return value of 1 means the request was completed synchronously (for
|
||||||
|
engines that need a challenge or token round-trip). */
|
||||||
|
if (configured == 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (configured == 0 &&
|
||||||
|
curl_multi_add_handle(multi_handle, job->handle) == CURLM_OK)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
cleanup_job_handle(job, job->handle);
|
cleanup_job_handle(job, job->handle);
|
||||||
|
|||||||
@@ -63,7 +63,9 @@ void configure_curl_handle(CURL *curl, const char *full_url,
|
|||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, get_random_user_agent());
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, get_random_user_agent());
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
|
/* Yahoo currently answers HTTP/2 requests with a 500 error. HTTP/1.1 is
|
||||||
|
accepted by all of the scraped engines, so use it everywhere. */
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
||||||
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
|
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
|
||||||
curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS);
|
curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS);
|
||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
|
|||||||
@@ -39,6 +39,70 @@ void free_xml_node_list(char *title, char *url, char *snippet) {
|
|||||||
xmlFree(snippet);
|
xmlFree(snippet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int parse_ddg_html(const char *engine_name, xmlDocPtr doc,
|
||||||
|
SearchResult **out_results, int max_results) {
|
||||||
|
(void)engine_name;
|
||||||
|
int found_count = 0;
|
||||||
|
|
||||||
|
xmlXPathContextPtr ctx = create_xpath_context(doc);
|
||||||
|
if (!ctx)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
xmlXPathObjectPtr obj = xml_xpath_eval(ctx, "//a[@class='result__a']");
|
||||||
|
if (!obj || !obj->nodesetval || obj->nodesetval->nodeNr == 0) {
|
||||||
|
free_xpath_objects(ctx, obj);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int num_links = obj->nodesetval->nodeNr;
|
||||||
|
*out_results = alloc_results_array(num_links, max_results);
|
||||||
|
if (!*out_results) {
|
||||||
|
free_xpath_objects(ctx, obj);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < num_links && found_count < max_results; i++) {
|
||||||
|
xmlNodePtr link_node = obj->nodesetval->nodeTab[i];
|
||||||
|
char *title = xml_node_content(link_node);
|
||||||
|
char *url = (char *)xmlGetProp(link_node, (xmlChar *)"href");
|
||||||
|
char *snippet_text = NULL;
|
||||||
|
|
||||||
|
xmlNodePtr result_node = link_node->parent;
|
||||||
|
while (result_node) {
|
||||||
|
if (result_node->type == XML_ELEMENT_NODE &&
|
||||||
|
xmlStrcasecmp(result_node->name, (const xmlChar *)"div") == 0) {
|
||||||
|
xmlChar *class_name =
|
||||||
|
xmlGetProp(result_node, (const xmlChar *)"class");
|
||||||
|
int is_result =
|
||||||
|
class_name && strstr((const char *)class_name, "result") != NULL;
|
||||||
|
xmlFree(class_name);
|
||||||
|
if (is_result)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result_node = result_node->parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result_node) {
|
||||||
|
ctx->node = result_node;
|
||||||
|
xmlXPathObjectPtr snippet_obj =
|
||||||
|
xml_xpath_eval(ctx, ".//a[contains(@class, 'result__snippet')]");
|
||||||
|
if (snippet_obj && snippet_obj->nodesetval &&
|
||||||
|
snippet_obj->nodesetval->nodeNr > 0)
|
||||||
|
snippet_text = xml_node_content(snippet_obj->nodesetval->nodeTab[0]);
|
||||||
|
if (snippet_obj)
|
||||||
|
xmlXPathFreeObject(snippet_obj);
|
||||||
|
ctx->node = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assign_result(&(*out_results)[found_count], url, title, snippet_text, 1);
|
||||||
|
free_xml_node_list(title, url, snippet_text);
|
||||||
|
found_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_xpath_objects(ctx, obj);
|
||||||
|
return found_count;
|
||||||
|
}
|
||||||
|
|
||||||
static int parse_ddg_lite(const char *engine_name, xmlDocPtr doc,
|
static int parse_ddg_lite(const char *engine_name, xmlDocPtr doc,
|
||||||
SearchResult **out_results, int max_results) {
|
SearchResult **out_results, int max_results) {
|
||||||
(void)engine_name;
|
(void)engine_name;
|
||||||
@@ -54,7 +118,7 @@ static int parse_ddg_lite(const char *engine_name, xmlDocPtr doc,
|
|||||||
|
|
||||||
if (!obj || !obj->nodesetval || obj->nodesetval->nodeNr == 0) {
|
if (!obj || !obj->nodesetval || obj->nodesetval->nodeNr == 0) {
|
||||||
free_xpath_objects(ctx, obj);
|
free_xpath_objects(ctx, obj);
|
||||||
return 0;
|
return parse_ddg_html(engine_name, doc, out_results, max_results);
|
||||||
}
|
}
|
||||||
|
|
||||||
int num_links = obj->nodesetval->nodeNr;
|
int num_links = obj->nodesetval->nodeNr;
|
||||||
@@ -309,7 +373,7 @@ static int parse_yahoo(const char *engine_name, xmlDocPtr doc,
|
|||||||
static int parse_mojeek(const char *engine_name, xmlDocPtr doc,
|
static int parse_mojeek(const char *engine_name, xmlDocPtr doc,
|
||||||
SearchResult **out_results, int max_results);
|
SearchResult **out_results, int max_results);
|
||||||
|
|
||||||
static int parse_yacy(const char *engine_name, xmlDocPtr doc,
|
static int parse_rss(const char *engine_name, xmlDocPtr doc,
|
||||||
SearchResult **out_results, int max_results) {
|
SearchResult **out_results, int max_results) {
|
||||||
(void)engine_name;
|
(void)engine_name;
|
||||||
int found_count = 0;
|
int found_count = 0;
|
||||||
@@ -434,6 +498,17 @@ SearchEngine ENGINE_REGISTRY[] = {
|
|||||||
.page_base = 1,
|
.page_base = 1,
|
||||||
.parser = parse_mojeek,
|
.parser = parse_mojeek,
|
||||||
.enabled = 1},
|
.enabled = 1},
|
||||||
|
{.id = "bing",
|
||||||
|
.name = "Bing",
|
||||||
|
.base_url = "https://www.bing.com/search?format=rss&q=",
|
||||||
|
.host_header = "www.bing.com",
|
||||||
|
.referer = "https://www.bing.com/",
|
||||||
|
.page_param = "first",
|
||||||
|
.page_multiplier = 10,
|
||||||
|
.page_base = 1,
|
||||||
|
.parser = parse_rss,
|
||||||
|
.enabled = 1,
|
||||||
|
.is_xml = 1},
|
||||||
{.id = "yacy",
|
{.id = "yacy",
|
||||||
.name = "YaCy",
|
.name = "YaCy",
|
||||||
.base_url = "",
|
.base_url = "",
|
||||||
@@ -442,7 +517,7 @@ SearchEngine ENGINE_REGISTRY[] = {
|
|||||||
.page_param = "startRecord",
|
.page_param = "startRecord",
|
||||||
.page_multiplier = 10,
|
.page_multiplier = 10,
|
||||||
.page_base = 0,
|
.page_base = 0,
|
||||||
.parser = parse_yacy,
|
.parser = parse_rss,
|
||||||
.enabled = 0,
|
.enabled = 0,
|
||||||
.is_xml = 1}};
|
.is_xml = 1}};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user