-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spider.java
271 lines (225 loc) · 5.96 KB
/
Spider.java
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
package com.ucr.cs172.project.crawler;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.net.URI;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.List;
import java.util.ArrayList;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Spider
{
private String seed = "http://arstechnica.com/";
private Document htmlDocument;
private String seedFilePath;
private int maxHopDistance;
private boolean seedPopulated = false;
private final long DOMAIN_WAIT_TIME_MILLI = 2000;
//LinkedBlockingQueue is to be used without the blocking capabilities
//ArrayList of queues that will track the hop depth from original seed urls
private List<LinkedBlockingQueue<String>> queueArrayList;
//This hash map will keep track
private ConcurrentHashMap<String, Long> visitedUrlHashMap = new ConcurrentHashMap<String, Long>();
private ConcurrentHashMap<String, Long> visitedDomainHashMap = new ConcurrentHashMap<String, Long>();
public Spider(String seedFilePath, int maxHopDistance)
{
this.seedFilePath = seedFilePath;
this.maxHopDistance = maxHopDistance;
queueArrayList = new ArrayList<LinkedBlockingQueue<String>>();
for (int i = 0; i <= maxHopDistance; i++)
{
queueArrayList.add(new LinkedBlockingQueue<String>());
}
this.seedPopulated = true;
}
public List<LinkedBlockingQueue<String>> getQueueArrayList()
{
return this.queueArrayList;
}
public ConcurrentHashMap<String, Long> getConcurrentHashMap()
{
return this.visitedUrlHashMap;
}
public boolean crawl(String url, int queueNumber)
{
try
{
Connection connection = Jsoup.connect(url);
Document htmlDocument = connection.get();
this.htmlDocument = htmlDocument;
if(connection.response().statusCode() == 200) // 200 is the HTTP OK status code
// indicating that everything is great.
{
System.out.println("\n**Visiting** Received web page at " + url);
}
if(!connection.response().contentType().contains("text/html"))
{
System.out.println("**Failure** Retrieved something other than HTML");
return false;
}
Elements linksOnPage = htmlDocument.select("a[abs:href]");
System.out.println("Found (" + linksOnPage.size() + ") links adding to queue number: " + queueNumber);
for(Element link : linksOnPage)
{
if(queueNumber >= 0)
{
this.queueArrayList.get(queueNumber).put(link.absUrl("href"));
}
}
return true;
}
catch(Exception e)
{
System.out.println("crawl exception");
// We were not successful in our HTTP request
return false;
}
}
public String nextUrl(int queueNumber)
{
String nextUrl;
do
{
nextUrl = this.queueArrayList.get(queueNumber).poll();
nextUrl = normalizeUrl(nextUrl);
while(nextUrl == "")
{
nextUrl = this.queueArrayList.get(queueNumber).poll();
nextUrl = normalizeUrl(nextUrl);
}
}
while(this.visitedUrlHashMap.containsKey(nextUrl));
this.visitedUrlHashMap.put(nextUrl, System.currentTimeMillis());
String hostUrl = getHost(nextUrl);
System.out.println(hostUrl);
if(this.visitedDomainHashMap.containsKey(hostUrl))
{
long hostElapsedTime = System.currentTimeMillis() - this.visitedDomainHashMap.get(hostUrl);
System.out.println("" + hostElapsedTime);
if( hostElapsedTime < DOMAIN_WAIT_TIME_MILLI)
{
try
{
Thread.sleep(hostElapsedTime);
}
catch(Exception e)
{
System.out.println("Interrupted");
}
}
}
this.visitedDomainHashMap.put(hostUrl, System.currentTimeMillis());
return nextUrl;
}
public int listSize()
{
try
{
//System.out.println("Queue size: " + queueArrayList.size());
return queueArrayList.size();
}
catch(Exception e)
{
System.out.println("listSize Error");
return 0;
}
}
public void testSeedInit()
{
try
{
queueArrayList.get(0).put(seed);
}
catch(Exception e)
{
System.out.println("testSeedInit Error");
}
}
public void printSeedQ()
{
try
{
System.out.println("Seed Size: " + queueArrayList.get(0).size());
}
catch(Exception e)
{
System.out.println("printSeedQ Error");
}
}
public String removeTrailingSlash(String url)
{
if(url.length() > 0)
{
if(url.charAt(url.length() - 1) == '/')
{
return new String(url.substring(0, url.length()-1));
}
}
return url;
}
public String removeBookmark(String url)
{
if(url.lastIndexOf('#') >= 0)
{
return "";
}
return url;
}
public boolean confirmDotGov(String url)
{
if(url.indexOf(".gov") != -1)
{
return true;
}
else
{
return false;
}
}
public String normalizeUrl(String url)
{
String temp = removeBookmark(url);
if (temp == "")
{
return "";
}
else
{
return removeTrailingSlash(temp);
}
}
public String getHost(String url)
{
try
{
URI temp = new URI(url);
return temp.getHost();
}
catch(Exception e)
{
return "";
}
}
public List<String> getUrlSeeds (String seedsFilePath) {
List<String> seeds = new ArrayList<String>();
// Read from the file and store the urls in a List
try {
// Get the file
FileReader fr = new FileReader(seedsFilePath);
BufferedReader in = new BufferedReader(fr);
String inputLine;
while ((inputLine = in.readLine()) != null)
seeds.add(inputLine);
// close the reader stream
in.close();
} catch (IOException e) {
System.out.println("File I/O Error!!!");
}
return seeds;
}
}