-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.cs
More file actions
297 lines (266 loc) · 10.4 KB
/
Crawler.cs
File metadata and controls
297 lines (266 loc) · 10.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using HAP = HtmlAgilityPack;
namespace WebCrawler
{
public class Crawler
{
private static int counter = 0;
public static string root;
public static string folder;
private static Dictionary<string, bool> isVisited = new Dictionary<string, bool>();
private static Queue<crawlElement> bfsQueue = new Queue<crawlElement>();
private static HashSet<string> ignoredExtension = new HashSet<string>();
public static List<crawlElement> crawlResult;
public class crawlElement
{
public string url = null;
public string fileName=null;
public string pageTitle = null;
public string content = null;
public List<bool> haveKey;
public crawlElement() {}
public crawlElement(string url) { this.url = url; }
public crawlElement(string url, string fileName) { this.url = url; this.fileName = fileName; }
public void initListBool(int n) {
haveKey = new List<bool>();
for (int i = 0; i < n; i++)
{
haveKey.Add(false);
}
}
}
public static void allocateFile(ref crawlElement c)
{
string filename = (++Crawler.counter) + ".html";
c.fileName = filename;
}
/**
* menggenerate semua link child P
* P : (parent) link awal (format http://...)
* C : (child) daftar link akhir
* N : jumlah node yang diexpand
**/
public static void expandLink(ref crawlElement P,ref List<crawlElement> C)
{
try
{
var doc = new HAP.HtmlDocument();
doc.Load(Configuration.getSavedPagesLocation() + Crawler.folder + "/" + P.fileName);
var a_nodes = getAllTag(doc, "a");
C.Clear();
foreach (var a_node in a_nodes)
{
string page = getLink(P.url, a_node.GetAttributeValue("href", ""));
//Console.WriteLine("Here" + a_node.InnerText.ToString());
C.Add(new crawlElement(page));
}
}
catch (Exception)
{
}
}
/**
* Menghasilkan semua node (isi tag) dengan kode tag
* doc : document yang dicari
* tag : nilai tag
* doc harus sudah di-load
*/
public static List<HAP.HtmlNode> getAllTag(HAP.HtmlDocument doc,string tag)
{
List<HAP.HtmlNode> retval = doc.DocumentNode.Descendants(tag).ToList();
return retval;
}
public static void clearData()
{
Crawler.counter = 0;
Crawler.isVisited.Clear();
Crawler.bfsQueue.Clear();
Crawler.crawlResult = new List<crawlElement>();
}
public static void runCrawler(string mode,int depth)
{
clearData();
crawlElement first = new crawlElement(Crawler.root);
if (mode == "DFS")
{
DFS(first, depth);
}
else
{
bfsQueue.Enqueue(first);
BFS(depth);
}
}
public static bool isValidLink(crawlElement ce)
{
bool retVal = (!isFile(ce.url)) && ce.url.StartsWith(Crawler.root);
return retVal;
}
public static void DFS(crawlElement parent,int depth)
{
if (!isValidLink(parent)) return;
if (saveContent(ref parent)) Crawler.crawlResult.Add(parent);
Console.WriteLine(parent.url + "\nsaved in : " + parent.fileName);
List<crawlElement> childs = new List<crawlElement>();
if (depth > 1){
expandLink(ref parent, ref childs);
foreach (var child in childs)
{
if (!isVisited.ContainsKey(child.url))
{
isVisited[child.url] = true;
DFS(child, depth - 1);
}
}
}
}
public static void BFS(int depth)
{
Queue<crawlElement> tempQueue = new Queue<crawlElement>();
while (bfsQueue.Count > 0)
{
crawlElement parent = bfsQueue.Dequeue();
if (!isValidLink(parent)) continue;
if (saveContent(ref parent)) Crawler.crawlResult.Add(parent);
Console.WriteLine(parent.url + "\n at depth = " + depth +" saved in : " + parent.fileName);
List<crawlElement> childs = new List<crawlElement>();
if (depth == 0) return; else expandLink(ref parent, ref childs);
foreach (var child in childs)
{
//Console.WriteLine("continue");
if (!isVisited.ContainsKey(child.url))
{
isVisited[child.url] = true;
tempQueue.Enqueue(child);
}
}
}
while (tempQueue.Count > 0)
{
bfsQueue.Enqueue(tempQueue.Dequeue());
}
if (depth > 1)
BFS(depth - 1);
}
/**
* menghasilkan link absolute berdasarkan parent dan link sekarang
* jika link sekarang bukan anggota dari link parent maka return link sekarang
*/
public static string getLink(string parent, string relative)
{
if (relative.StartsWith("http:"))
return relative;
Uri baseUri = new Uri(parent, UriKind.Absolute);
Uri u = new Uri(baseUri, relative);
return u.ToString();
}
/**
* jika status = false, maka link invalid
*/
public static bool saveContent(ref crawlElement ce)
{
using (var client = new System.Net.WebClient())
{
allocateFile(ref ce);
try
{
client.DownloadFile(ce.url, Configuration.getSavedPagesLocation() + Crawler.folder + "/" + ce.fileName);
HAP.HtmlDocument doc = new HAP.HtmlDocument();
doc.Load(Configuration.getSavedPagesLocation() + Crawler.folder + "/" + ce.fileName);
List<HAP.HtmlNode> t_nodes = getAllTag(doc, "title");
if (t_nodes.Count == 0) t_nodes = getAllTag(doc, "h1");
if (t_nodes.Count == 0) t_nodes = getAllTag(doc, "h2");
if (t_nodes.Count == 0) t_nodes = getAllTag(doc, "h3");
ce.pageTitle = "-Untitled Pages-";
foreach (var t_node in t_nodes)
{
ce.pageTitle = t_node.InnerHtml.ToString();
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
}
public static void initIgnoredExtension()
{
System.IO.StreamReader file = new System.IO.StreamReader(Configuration.getIgnoredExtensionLocation());
string line;
while ((line = file.ReadLine()) != null)
{
ignoredExtension.Add(line);
}
file.Close();
}
public static bool isFile(string s)
{
s = s.ToUpper();
foreach (var ext in ignoredExtension)
{
string extension = ("." + ext).Trim();
if (s.Trim().EndsWith(extension))
return true;
}
return false;
}
public static void doCrawler(string s,string mode,string savefoldername,int depth)
{
Crawler.root = s;
Crawler.folder = savefoldername;
Crawler.createDirectory(Configuration.getSavedPagesLocation() + savefoldername);
Crawler.runCrawler(mode, depth);
}
public static void createDirectory(string s)
{
if (!Directory.Exists(s))
{
Directory.CreateDirectory(s);
}
}
public static void saveToIndex()
{
string saveFolderName = Configuration.getIndexLocation() + Crawler.folder +"/";
if (!Directory.Exists(saveFolderName))
{
Directory.CreateDirectory(saveFolderName);
}
foreach (var ce in Crawler.crawlResult)
{
IndexElement ie = new IndexElement(ce.url, ce.fileName);
ie.createWords();
System.IO.StreamWriter printer = new System.IO.StreamWriter(ie.indexFileLocation);
foreach (var word in ie.words)
{
printer.WriteLine(word);
}
printer.Close();
Console.WriteLine("saved to " + ie.indexFileLocation);
}
}
public static string getAbsolutePath(string relative)
{
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return Path.Combine(dir, relative);
}
public static void saveResultToFile()
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Configuration.getCrawlResultLocation() + Crawler.folder + ".result");
foreach (var celmt in Crawler.crawlResult)
{
Console.WriteLine(celmt.url);
celmt.url = celmt.url.Replace("\n", ""); sw.WriteLine(celmt.url);
celmt.fileName = celmt.fileName.Replace("\n", ""); sw.WriteLine(celmt.fileName);
celmt.pageTitle = celmt.pageTitle.Replace("\n", ""); sw.WriteLine(celmt.pageTitle);
}
sw.Close();
}
}
}