-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (44 loc) · 1.33 KB
/
Main.java
File metadata and controls
53 lines (44 loc) · 1.33 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
import crawlers.ConcurrentCrawler;
import crawlers.Crawler;
import crawlers.SimpleCrawler;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
public class Main {
public static void main(String[] args) {
final String ERROR_MSG =
"USAGE: "
+ "java -jar crawler.jar {'-c'/'-s'} [Start URL] [Number of URLs to find] \n"
+ "\tUse options '-c' and '-s' to run the concurrent and sequential crawlers"
+ " respectively.";
if (args.length != 3) {
System.out.println(ERROR_MSG);
return;
}
String startUrl = args[1];
try {
new URL(startUrl);
} catch (MalformedURLException e) {
System.out.println(startUrl + " is not a valid URL.");
return;
}
int limit;
try {
limit = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
System.out.println("Limit needs to be a valid Integer.");
return;
}
Crawler crawler;
if (args[0].equals("-c")) {
crawler = new ConcurrentCrawler(limit, "ConcurrentCrawler");
} else if (args[0].equals("-s")) {
crawler = new SimpleCrawler(limit, "SequentialCrawler");
} else {
System.out.println(ERROR_MSG);
return;
}
Set<String> results = crawler.crawlFrom(startUrl);
results.forEach(System.out::println);
}
}