Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.java
More file actions
315 lines (268 loc) · 10.2 KB
/
Copy pathCrawler.java
File metadata and controls
315 lines (268 loc) · 10.2 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Crawler extends Thread {
/**
*
*/
public static ConcurrentHashMap<String, Integer> Vis_URL = new ConcurrentHashMap<String, Integer>();
public static ConcurrentHashMap<String, String> Hashed_URL = new ConcurrentHashMap<String, String>();
public static Queue<String> To_Vis_URL = new ConcurrentLinkedQueue<String>();
public static ConcurrentHashMap<String, Integer> Ranking = new ConcurrentHashMap<String, Integer>();
public int maxdepth = 10;
public static int VisUrlSz = 0;
public static int cnt;
public Number statenum = 0;
public boolean flag = true;
public int time;
Crawler(int t) {
time = t;
}
public ConcurrentHashMap<String, Integer> GetHashRank (){
return Ranking;
}
public synchronized void Visited(String site) {
Vis_URL.put(site, 1);
}
public synchronized void SetHash(String site, Document doc) {
// Vis for Freq array
Elements e = doc.getElementsByTag("title");
String title = e.text();
e = doc.getElementsByTag("meta");
String meta = e.attr("content");
e = doc.getElementsByTag("a");
String a = e.text();
Hashed_URL.put(site, a + title + meta);
}
public synchronized void towrite(String site) {
try (BufferedWriter statefile = new BufferedWriter(new FileWriter("/home/mostafa/NetBeansProjects/WebApplication5/State", true))) {
statefile.write(site + "/");
statefile.newLine();
} catch (IOException e) {
System.out.println(e);
}
}
public synchronized boolean Check(String site) {
if (Vis_URL.get(site) == null) {
return true;
}
return false;
}
public synchronized void tovis(String site) {
if (Vis_URL.get(site) == null) {
To_Vis_URL.add(site);
}
Object o = Ranking.get(site);
if (o == null) {
Ranking.put(site,1);
return;
}
Ranking.put(site, (int)o +1);
}
public ConcurrentHashMap<String, Integer> GetRanking(){
return Ranking;
}
public void processPage(String URL_Link, int depth) throws InterruptedException, MalformedURLException {
if (VisUrlSz >= 900) {
System.out.println("ANA 5LST.. 25ern b2et farasha gamyleeeeh 2222!!");
return;
}
Visited(URL_Link);
try {
URL RobotURL = new URL(URL_Link);
String Host = "http://" + RobotURL.getHost();
//System.out.println("Host= " + Host);
// String temp1 = link.attr("abs:href");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(new URL(Host + "/robots.txt").openStream()))) {//temp1 + "robots.txt
//System.out.println("B3d " + e.attr("abs:href"));
String line = null;
int tcnt = 1;
while ((line = in.readLine()) != null && ++tcnt < 50) {
if (line.startsWith("Disallow")) {
try (BufferedWriter rob = new BufferedWriter(
new FileWriter("/home/mostafa/NetBeansProjects/WebApplication5/robot", true))) {
String y = line.substring(9);
rob.write(y);
rob.newLine();
String Comp = Host;
if (y.length() > 3) {
String te = line.substring(10), te2 = "";
for (int i = 0; i < te.length(); i++) {
if (te.charAt(i) == '*') {
te2 += "(.*)";
} else {
te2 += te.charAt(i);
}
}
// System.out.println("teStre " + te + "// te2String " + te2);
// System.out.println(Host);
if (URL_Link.matches(te2)) {
System.out.println("msktk !!");
return;
}
}
} catch (IOException e1) {
System.out.println(e1);
}
}
}
} catch (IOException e1) {
System.out.println(e1);;
} catch (IllegalArgumentException ee) {
System.out.println(ee);
}
} catch (java.net.MalformedURLException e) {
System.out.println("Could not create URL !!");
}
// System .out.println(x);
if (URL_Link.endsWith("/")) {
URL_Link = URL_Link.substring(0, URL_Link.length() - 1);
}
Document doc = null;
try {
if (URL_Link == "") {
return;
}
doc = Jsoup.connect(URL_Link).get();
} catch (IOException e) {
System.out.println("Doc Not connected to " + URL_Link);
return;
}
System.out.println("D5l be " + URL_Link);
SetHash(URL_Link, doc);
towrite(URL_Link);
// HTML file
try (BufferedWriter file = new BufferedWriter(
new FileWriter("/home/mostafa/NetBeansProjects/WebApplication5/HTML/" + this.cnt++ + ".html", false))) {
file.write((doc.toString()));
VisUrlSz++;
} catch (IOException e) {
System.out.println(e);
}
Elements links = null;
try {
links = doc.body().getElementsByAttribute("href");
} catch (NullPointerException e) {
return;
}
if (links != null) {
for (Element link : links) {
tovis(link.attr("abs:href"));// In queue
}
}
//System.out.println("size abl: " + To_Vis_URL.size());
while (!To_Vis_URL.isEmpty() && (int) (System.currentTimeMillis() / 1000) < time) {
String site = To_Vis_URL.poll();
if (Check(site)) {
processPage(site, depth + 1);
}
}
System.out.println("ANA 5LST.. 25ern b2et farasha gamyleeeeh !!");
}
public void mainprocessPage(String URL, HashMap<String, Integer> Vis) {
if (!Vis.isEmpty()) {
Vis_URL.putAll(Vis);
}
int cnter = Vis.size();
if (cnter != 0) {
cnt = cnter;
} else if (!Vis_URL.isEmpty()) {
cnt = Vis_URL.size();
} else {
cnt = 0;
}
//System.out.println("Main Process Man cnt = " + cnt);
if (Hashed_URL.isEmpty()) {
Document doc = null;
try {
doc = Jsoup.connect(URL).get();
} catch (IOException e) {
System.out.println(e);
}
try (BufferedWriter file = new BufferedWriter(
new FileWriter("/home/mostafa/NetBeansProjects/WebApplication5/HTML/" + cnt++ + ".html", false))) {
file.write((doc.toString()));
VisUrlSz++;
} catch (IOException e) {
System.out.println(e);
}
Visited(URL);
SetHash(URL, doc);
towrite(URL);
Elements temp = doc.body().getElementsByAttribute("href");
for (Element e : temp) {
tovis(e.attr("abs:href"));
}
} else { // for freq
//System.out.println("d5l fy al else..");
String Lsite = "";
Iterator<?> it = Hashed_URL.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
String site = (String) pair.getKey();
String has = (String) pair.getValue();
Lsite = site;
Document doc = null;
try {
doc = Jsoup.connect(site).get();
} catch (IOException e) {
System.out.println(e);
}
//get meta + a + title
Elements e = doc.getElementsByTag("title");
String title = e.text();
e = doc.getElementsByTag("meta");
String meta = e.attr("content");
e = doc.getElementsByTag("a");
String a = e.text();
String temp = a + title + meta;
if (!has.equals(temp)) {
// System.out.println("has= " + has + "site= " + a + title);
// System.out.println("d5l fy to visit..2");
tovis(site);
Lsite = "";
} else {
System.out.println("Not changed!!");
}
}
if (!Lsite.isEmpty()) {
tovis(Lsite);
}
}
}
@Override
public void run() {
// System.out.println("cnt is: " + cnt);
while (!To_Vis_URL.isEmpty()) {
String site = To_Vis_URL.poll();
// System.out.println("Run ste is: " + site);
try {
processPage(site, 1);
break;
} catch (InterruptedException ex) {
Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
}
}
// State file
}
}
You can’t perform that action at this time.
