-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawler.java
168 lines (149 loc) · 4.14 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.StringTokenizer;
public class crawler {
public static final String DISALLOW = "Disallow: ";
public static boolean robotSafe(URL url) {
String strHost = url.getHost();
// form URL of the robots.txt file
String strRobot = "http://" + strHost + "/robots.txt";
URL urlRobot;
try {
urlRobot = new URL(strRobot);
} catch (MalformedURLException e) {
System.err.println("malformed");
return false;
}
String strCommands;
try {
InputStream urlRobotStream = urlRobot.openStream();
byte b[] = new byte[1000];
int numRead = urlRobotStream.read(b);
strCommands = new String(b, 0, numRead);
while (numRead != -1) {
numRead = urlRobotStream.read(b);
if (numRead != -1) {
String newCommands = new String(b, 0, numRead);
strCommands += newCommands;
}
}
urlRobotStream.close();
} catch (IOException e) {
return true;
}
System.out.println(strCommands);
String strURL = url.getFile();
int index = 0;
while ((index = strCommands.indexOf(DISALLOW, index)) != -1) {
index += DISALLOW.length();
String strPath = strCommands.substring(index);
StringTokenizer st = new StringTokenizer(strPath);
if (!st.hasMoreTokens()) {
break;
}
String strBadPath = st.nextToken();
//System.err.println(strBadPath);
if (strURL.indexOf(strBadPath) == 0) {
System.err.println(strBadPath);
System.err.println(strURL);
return false;
}
}
return true;
}
public static String getPage(URL url) {
try {
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
InputStream urlStream = url.openStream();
byte b[] = new byte[1000];
int numRead = urlStream.read(b);
String content = new String(b, 0, numRead);
while ((numRead != -1)) {
numRead = urlStream.read(b);
if (numRead != -1) {
String newContent = new String(b, 0, numRead);
content += newContent;
}
}
return content;
} catch (IOException e) {
System.err.println("ERROR: couldn't open URL ");
return "";
}
}
public static void savePage(String page, String filepath, String desPath) {
//System.out.println("FILENAME: " + filename);
String path;
int index = filepath.lastIndexOf("/");
String filename = filepath.substring(filepath.lastIndexOf("/", index - 1) + 1, index);
if (desPath.charAt(desPath.length() - 1) != '/') {
path = desPath + "/" + filename;
}
else {
path = desPath + filename;
}
File f = new File(path);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
try {
if (f.createNewFile()) {
FileWriter filewriter = new FileWriter(f);
filewriter.write(page);
filewriter.flush();
filewriter.close();
}
} catch (IOException e) {
System.err.println("ERROR: fail to save page ");
return;
}
}
public static void main(String args[]) {
if (args.length != 2) {
System.err.println("crawler takes 2 argument: url list and destination path");
System.exit(1);
}
String urlFileName = args[0];
File urlFile = new File(urlFileName);
List<String> urls = null;
try {
urls = Files.readAllLines(urlFile.toPath(), StandardCharsets.UTF_8);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
for (int i = 0; i < urls.size(); i++) {
URL url = null;
try {
url = new URL(urls.get(i).trim());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//if (robotSafe(url)) {
String page = getPage(url);
if (page.length() != 0) {
savePage(page, url.getFile(), args[1]);
}
//}
//else {
// System.err.println("robot exclusion");
//}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}