-
Notifications
You must be signed in to change notification settings - Fork 0
/
crowller.h
255 lines (233 loc) · 6.36 KB
/
crowller.h
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
#include"crowllerlist.h"
int NormalizeURL(char* URL)
{
int len = strlen(URL);
if (len <= 1 )
return 0;
//! Normalize all URLs.
if (URL[len - 1] == '/')
{
URL[len - 1] = 0;
len--;
}
int i, j;
len = strlen(URL);
//! Safe check.
if (len < 2)
return 0;
//! Locate the URL's suffix.
for (i = len - 1; i >= 0; i--)
if (URL[i] =='.')
break;
for (j = len - 1; j >= 0; j--)
if (URL[j] =='/')
break;
//! We ignore other file types.
//! So if a URL link is to a file that are not in the file type of the following
//! one of four, then we will discard this URL, and it will not be in the URL list.
if ((j >= 7) && (i > j) && ((i + 2) < len))
{
if ((!strncmp((URL + i), ".htm", 4))
||(!strncmp((URL + i), ".HTM", 4))
||(!strncmp((URL + i), ".php", 4))
||(!strncmp((URL + i), ".jsp", 4))
)
{
len = len; // do nothing.
}
else
{
return 0; // bad type
}
}
return 1;
}
//-------------------------//
void removeWhiteSpace(char* html)
{
int i;
char *buffer = malloc(strlen(html)+1), *p=malloc (sizeof(char)+1);
memset(buffer,0,strlen(html)+1);
for (i=0;html[i];i++)
{
if(html[i]>32)
{
sprintf(p,"%c",html[i]);
strcat(buffer,p);
}
}
strcpy(html,buffer);
free(buffer); free(p);
}
//-------------------//
int GetNextURL(char* html, char* urlofthispage, char* result, int pos)
{
char c;
int len, i, j;
char* p1; //!< pointer pointed to the start of a new-founded URL.
char* p2; //!< pointer pointed to the end of a new-founded URL.
// NEW
// Clean up \n chars
if(pos == 0) {
removeWhiteSpace(html);
}
// /NEW
// Find the <a> <A> HTML tag.
while (0 != (c = html[pos]))
{
if ((c=='<') &&
((html[pos+1] == 'a') || (html[pos+1] == 'A')))
{
break;
}
pos++;
}
//! Find the URL it the HTML tag. They usually look like <a href="www.abc.com">
//! We try to find the quote mark in order to find the URL inside the quote mark.
if (c)
{
// check for equals first... some HTML tags don't have quotes...or use single quotes instead
p1 = strchr(&(html[pos+1]), '=');
if ((!p1) || (*(p1-1) == 'e') || ((p1 - html - pos) > 10))
{
// keep going...
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (*(p1+1) == '\"' || *(p1+1) == '\'')
p1++;
p1++;
p2 = strpbrk(p1, "\'\">");
if (!p2)
{
// keep going...
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (*p1 == '#')
{ // Why bother returning anything here....recursively keep going...
return GetNextURL(html,urlofthispage,result,pos+1);
}
if (!strncmp(p1, "mailto:",7))
return GetNextURL(html, urlofthispage, result, pos+1);
if (!strncmp(p1, "http", 4) || !strncmp(p1, "HTTP", 4))
{
//! Nice! The URL we found is in absolute path.
strncpy(result, p1, (p2-p1));
return (int)(p2 - html + 1);
}
else
{
//! We find a URL. HTML is a terrible standard. So there are many ways to present a URL.
if (p1[0] == '.') {
//! Some URLs are like <a href="../../../a.txt"> I cannot handle this.
// again...probably good to recursively keep going..
// NEW
return GetNextURL(html,urlofthispage,result,pos+1);
// /NEW
}
if (p1[0] == '/') {
//! this means the URL is the absolute path
for (i = 7; i < strlen(urlofthispage); i++)
if (urlofthispage[i] == '/')
break;
strcpy(result, urlofthispage);
result[i] = 0;
strncat(result, p1, (p2 - p1));
return (int)(p2 - html + 1);
} else {
//! the URL is a absolute path.
len = strlen(urlofthispage);
for (i = (len - 1); i >= 0; i--)
if (urlofthispage[i] == '/')
break;
for (j = (len - 1); j >= 0; j--)
if (urlofthispage[j] == '.')
break;
if (i == (len -1)) {
//! urlofthis page is like http://www.abc.com/
strcpy(result, urlofthispage);
result[i + 1] = 0;
strncat(result, p1, p2 - p1);
return (int)(p2 - html + 1);
}
if ((i <= 6)||(i > j)) {
//! urlofthis page is like http://www.abc.com/~xyz
//! or http://www.abc.com
strcpy(result, urlofthispage);
result[len] = '/';
strncat(result, p1, p2 - p1);
return (int)(p2 - html + 1);
}
strcpy(result, urlofthispage);
result[i + 1] = 0;
strncat(result, p1, p2 - p1);
return (int)(p2 - html + 1);
}
}
}
return -1;
}
//--------------------------//
int if_already_present(char *tmp,char **result,int l)
{
int i;
for(i=0;i<l;i++)
{
if(strcmp(result[i],tmp)==0)
{
return 1;
}
}
return 0;
}
//---------------------------//
void print_links(char **result,int l)
{
int i;
for(i=0;i<l;i++)
{
printf("\n%s\n",result[i]);
}
}
//---------------------------//
void get_urls(char *html,int file_size,char *urlofthispage,int depth)
{
int i;
int pos=0;
char *result[61];
char *tmp;
result[0]=urlofthispage;
for(i=1;i<61&&pos<file_size;)
{
tmp=(char*)calloc(1,125);
pos=GetNextURL(html , urlofthispage , tmp , pos);
//NormalizeURL(tmp);
if(tmp[strlen(tmp)-1]=='/')
tmp[strlen(tmp)-1]='\0';
if(!(if_already_present(tmp,result,i)))
{
result[i]=tmp;
i++;
}
else
free(tmp);
}
put_links_in_list(result,i,depth);
}
//-------------------------//
void copy_content(char *open,char *url,int depth)
{
struct stat st; // variable which will count length of file.
stat(open,&st); // temp.txt is the file where wget fetch the html
int file_size=st.st_size;
char *html=(char*)calloc(1,file_size);
FILE *temp=fopen(open,"r");
char ch;
int i=0;
while((ch=fgetc(temp))!=EOF)
{
html[i++]=ch;
}
html[i]='\0';
get_urls(html,file_size,url,depth);
free(html);
}