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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
#include "Scraping.h"
#include "../Cache/Cache.h"
#include "../Proxy/Proxy.h"
#include "../Utility/XmlHelper.h"
#include "Config.h"
#include <curl/curl.h>
#include <libxml/HTMLparser.h>
#include <libxml/parser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static int response_contains(const char *response, const char *needle) {
return response && needle && strstr(response, needle) != NULL;
}
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,
const char *response) {
if (!is_startpage_job(job))
return 0;
return response_contains(response, "<title>Startpage Captcha</title>") ||
response_contains(response, "Startpage Captcha") ||
response_contains(response,
"/static-pages-assets/page-data/captcha/") ||
response_contains(response, ">Startpage Blocked</title>");
}
static int response_looks_like_results_page(const ScrapeJob *job,
const char *response) {
if (!job || !job->engine || !response)
return 0;
if (strcmp(job->engine->name, "DuckDuckGo Lite") == 0) {
return response_contains(response, "result-link") ||
response_contains(response, "result-snippet");
}
if (strcmp(job->engine->name, "Startpage") == 0) {
return response_contains(response,
"<title>Startpage Search Results</title>") ||
response_contains(response, "class=\"w-gl") ||
response_contains(response, "data-testid=\"gl-title-link\"");
}
if (strcmp(job->engine->name, "Yahoo") == 0) {
return response_contains(response, "algo-sr") ||
response_contains(response, "compTitle") ||
response_contains(response, "compText");
}
if (strcmp(job->engine->name, "Mojeek") == 0) {
return response_contains(response, "class=\"results-standard\"") ||
response_contains(response, "Mojeek Search");
}
if (strcmp(job->engine->name, "YaCy") == 0) {
return response_contains(response, "<item") ||
response_contains(response, "<rss");
}
return 0;
}
static void classify_job_response(ScrapeJob *job, const char *response,
size_t response_size) {
job->results_count = 0;
if (!response || response_size == 0) {
job->status = SCRAPE_STATUS_FETCH_ERROR;
return;
}
if (response_is_startpage_captcha(job, response)) {
job->status = SCRAPE_STATUS_BLOCKED;
return;
}
xmlDocPtr doc;
if (job->engine->is_xml) {
doc = xmlReadMemory(response, response_size, NULL, NULL,
XML_PARSE_RECOVER | XML_PARSE_NOERROR |
XML_PARSE_NOWARNING);
} else {
doc = htmlReadMemory(response, response_size, NULL, NULL,
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |
HTML_PARSE_NOWARNING);
}
if (!doc) {
job->status = SCRAPE_STATUS_FETCH_ERROR;
return;
}
job->results_count = job->engine->parser(job->engine->name, doc,
job->out_results, job->max_results);
xmlFreeDoc(doc);
if (job->results_count > 0) {
job->status = SCRAPE_STATUS_OK;
return;
}
if (job->http_status >= 400) {
job->status = SCRAPE_STATUS_FETCH_ERROR;
return;
}
if (response_looks_like_results_page(job, response)) {
job->status = SCRAPE_STATUS_PARSE_MISMATCH;
return;
}
job->status = SCRAPE_STATUS_EMPTY;
}
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);
if (!key)
return 0;
char *cached_data = NULL;
size_t cached_size = 0;
if (cache_get(key, (time_t)get_cache_ttl_search(), &cached_data,
&cached_size) == 0 &&
cached_data && cached_size > 0) {
classify_job_response(job, cached_data, cached_size);
if (job->status == SCRAPE_STATUS_BLOCKED) {
free(cached_data);
free(key);
return 0;
}
free(cached_data);
free(key);
if (job->results_count == 0)
return 0;
return 1;
}
free(key);
return 0;
}
void parse_and_cache_response(ScrapeJob *job) {
if (job->response.size == 0) {
job->results_count = 0;
job->status = SCRAPE_STATUS_FETCH_ERROR;
return;
}
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);
if (key && get_cache_ttl_search() > 0)
cache_set(key, job->response.memory, job->response.size);
free(key);
}
}
static void cleanup_job_results(ScrapeJob *job) {
if (!job || !job->out_results)
return;
xml_result_free(*job->out_results, job->results_count);
*job->out_results = NULL;
job->results_count = 0;
}
static void cleanup_job_handle(ScrapeJob *job, CURL *handle) {
struct curl_slist *headers = NULL;
if (handle)
curl_easy_getinfo(handle, CURLINFO_PRIVATE, &headers);
if (headers)
curl_slist_free_all(headers);
free(job->response.memory);
job->response.memory = NULL;
job->response.size = 0;
job->response.capacity = 0;
}
void process_response(ScrapeJob *job, CURL *handle, CURLMsg *msg) {
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &job->http_status);
if (msg->data.result == CURLE_OK)
parse_and_cache_response(job);
else {
job->results_count = 0;
job->status = SCRAPE_STATUS_FETCH_ERROR;
}
cleanup_job_handle(job, handle);
}
int setup_job(ScrapeJob *job, CURLM *multi_handle) {
if (job->handle) {
cleanup_job_handle(job, 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;
}
cleanup_job_results(job);
job->http_status = 0;
job->status = SCRAPE_STATUS_PENDING;
if (check_cache_for_job(job))
return 0;
cleanup_job_results(job);
char *encoded_query = curl_easy_escape(NULL, job->query, 0);
if (!encoded_query) {
job->status = SCRAPE_STATUS_FETCH_ERROR;
return -1;
}
char *read = encoded_query;
char *write = encoded_query;
while (*read) {
if (read[0] == '%' && read[1] == '2' && read[2] == '0') {
*write++ = '+';
read += 3;
} else {
*write++ = *read++;
}
}
*write = '\0';
char *full_url =
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);
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_easy_cleanup(job->handle);
job->handle = NULL;
job->status = SCRAPE_STATUS_FETCH_ERROR;
return -1;
}
return 0;
}
static void cleanup_unfinished_jobs(CURLM *multi_handle, ScrapeJob *jobs,
int num_jobs) {
for (int i = 0; i < num_jobs; i++) {
if (!jobs[i].handle)
continue;
curl_multi_remove_handle(multi_handle, jobs[i].handle);
cleanup_job_handle(&jobs[i], jobs[i].handle);
curl_easy_cleanup(jobs[i].handle);
jobs[i].handle = NULL;
if (jobs[i].status == SCRAPE_STATUS_PENDING)
jobs[i].status = SCRAPE_STATUS_FETCH_ERROR;
}
}
int handle_responses(CURLM *multi_handle, ScrapeJob *jobs, int num_jobs) {
CURLMsg *msg;
int msgs_left;
while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
if (msg->msg != CURLMSG_DONE)
continue;
CURL *handle = msg->easy_handle;
for (int i = 0; i < num_jobs; i++) {
if (jobs[i].handle && jobs[i].handle == handle) {
process_response(&jobs[i], handle, msg);
curl_multi_remove_handle(multi_handle, handle);
curl_easy_cleanup(handle);
jobs[i].handle = NULL;
break;
}
}
}
return 0;
}
int should_retry(ScrapeJob *jobs, int num_jobs) {
if (proxy_count <= 0)
return 0;
for (int i = 0; i < num_jobs; i++) {
if (jobs[i].status == SCRAPE_STATUS_FETCH_ERROR ||
jobs[i].status == SCRAPE_STATUS_BLOCKED)
return 1;
}
return 0;
}
int scrape_engines_parallel(ScrapeJob *jobs, int num_jobs) {
int retries = 0;
retry:;
CURLM *multi_handle = curl_multi_init();
if (!multi_handle)
return -1;
for (int i = 0; i < num_jobs; i++) {
setup_job(&jobs[i], multi_handle);
}
http_delay();
int still_running = 0;
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
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);
if (retries < max_proxy_retries && should_retry(jobs, num_jobs)) {
retries++;
goto retry;
}
return 0;
}
int scrape_engine(const SearchEngine *engine, const char *query,
SearchResult **out_results, int max_results) {
ScrapeJob job = {.engine = engine,
.query = (char *)query,
.out_results = out_results,
.max_results = max_results,
.results_count = 0,
.page = 1,
.http_status = 0,
.status = SCRAPE_STATUS_PENDING};
scrape_engines_parallel(&job, 1);
return job.results_count;
}
|