-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawl.c
executable file
·322 lines (287 loc) · 7.51 KB
/
crawl.c
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
// crawl.c
// By: Steve Webb ( [email protected] )
// License: LGPL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <getopt.h>
#include <curl/curl.h>
#define IOBUFF_SIZE 32*1024
#define BUFF_SIZE 30000
#define FILENAME "crawl.dat"
//#define FILENAMEOUT "output/crawl.out"
#define DEFAULT_NUM_THREADS 100
#define DELAY_SECONDS 10
#define debug(x) printf("%s:%d:%s(): %s\n", __FILE__, __LINE__, __FUNCTION__, x)
typedef enum { STOPPED, STARTED, FINISHED
} status;
typedef enum { OK, HAMMERED
} domain_status;
typedef struct domains {
char *domain;
time_t *last_crawl_time;
struct domains *next;
} domains;
typedef struct my_url {
char *url;
int retcode;
double old_size;
double new_size;
status status;
struct my_url *next;
char *filename;
int url_id;
int depth;
} my_url;
extern int optind;
extern char *optarg;
static char *progname;
int num_threads;
int max_id = 0;
FILE *status_out;
long unixtime;
char tmpstring[2000];
char useragent[2000];
my_url *root;
my_url *next_url;
domains *my_domains;
/*
char *get_domain(char *url) {
char *new_url,*new_domain;
domains *curr_domain,*prev_domain;
// returns a pointer to the domain name in a newly malloc'ed string
// parent needs to free() the pointer after using.
new_url = (char *)malloc(sizeof(url));
strcpy (new_url,url); // make a local copy to mangle
curr_domain = my_domains;
while (curr_domain) {
// scan domain list and find (and reset) one
prev_domain = curr_domain;
curr_domain = curr_domain->next;
}
if (curr_domain == NULL) {
// not found, make one and set it to now()
prev_domain->next = (domains *)malloc(sizeof(domains));
prev_domain->next->domain = new_domain;
time(prev_domain->next->last_crawl_time);
prev_domain->next->next = NULL;
}
}
*/
/*
int is_hammered(my_url *my_url) {
char *domain;
domain = get_domain(my_url->url);
}
*/
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) {
char outbuff[IOBUFF_SIZE];
setbuf(stream, outbuff);
return fwrite(ptr, size, nmemb, stream);
}
size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) {
return fread(ptr, size, nmemb, stream);
}
void read_file(char *filename) {
my_url *tmp_node;
FILE *in;
char buffer[BUFF_SIZE];
int url_id;
int depth;
char *url;
char *token;
int token_num;
in = fopen(filename,"r");
if (!in) {
printf("Error: can't open file.\n");
exit(1);
} else {
while(fgets(buffer, BUFF_SIZE, in)!=NULL) {
token_num = 0;
url_id=max_id + 1;
depth=0;
token = strtok(buffer,"|\n");
while (token) {
if (token_num == 0) {
url = (char *)malloc(sizeof(char) * (strlen(token)+1));
strcpy(url,token);
}
if (token_num == 1) {
url_id = atoi(token);
}
if (token_num == 2) {
depth = atoi(token);
}
token_num++;
token = strtok(NULL,"|\n");
}
tmp_node = (my_url *)malloc(sizeof(my_url));
tmp_node->old_size = 0;
tmp_node->url = url;
tmp_node->depth = depth;
tmp_node->url_id = url_id;
tmp_node->status = STOPPED;
tmp_node->next = root;
root = tmp_node; // inserts in reverse-order
if (url_id > max_id)
max_id = url_id;
}
}
fclose(in);
}
my_url *get_next_url(void) {
my_url *ret;
if (next_url) {
if ((next_url == root) && (next_url->next)){
next_url = next_url->next;
ret = root;
} else if (next_url->next) {
ret = next_url;
next_url = next_url->next;
} else {
ret = next_url;
if (next_url) {
next_url = next_url->next;
}
}
} else {
ret = NULL;
}
return (ret);
}
void *pull_one_url(void *thread_num) {
CURL *curl;
FILE *outfile;
FILE *headerfile;
// FILE *out;
char outfilename[100];
char headerfilename[100];
my_url *tmp_url;
double size;
long retcode;
CURLcode ret;
static int counter;
tmp_url = get_next_url();
while (tmp_url) {
counter++;
sprintf(outfilename,"output/%ld/%d.txt",unixtime,tmp_url->url_id);
outfile = fopen(outfilename, "w");
sprintf(headerfilename,"output/%ld/%d.headers.txt",unixtime,tmp_url->url_id);
headerfile = fopen(headerfilename, "w");
curl = curl_easy_init();
tmp_url->status = STARTED;
curl_easy_setopt(curl, CURLOPT_URL, tmp_url->url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func);
curl_easy_setopt(curl, CURLOPT_WRITEHEADER, headerfile);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, my_write_func);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ENCODING, "deflate");
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20);
curl_easy_setopt(curl, CURLOPT_MAXFILESIZE, 2048000);
// curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, DELAY_SECONDS);
curl_easy_setopt(curl, CURLOPT_USERAGENT, useragent);
curl_easy_perform(curl);
ret = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD,&size);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,&retcode);
tmp_url->status = FINISHED;
tmp_url->retcode = retcode;
tmp_url->filename = strdup(outfilename);
curl_easy_cleanup(curl);
fclose(outfile);
fclose(headerfile);
if ((retcode == 200) && (size > 0)) {
tmp_url->new_size = size;
fprintf (stderr,"%03d %7g [%3d] %s\n",(int)thread_num,size,(int)retcode,tmp_url->url);
} else {
tmp_url->new_size = tmp_url->old_size;
fprintf (stderr,"%03d ------- [%3d] %s\n",(int)thread_num,(int)retcode,tmp_url->url);
unlink(outfilename);
unlink(headerfilename);
}
fprintf(status_out,"%d|%d|%s\n",tmp_url->retcode,tmp_url->url_id,tmp_url->url);
// fflush(status_out);
tmp_url = get_next_url();
}
return NULL;
}
/*
void write_file (char *filename) {
FILE *out;
my_url *tmp_url;
printf("writing output file\n");
tmp_url = root;
out = fopen(filename,"w");
while (tmp_url) {
fprintf(out,"%d|%d|%s|%d\n",tmp_url->retcode,tmp_url->url_id,tmp_url->url,tmp_url->depth);
tmp_url = tmp_url->next;
}
fclose(out);
}
*/
static void usage() {
fprintf (stderr, "%s: usage: %s [-u \"useragent string\"] [-t num_threads] [-h]\n", progname, progname);
}
int main(int argc, char **argv) {
int i;
int opt;
int error;
int retcode;
num_threads = DEFAULT_NUM_THREADS;
unixtime = (long)time(NULL);
retcode = system("mkdir output");
sprintf(tmpstring,"mkdir output/%ld",unixtime);
retcode = system(tmpstring);
sprintf(tmpstring,"output/%ld/crawl.txt",unixtime);
status_out = fopen(tmpstring,"w");
strcpy(useragent,"Steve Webb's Crawler ([email protected])");
progname = strrchr(argv[0], '/');
while ((opt = getopt(argc, argv, "t:u:h")) != EOF) {
switch (opt) {
case 'u':
strcpy(useragent,optarg);
break;
case 't':
num_threads = atoi(optarg);
break;
case 'h':
usage();
exit(2);
break;
default:
usage ();
exit (2);
}
}
pthread_t tid[num_threads];
root = NULL;
my_domains = NULL;
read_file(FILENAME);
next_url = root;
for(i=0; i< num_threads; i++) {
error = pthread_create(&tid[i],
NULL, /* default attributes please */
pull_one_url,
(void *)i);
if (0 != error) {
fprintf(stderr, "Couldn't run thread number %d, errno %d\n", i, error);
}
}
fprintf(stderr,"Started %d threads.\n",num_threads);
for(i=0; i< num_threads; i++) {
error = pthread_join(tid[i], NULL);
}
// unlink(FILENAME);
// write_file(FILENAMEOUT);
fclose(status_out);
sprintf(tmpstring,"touch output/%ld/crawl.done",unixtime);
retcode = system(tmpstring);
return 0;
}