-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
201 lines (162 loc) · 4.73 KB
/
crawler.js
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
/* Web crawler for Go-Cardless coding challenge*/
var readline = require('readline'); // For console input
var chr = require('cheerio'); // For parsing HTML
var req = require('request'); // For making HTML request
var pq = require('priorityqueuejs'); // For making a queue of the URLs visited
var rl = readline.createInterface
({
input: process.stdin,
output: process.stdout
});
var start_url = ""; // to store the input URL
var html_data = ""; // to store the HTML data of the web page
var already_visited = {}; // to store the links which have already been visited
var requisite_array = []; // To store the final output of JSON objects with URLs and assets
var queue = new pq(); // to store the non visited URLs
function ret_complete_url(url,path)
{
if (path.substring(0,4) != "http") // the path is not an absolute path
{
if (path.charAt(0) == '/') // path begins with a /
{
if (url.charAt(url.length-1) == '/')
url = url.substring(0,url.length-1)+path;
else
url = url+path;
return url;
}
else if (path.substring(0,2) == "./") // path begins with a ./
{
if (url.charAt(url.length-1) == '/')
url = url.substring(0,url.length-1)+path.substring(1,path.length);
else
url = url+path.substring(1,path.length);
return url;
}
else if (path.substring(0,3) == "../") // path begins with a ../
{
if (url.charAt(url.length-1) == '/')
url = url.substring(0,url.length-1)+path.substring(2,path.length);
else
url = url+path.substring(2,path.length);
return url;
}
else
{
if (url.charAt(url.length-1) == '/')
url = url+path;
else
url = url+"/"+path;
return url;
}
}
else
return path;
}
function abc()
{
if (queue.size() == 0)
{
console.log(requisite_array);
return 0;
}
var my_url = queue.peek().toString(); // pick the first non visited URL
// console.log(my_url);
queue.deq(); // remove the first item from the queue
var done = false;
req(String(my_url), function(err,response,html)
{
// console.log("here");
if (err)
console.log("Error in making a request!!!");
else
{
var $ = chr.load(html); // Load the html in the JQuery context
var cur_url = my_url; // current URL
var resource = {}; // to store the resources of the current page
resource["url"] = cur_url;
resource["assets"] = [];
// links reachable from the current page
var a_tags = $("a");
for(var i = 0; i < a_tags.length; i++)
{
if (a_tags[i].attribs.hasOwnProperty("href")) // if the tag has a source
{
var page_path = a_tags[i].attribs["href"];
var page_src = ret_complete_url(cur_url,page_path);
if (already_visited.hasOwnProperty(page_src) != true && page_path.substring(0,4) == "http")
{
queue.enq(page_src);
already_visited[page_src] = true;
}
}
}
// images as resources
a_tags = $("img");
for(var i = 0; i < a_tags.length; i++)
{
if (a_tags[i].attribs.hasOwnProperty("src")) // if the tag has a source
{
var img_src = a_tags[i].attribs["src"];
img_src = ret_complete_url(cur_url,img_src);
resource["assets"].push(img_src);
}
}
a_tags = $("body");
for(var i = 0; i < a_tags.length; i++)
{
if (a_tags[i].attribs.hasOwnProperty("background")) // if the body tag has a background attribute
{
var img_src = a_tags[i].attribs["background"];
img_src = ret_complete_url(cur_url,img_src);
resource["assets"].push(img_src);
}
}
// scripts as resources
a_tags = $("script");
for(var i = 0; i < a_tags.length; i++)
{
if (a_tags[i].attribs.hasOwnProperty("src")) // if the tag has a source
{
var script_src = a_tags[i].attribs["src"];
script_src = ret_complete_url(cur_url,script_src);
resource["assets"].push(script_src);
}
}
// stylesheets as resources
a_tags = $("link");
for(var i = 0; i < a_tags.length; i++)
{
if (a_tags[i].attribs.hasOwnProperty("rel")) // if the tag has the rel attribute
{
if (a_tags[i].attribs.hasOwnProperty("href"))
{
var link_rel = a_tags[i].attribs["rel"];
if (link_rel == "stylesheet") // if the resource is a stylesheet
{
var stylesheet_src = a_tags[i].attribs["href"]; // source of the stylesheet
stylesheet_src = ret_complete_url(cur_url,stylesheet_src);
resource["assets"].push(stylesheet_src);
}
}
}
}
requisite_array.push(resource);
// console.log(requisite_array);
}
var funcall = abc();
});
}
function main()
{
rl.question("Please enter the url: ", function(answer)
{
start_url = answer;
rl.close();
// add the start URL to the priority queue and hash table
already_visited[start_url] = true;
queue.enq(start_url);
var funcall = abc();
});
}
var execute = main();