-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawler.java
780 lines (748 loc) · 34.3 KB
/
Crawler.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
package crawler;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import database_manager.DatabaseManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Crawler implements Runnable {
private class UrlObject {
public String url;
public java.util.Date date;
public int id;
public UrlObject(String url, java.util.Date date, int id) {
this.url = url;
this.date = date;
this.id = id;
}
}
private static class UrlInDB {
public java.util.Date date;
public int id;
public UrlInDB(java.util.Date date, int id) {
this.date = date;
this.id = id;
}
}
// max number of website to crawl may be more but not less
public static final int MAX_WEBSITES = 5000;
private static final int EXTRA_MAX_WEBSITES_FACTOR = 5;
private static final int EXTRA_FOR_IO_EXEPTION = MAX_WEBSITES / EXTRA_MAX_WEBSITES_FACTOR;
public static final int TOTAL_MAX_WEBSITES_TO_QUEUE = MAX_WEBSITES + EXTRA_FOR_IO_EXEPTION;
// user agent name
private static final String USER_AGENT_NAME = "*";
// start crawling time
private final long startCrawlingTime = System.currentTimeMillis();
// number of threads to run
private static final int numThreads = 10;
// seedset file for intail crawling
public static final String seedSetFileName = "./src/crawler/SeedSet.txt";
// folder to download the crawled pages
public static final String outputFolderBase = "./html_docs/";
public static final DatabaseManager dbManager = new DatabaseManager(); // database manager instance
// formatter to GMT
public static final SimpleDateFormat formatter= new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
public static final Object LOCK_LINKS_QUEUE = new Object(); // linksQueue lock
public static final Object LOCK_VISITED_SET = new Object(); // visitedSet Lock
private static final Object LOCK_RECRAWLING_QUEUE = new Object(); // linksQueue lock
public static final Object LOCK_LINKS_DB_FAIL = new Object(); // fail list lock
public static List<String> failedLinksList = new ArrayList<>(); // linksQueue lock
public static Queue<String> linksQueue = new LinkedList<>(); // linksQueue lock
public static HashMap<String, UrlInDB> visitedLinks = new HashMap<>(); // visited links list
private static Queue<UrlObject> recrawlingQueue = new LinkedList<>(); // recrawling urls queue
// array of connections every thread has a connection with the sql server
private static List<Connection> connections = new ArrayList<>();
// blocked extensions from crawling
private final List<String> extensions = new ArrayList<>() {
{
// videos
add(".gif");
add(".gifv");
add(".mp4");
add(".webm");
add(".mkv");
add(".flv");
add(".vob");
add(".ogv");
add(".ogg");
add(".avi");
add(".mts");
add(".m2ts");
add(".ts");
add(".mov");
add(".qt");
add(".wmv");
add(".yuv");
add(".rm");
add(".rmvb");
add(".asf");
add(".amv");
add(".m4p");
add(".m4v");
add(".mpg");
add(".mp2");
add(".mpeg");
add(".mpe");
add(".mpv");
add(".m2v");
add(".m4v");
add(".svi");
add(".3gp");
add(".3g2");
add(".mxf");
add(".roq");
add(".nsv");
add(".f4v");
// images
add(".png");
add(".jpg");
add(".webp");
add(".tiff");
add(".psd");
add(".raw");
add(".bmp");
add(".heif");
add(".indd");
add(".jp2");
add(".svg");
add(".ai");
add(".eps");
add(".pdf"); // not html
add(".ppt"); // not html
}
};
public Crawler() {}
/* if url end of one of the blocked extensions return true otherwise false */
private boolean is_url_end_with_extensions(String url) {
return extensions.stream().anyMatch(entry -> url.endsWith(entry));
}
/* dequeue url from state table */
private boolean dequeue_state(Connection connection) {
try {
String query = "DELETE FROM state LIMIT 1;";
Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate(query);
return true;
} catch (SQLException e) {
e.getMessage();
return false;
}
}
/* enqueue url to state table */
private boolean enqueue_state(Connection connection, String url) {
try{
String query = String.format("INSERT INTO state (url) VALUES ('%s');", url);
Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate(query);
return true;
} catch (SQLException e) {
e.getMessage();
synchronized (Crawler.LOCK_LINKS_DB_FAIL) {
failedLinksList.add(url);
}
return false;
}
}
/* enqueue list of urls to state table */
private boolean enqueue_multiple_links(Connection connection, List<String> urls) {
try {
String query = "INSERT INTO state (url) VALUES (?);";
PreparedStatement pst = connection.prepareStatement(query);
connection.setAutoCommit(false);
for (String url : urls) {
pst.setString(1, url);
pst.addBatch();
}
pst.executeLargeBatch();
connection.setAutoCommit(true);
return true;
} catch (SQLException e) {
e.getMessage();
failedLinksList.addAll(urls);
return false;
}
}
private boolean clear_state(Connection connection) {
try{
String query = "DELETE FROM state;";
Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate(query);
return true;
} catch (SQLException e){
e.getMessage();
return false;
}
}
private boolean is_allowed_url(Connection connection, String url) {
try {
if(is_url_end_with_extensions(url)) {
return false;
}
boolean flagAlreadyVisited = false;
// not necessary but instead of parsing robot.txt for already visited link
synchronized (Crawler.LOCK_VISITED_SET) {
if (Crawler.visitedLinks.containsKey(url)) {
flagAlreadyVisited = true;
}
}
if(flagAlreadyVisited) {
return false;
}
URI uri = new URI(url);
uri = uri.normalize();
String path = uri.getPath();
// System.out.println(path);
String rootPath = url.replace(path, "");
String robotPath = rootPath + "/robots.txt";
// System.out.println(robotPath);
try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL(robotPath).openStream()))) {
String line;
boolean flag = false;
final String disallowStartWith = "disallow:";
final String userAgentStartWith = "user-agent:";
while ((line = in.readLine()) != null) {
int offset = line.indexOf("#");
if (offset != -1) {
line = line.substring(0, offset);
}
line = line.replaceAll(" ", "").replace("\n", "").toLowerCase();
// System.out.println(line);
if (flag) {
if (line.startsWith(disallowStartWith)) {
offset = line.indexOf(":");
if (-1 != offset && ++offset < line.length()) {
String disallowedValue;
if (line.charAt(line.length() - 1) == '/') {
disallowedValue = line.substring(offset, line.length() - 1);
} else {
disallowedValue = line.substring(offset);
}
String disallowedURL = rootPath + disallowedValue;
// System.out.println(disallowedURL);
if (url.length() >= disallowedURL.length()
&& disallowedURL.equals(url.substring(0, disallowedURL.length()))) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("(Robot.txt): Not allowed to crawl: " + url);
System.out.println(
"_________________________________________________________________________________________");
return false;
}
}
} else if (line.startsWith(userAgentStartWith)) {
break;
}
} else if (line.startsWith(userAgentStartWith)) {
if (userAgentStartWith.length() < line.length()) {
if (line.substring(userAgentStartWith.length()).equals(Crawler.USER_AGENT_NAME)) {
// System.out.println("FOUND: " + line);
flag = true;
}
}
}
}
return true;
}
} catch (IOException ex1) {
synchronized (Crawler.LOCK_LINKS_QUEUE) { // try again later by pushing in the end of queue
if(this.enqueue_state(connection, url)) {
Crawler.linksQueue.add(url);
}
}
return false;
} catch (URISyntaxException | IllegalArgumentException e) {
return false;
}
}
public static String normalizeUrl(String urlStr) {
if (urlStr == null) {
return null;
}
String path = urlStr;
if (path != null) {
String http = "http:/";
String https = "https:/";
// http:////wWw.Fb.cOm////
path = path.replaceAll("//*/", "/"); // http:/wWw.Fb.cOm/
if (path.startsWith(http)) {
path = path.substring(http.length()); // wWw.Fb.cOm/
} else if (path.startsWith(https)) {
path = path.substring(https.length());
}
path = http + "/" + path; // http://wWw.Fb.cOm/
if (path.charAt(path.length() - 1) == '/') {
path = path.substring(0, path.length() - 1);
}
// http://wWw.Fb.cOm
path = path.toLowerCase();
// http://www.fb.com
}
return path;
}
public void recrawling(Connection connection) {
System.out.println("Thread (" + Thread.currentThread().getName() + "): starts recrawling");
synchronized (Crawler.LOCK_RECRAWLING_QUEUE) {
if (Crawler.recrawlingQueue.isEmpty()) {
synchronized (Crawler.LOCK_LINKS_QUEUE) {
Crawler.linksQueue.clear(); // clear in the start to make sure nothing there
clear_state(connection);
}
synchronized (Crawler.LOCK_VISITED_SET) {
Crawler.visitedLinks.entrySet().forEach(entry -> {
Crawler.recrawlingQueue.add(new UrlObject(entry.getKey(), entry.getValue().date, entry.getValue().id));
});
}
}
}
while (true) {
UrlObject crawledURL = null;
// start lock
boolean flagEmptyQueue = false;
// start lock
synchronized (Crawler.LOCK_RECRAWLING_QUEUE) {
if (!Crawler.recrawlingQueue.isEmpty()) {
crawledURL = Crawler.recrawlingQueue.poll();
} else {
flagEmptyQueue = true;
}
}
// end lock
if(flagEmptyQueue) {
break;
}
try {
if(crawledURL == null) {
continue;
}
final URL url = new URL(crawledURL.url);
try {
System.out.println("Time: " + (System.currentTimeMillis() - this.startCrawlingTime)
+ ", recrawling url : " + url);
URLConnection connectionHead = url.openConnection();
String lastModified = connectionHead.getHeaderField("Last-Modified");
boolean isChanged = false;
java.util.Date lastModifiedDate = null;
java.util.Date downloadDate = Crawler.visitedLinks.get(crawledURL.url).date;
if(lastModified != null) {
lastModifiedDate = Crawler.formatter.parse(lastModified);
isChanged = lastModifiedDate.after(downloadDate);
}
final int delay = 3600000; // ms
java.util.Date dateToRecrawl = new Date(System.currentTimeMillis() - delay);
// if lastModifiedDate is null update
if(isChanged || (lastModifiedDate == null && downloadDate.before(dateToRecrawl))) {
Document urlContent = save_url_to_db(connection, url.toString(), crawledURL.id);
if(urlContent != null) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Thread (" + Thread.currentThread().getName() + "): " + crawledURL
+ " is now added to output folder");
System.out.println(
"_________________________________________________________________________________________");
final Elements linksFound = urlContent.select("a[href]");
List<String> urls = new ArrayList<>();
for (final Element link : linksFound) {
final String urlText = link.attr("abs:href");
String path = normalizeUrl(urlText); // URL Normalization
if (this.is_allowed_url(connection, path)) {
urls.add(path);
}
}
if(urls.size() > 0) {
// start lock
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if(this.enqueue_multiple_links(connection, urls)) {
Crawler.linksQueue.addAll(urls);
}
}
// end lock
}
} else {
/* delete from db */
// delete_from_db(connection, crawledURL.url, crawledURL.id);
/* delete from HDD */
// File toDeleteFile = new File(outputFolderBase+crawledURL.id+".html");
// toDeleteFile.delete();
}
}
} catch (IOException e) {
synchronized (Crawler.LOCK_LINKS_QUEUE) { // try again later by pushing in the end of queue
Crawler.recrawlingQueue.add(crawledURL);
}
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error IO-Exception while crawling : " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
} catch (ParseException e){
e.printStackTrace();
}
} catch (MalformedURLException e) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error Mal-Formed-URL while crawling : " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
}
}
}
public void crawling(Connection connection){
System.out.println("Thread (" + Thread.currentThread().getName() + "): starts crawling");
while (true) {
String crawledURL = "";
// start lock
boolean flagReachedToMax = false;
synchronized (Crawler.LOCK_VISITED_SET) {
if (visitedLinks.size() >= MAX_WEBSITES) {
flagReachedToMax = true;
}
}
if (flagReachedToMax) {
return;
}
// end lock
boolean flagAlreadyVisited = false;
boolean flagEmptyQueue = false;
// start lock
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if (!Crawler.linksQueue.isEmpty()) {
if(dequeue_state(connection)) {
crawledURL = Crawler.linksQueue.poll();
synchronized (Crawler.LOCK_VISITED_SET) {
if (Crawler.visitedLinks.containsKey(crawledURL)) {
flagAlreadyVisited = true;
}
}
} else {
flagAlreadyVisited = true;
}
} else {
System.out.println("Thread (" + Thread.currentThread().getName() + "): Empty Queue List");
flagEmptyQueue = true;
}
}
if (flagAlreadyVisited || flagEmptyQueue) {
continue;
}
// end lock
try {
final URL url = new URL(crawledURL);
try {
System.out.println("Time: " + (System.currentTimeMillis() - this.startCrawlingTime)
+ ", crawling url : " + url);
Document urlContent = save_url_to_db(connection, url.toString(), -1);
if(urlContent != null) {
System.out.println("Thread (" + Thread.currentThread().getName() + "): " + crawledURL
+ " is now added to output folder");
int remainingLinksCount;
synchronized (Crawler.LOCK_LINKS_QUEUE) {
synchronized (Crawler.LOCK_VISITED_SET) {
remainingLinksCount = Crawler.TOTAL_MAX_WEBSITES_TO_QUEUE - (Crawler.visitedLinks.size() + Crawler.linksQueue.size());
}
}
if (remainingLinksCount <= 0) {
continue; // continue downloading pages
}
List<String> urls = new ArrayList<>();
final Elements linksFound = urlContent.select("a[href]");
for (final Element link : linksFound) {
final String urlText = link.attr("abs:href");
// start lock
String path = normalizeUrl(urlText); // URL Normalization
if (this.is_allowed_url(connection, path)) {
urls.add(path);
remainingLinksCount--;
}
if(remainingLinksCount == 0){
break;
}
}
if(urls.size() > 0) {
// start lock
synchronized (Crawler.LOCK_LINKS_QUEUE) { // make sure before add to db
synchronized (Crawler.LOCK_VISITED_SET) {
remainingLinksCount = Crawler.TOTAL_MAX_WEBSITES_TO_QUEUE - (Crawler.linksQueue.size() + Crawler.visitedLinks.size());
}
}
// end lock
if(remainingLinksCount <= 0) {
continue;
}
if(urls.size() > remainingLinksCount) {
urls = urls.subList(0, remainingLinksCount);
}
// start lock
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if(this.enqueue_multiple_links(connection, urls)) {
Crawler.linksQueue.addAll(urls);
}
}
// end lock
}
}
} catch (IOException e) {
synchronized (Crawler.LOCK_LINKS_QUEUE) { // try again later by pushing in the end of queue
if(this.enqueue_state(connection, crawledURL)) {
Crawler.linksQueue.add(crawledURL);
}
}
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error IO-Exception while crawling : " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
}
} catch (MalformedURLException e) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error Mal-Formed-URL while crawling : " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
}
}
}
public void crawlUpdatedLinks(Connection connection){
System.out.println("Thread (" + Thread.currentThread().getName() + "): starts crawl updated links");
while (true) {
String crawledURL = "";
boolean flagVisitedLink = false, flagEmptyQueue = false;
// start lock
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if (!Crawler.linksQueue.isEmpty()) {
if(dequeue_state(connection)) {
crawledURL = Crawler.linksQueue.poll();
synchronized (Crawler.LOCK_VISITED_SET) {
if (Crawler.visitedLinks.containsKey(crawledURL)) {
flagVisitedLink = true;
}
}
} else {
flagVisitedLink = true;
}
} else {
System.out.println("Thread (" + Thread.currentThread().getName() + "): Empty Queue List");
flagEmptyQueue = true;
}
}
if (flagVisitedLink) {
continue; // get new one from queue
}
if (flagEmptyQueue) {
break;
}
// end lock
try {
final URL url = new URL(crawledURL);
try {
System.out.println("Time: " + (System.currentTimeMillis() - this.startCrawlingTime)
+ ", crawling new url in recrawling: " + url);
Document urlContent = save_url_to_db(connection, url.toString(), -1);
} catch (IOException e) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error IO-Exception while crawling new Recrawled links: " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
}
} catch (MalformedURLException e) {
System.out.println(
"_________________________________________________________________________________________");
System.out.println("Error Mal-Formed-URL while crawling new Recrawled links : " + crawledURL);
System.out.println(
"_________________________________________________________________________________________");
}
}
}
public Document save_url_to_db(Connection connection, String url, int updateId) throws IOException{
Document urlContent = null;
try {
urlContent = Jsoup.connect(url).get();
Date date = new Date(System.currentTimeMillis());
String query;
if(updateId != -1) { // update
query = String.format("UPDATE page SET url='%s', crawled_time='%s' WHERE id=%d;", url, Crawler.formatter.format(date), updateId);
}
else{ // insert
query = String.format("INSERT INTO page (url, crawled_time) VALUES ('%s', '%s');", url, Crawler.formatter.format(date));
}
Statement stmt = connection.createStatement();
synchronized (Crawler.LOCK_VISITED_SET) {
if(!Crawler.visitedLinks.containsKey(url)) {
int rowsAffected = stmt.executeUpdate( query, Statement.RETURN_GENERATED_KEYS );
ResultSet rs = stmt.getGeneratedKeys();
rs.beforeFirst();
rs.next();
int id = updateId != -1? updateId:rs.getInt(1);
stmt.close();
// System.out.println("ID: "+ id);
BufferedWriter writer = new BufferedWriter(new FileWriter(Crawler.outputFolderBase + id + ".html"));
writer.write(urlContent.toString());
writer.close();
Crawler.visitedLinks.put(url, new UrlInDB(date, id));
} else {
urlContent = null;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return urlContent;
}
public void delete_from_db(Connection connection, String url, int deleteId) {
try {
String query = String.format("DELETE FROM page WHERE id=%d;", deleteId);
Statement stmt = connection.createStatement();
int rowsAffected = stmt.executeUpdate(query);
synchronized (Crawler.LOCK_VISITED_SET) {
Crawler.visitedLinks.remove(url);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void handel_falied_inserted_urls(Connection connection) {
// Handeling not inserted in db urls
// I chose to crawl them
while(failedLinksList.size() > 0){
synchronized (Crawler.LOCK_LINKS_DB_FAIL) {
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if (enqueue_multiple_links(connection, failedLinksList)) {
linksQueue.addAll(failedLinksList);
failedLinksList.clear();
}
}
}
crawlUpdatedLinks(connection);
}
}
public void run() {
int threadNumber = Integer.valueOf(Thread.currentThread().getName());
Connection connection = Crawler.connections.get(threadNumber);
crawling(connection); // before recrawling just crawl new websites
handel_falied_inserted_urls(connection);
while(true){
recrawling(connection);
crawlUpdatedLinks(connection); // After recrawling, this function crawls new fetched urls in updated websites
handel_falied_inserted_urls(connection);
}
}
public static void main(String[] args) {
Crawler.formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
boolean isRecrawling = false;
try {
String query = "SELECT * FROM page;";
Connection connection = dbManager.getDBConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(query);
System.out.println(result);
while (result.next()) {
String url = result.getString("url");
String crawled_time = result.getString("crawled_time");
int id = result.getInt("id");
java.util.Date crawledDate = Crawler.formatter.parse(crawled_time);
Crawler.visitedLinks.put(url, new UrlInDB(crawledDate, id));
}
result.close();
statement.close();
connection.close();
// check if in recrawling mode first // if yes don't load queue list
isRecrawling = (Crawler.visitedLinks.size() >= Crawler.MAX_WEBSITES);
if(!isRecrawling) {
query = "SELECT * FROM state;";
connection = dbManager.getDBConnection();
statement = connection.createStatement();
result = statement.executeQuery(query);
System.out.println(result);
while (result.next()) {
String url = result.getString("url");
Crawler.linksQueue.add(url);
}
result.close();
statement.close();
connection.close();
if (Crawler.linksQueue.isEmpty()) {
System.out.println("QUEUE EMPTY!!");
File seedSetFile = new File(Crawler.seedSetFileName);
Scanner seedSetScanner = new Scanner(seedSetFile);
while (seedSetScanner.hasNextLine()) {
Crawler.linksQueue.add(seedSetScanner.nextLine());
}
}
}
} catch (FileNotFoundException e) {
try {
System.out.println("Can not open Queue file, QUEUE EMPTY!!, Loading Seed Set");
if (Crawler.linksQueue.isEmpty()) {
System.out.println("QUEUE EMPTY!!");
File seedSetFile = new File(Crawler.seedSetFileName);
Scanner seedSetScanner = new Scanner(seedSetFile);
while (seedSetScanner.hasNextLine()) {
Crawler.linksQueue.add(seedSetScanner.nextLine());
}
}
} catch (FileNotFoundException ex) {
System.out.println("Can not open seed seed file, exit program ... ");
System.exit(-1);
}
} catch (SQLException e) {
System.out.println("Can not connect to Data base or parsing Date error ... ");
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
int counter = 0;
List<Thread> threads = new ArrayList<>();
Thread t;
while (Crawler.numThreads > counter) {
t = new Thread(new Crawler());
t.setName(String.valueOf(counter++));
threads.add(t);
Connection connection = dbManager.getDBConnection();
Crawler.connections.add(connection);
System.out.println("Thread " + t.getName() + " is created");
}
// start threads
for (final Thread thread : threads) {
int count = 0;
synchronized (Crawler.LOCK_LINKS_QUEUE) {
if(!isRecrawling) {
count = Crawler.linksQueue.size();
if (count >= 1) {
thread.start();
}
} else {
thread.start();
}
}
try {
if (count <= 1) {
TimeUnit.SECONDS.sleep(5);
}
} catch (InterruptedException e) {
System.out.println(
" ----------------- Error interupted while sleeping ----------------- ");
}
}
// join threads
for (final Thread thread : threads) {
try {
thread.join();
int threadNumber = Integer.valueOf(Thread.currentThread().getName());
Connection connection = Crawler.connections.get(threadNumber);
connection.close();
} catch (InterruptedException | SQLException e) {
System.out.println(
" ----------------- Error Thread has been interupted ----------------- ");
}
}
}
}