-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebCrawler.java
173 lines (152 loc) · 4.8 KB
/
WebCrawler.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
import java.net.*;
import java.io.*;
import adc.parser.*;
import java.util.*;
public class WebCrawler implements Runnable {
URL uin;
int num;
int bytesread=0;
static int numconnects = 0;
static int numthreads = 0;
static int urls = 0;
static int numdups = 0;
static int numwaiting = 0;
static double bps = 0;
static double waittime = 0;
static double connecttime = 0;
static Hashtable visited = new Hashtable(10);
static Object indicator = new Object();
WebCrawler(URL u) {
uin = u;
urls++;
num = urls;
}
static public void main(String argv[]) {
int intv = Integer.parseInt(argv[1]);
new Thread(new Clock(intv)).start();
try { new Thread(new WebCrawler(new URL(argv[0]))).start(); }
catch (MalformedURLException e) {
System.out.println("Bad input URL");
}
}
public void run() {
try {
numthreads++;
URL[] uout = go();
for(int i = 0; i<uout.length; i++) {
if (!(visited.get(uout[i]) == indicator)) {
visited.put(uout[i], indicator);
new Thread(new WebCrawler(uout[i])).start();
}
else numdups++;
}
}
finally {numthreads--;}
}
URL[] go() {
Object contents = null;
long time = System.currentTimeMillis();
try {
numwaiting++;
try {
contents = uin.getContent();
}
catch (IOException e) {
//System.out.println("couldn't read this URL: " +
// uin.toString());
}
}
finally { numwaiting--;
waittime = (waittime*(double)((double)num/(double)(num + 1))) +
((double)(System.currentTimeMillis()-time)/(double)(num + 1));
}
time = System.currentTimeMillis();
try {
numconnects++;
if (!(contents instanceof InputStream)) return new URL[0];
HtmlStreamTokenizer htmlstream = new HtmlStreamTokenizer((InputStream)contents);
Vector v = new Vector();
while (true) {
URL thing = getNextURL(htmlstream);
if (thing==null) break;
v.addElement(thing);
}
bytesread = htmlstream.numbytes;
return vectorToArray(v);
}
finally { numconnects--;
long now = System.currentTimeMillis()-time;
double b = (double)bytesread;
double n = (double)now;
double nsec = (n+1)/1000.0;
double numa = (double)num;
double fract = numa / (numa + 1);
connecttime = (connecttime*fract) + (n/(numa+1));
bps = (bps*fract) + ((b/nsec)/(numa+1));
}
}
URL[] vectorToArray(Vector v) {
URL[] output = new URL[v.size()];
int i = 0;
for(Enumeration e = v.elements(); e.hasMoreElements();) {
output[i++] = (URL)e.nextElement();
}
return output;
}
URL getNextURL(HtmlStreamTokenizer h) {
HtmlTag tag = new HtmlTag();
try {
while (h.nextToken() != HtmlStreamTokenizer.TT_EOF) {
int ttype = h.getTokenType();
if (ttype == HtmlStreamTokenizer.TT_TAG) {
try {
h.parseTag(h.getStringValue(), tag);
if (tag.getTagType() == HtmlTag.T_A) {
String href = tag.getParam(HtmlTag.P_HREF);
if (href != null) {
URL output = null;
try {
output = new URL(uin, href);
}
catch (MalformedURLException e) { System.out.println("new URL " + href + " didn't work"); }
return output;
}
}
}
catch (HtmlException f) {
System.out.println("html error");
}
}
}
return null;
}
catch (IOException g) {}
return null; // dumbass compiler
}
}
class Clock implements Runnable {
long currenttime = System.currentTimeMillis();
long base = currenttime;
long interval = 1000;
Clock(long intv) {
interval = intv;
}
public void run() {
while (true) {
long now = System.currentTimeMillis();
if ((now - currenttime) > interval) {
System.out.println("Time is " + (now-base) +
". Avg Bytes/Second: " + WebCrawler.bps + ".");
System.out.println("#Threads: " + WebCrawler.numthreads +
". #Active Connections: " + WebCrawler.numconnects +
". Avg Connect Time: " + (WebCrawler.connecttime/1000) +
". \n#Waiting to Connect: " + WebCrawler.numwaiting +
". Avg Wait to Connect Time: " + (WebCrawler.waittime/1000) +
". \n#Duplicate URLs pruned: " + WebCrawler.numdups + ".");
currenttime = now;
}
try { Thread.currentThread().sleep(1000); }
catch(InterruptedException e) {}
}
}
}