-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawl.cs
More file actions
400 lines (343 loc) · 15.1 KB
/
Crawl.cs
File metadata and controls
400 lines (343 loc) · 15.1 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
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
using HtmlAgilityPack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace crawler
{
internal class Crawl
{
public static string visitlist = string.Empty;
public static string resultsjson = string.Empty;
private static readonly string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
public static Result result(string url)
{
// Get the Results JSON
string resultspath = Path.Combine(BaseDirectory, "results.json");
if (File.Exists(resultspath))
resultsjson = File.ReadAllText(resultspath);
// Get the Visitlist
string visitpath = Path.Combine(BaseDirectory, "visitlist.json");
if (File.Exists(visitpath))
visitlist = File.ReadAllText(visitpath);
Result result = new Result();
try
{
// Rank
double rank = 0;
WebClient webClient = new WebClient();
webClient.Headers["UserAgent"] = "ArtadoBot/1.0";
string htmlContent = webClient.DownloadString(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
// Extract title
result.Title = doc.DocumentNode.SelectSingleNode("//title")?.InnerText;
Console.WriteLine(result.Title);
// URL
result.URL = url;
Console.WriteLine(result.URL);
// Extract meta description
HtmlNode? metaDescriptionNode = doc.DocumentNode.SelectSingleNode("//meta[@name='description']");
if (metaDescriptionNode != null)
{
result.Description = metaDescriptionNode.GetAttributeValue("content", "");
rank++;
Console.WriteLine(result.Description);
}
else
{
HtmlNode h1 = doc.DocumentNode.Descendants("h1").FirstOrDefault();
if (h1 != null)
{
result.Description = h1.InnerText;
}
else
{
HtmlNode firstElementInBody = doc.DocumentNode.Descendants("body").FirstOrDefault().Descendants().FirstOrDefault();
// Remove extra spaces
string trimmed = System.Text.RegularExpressions.Regex.Replace(firstElementInBody.InnerText.Trim(), @"\s+", " ");
// Take only the first 100 characters
string truncated = trimmed.Length > 100 ? trimmed.Substring(0, 100) : trimmed;
result.Description = truncated.Trim();
}
Console.WriteLine(result.Description);
}
// Extract meta keywords
HtmlNode? metaKeywordsNode = doc.DocumentNode.SelectSingleNode("//meta[@name='keywords']");
if (metaKeywordsNode != null)
{
result.Keywords = metaKeywordsNode.GetAttributeValue("content", "").Split();
rank++;
Console.WriteLine(result.Keywords);
}
// Extract lang
HtmlNode? langNode = doc.DocumentNode.SelectSingleNode("//html");
if (langNode != null)
{
result.Lang = langNode.GetAttributeValue("lang", "");
if (result.Lang != null)
rank++;
Console.WriteLine(result.Lang);
}
// Mobile Support
HtmlNode? mobileNode = doc.DocumentNode.SelectSingleNode("//meta[@name='viewport']");
if (mobileNode != null)
{
rank++;
}
// Homepage detection
if (IsMainDirectory(url))
{
rank++;
}
// Prioritize the wikipedia results
int wiki = url.IndexOf("wikipedia.org");
if(wiki >= 0)
{
rank++;
}
// Find all <a> tags
List<WebsiteLink> links = new List<WebsiteLink>();
Console.WriteLine("Getting the a links");
try
{
string jsonFilePath = Path.Combine(BaseDirectory, "visitlist.json");
// Load existing results from JSON file once
if (File.Exists(jsonFilePath))
{
string jsonContent = File.ReadAllText(jsonFilePath);
links = JsonConvert.DeserializeObject<List<WebsiteLink>>(jsonContent);
}
var linkNodes = doc.DocumentNode.SelectNodes("//a[@href]")?.ToList();
if (linkNodes != null)
{
Parallel.ForEach(linkNodes, linkNode =>
{
string href = linkNode.GetAttributeValue("href", "");
string linkUrl = new Uri(new Uri(url), href).AbsoluteUri;
int permalink = linkUrl.IndexOf("#");
if (!IsUrlInVisitList(linkUrl, visitlist) && !IsUrlInLocal(linkUrl, resultsjson) && permalink < 0 && IsURL(linkUrl) && url != linkUrl)
{
// Initialize links as not visited
links.Add(new WebsiteLink { Url = linkUrl, Visited = false });
Console.WriteLine("Link saved:" + linkUrl);
}
else
{
Console.WriteLine(linkUrl);
Console.WriteLine("Link already saved");
}
});
}
// Save all links back to the JSON file once
string newContent = JsonConvert.SerializeObject(links, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(jsonFilePath, newContent);
}
catch(Exception ex)
{
Console.WriteLine("No a tags in this site. Error: " + ex);
}
// Final rank calculation
Console.WriteLine("Rank: " + rank);
result.Rank = rank;
SaveWebsiteInfoToJson(result);
// Index in ElasticSearch
IndexWebsiteInElasticSearch(result);
}
catch (WebException ex)
{
Console.WriteLine("Error downloading website content: " + ex.Message);
}
return result;
}
private static readonly Regex UrlRegex = new Regex(@"^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
static bool IsURL(string input)
{
return UrlRegex.IsMatch(input);
}
static bool IsMainDirectory(string url)
{
// Parse the URL
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
// Check if the path component is empty or "/"
return string.IsNullOrEmpty(uri.AbsolutePath) || uri.AbsolutePath == "/";
}
// If the URL is invalid or cannot be parsed, return false
return false;
}
static void SaveWebsiteInfoToJson(Result websiteInfo)
{
string jsonFilePath = Path.Combine(BaseDirectory, "results.json");
List<Result> results = new List<Result>();
// Load existing results from JSON file
if (File.Exists(jsonFilePath))
{
string jsonContent = File.ReadAllText(jsonFilePath);
results = JsonConvert.DeserializeObject<List<Result>>(jsonContent);
}
// Add the new websiteInfo to the results list
results.Add(websiteInfo);
// Serialize and save the updated results to the JSON file
string updatedJsonContent = JsonConvert.SerializeObject(results, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(jsonFilePath, updatedJsonContent);
}
public static int GetResultCountFromJson()
{
string jsonFilePath = Path.Combine(BaseDirectory, "results.json");
if (File.Exists(jsonFilePath))
{
string jsonContent = File.ReadAllText(jsonFilePath);
List<Result> results = JsonConvert.DeserializeObject<List<Result>>(jsonContent);
return results.Count;
}
return 0;
}
// ElasticSearch Methods
public static async Task ImportResultsToElasticSearch()
{
string jsonFilePath = Path.Combine(BaseDirectory, "results.json");
try
{
// Read JSON file
string jsonContent = File.ReadAllText(jsonFilePath);
List<Result> results = JsonConvert.DeserializeObject<List<Result>>(jsonContent);
// Process each result and index it in ElasticSearch
foreach (Result websiteInfo in results)
{
await IndexWebsiteInElasticSearch(websiteInfo);
}
Console.WriteLine("Results imported to ElasticSearch.");
// Optionally delete the file after successful import
File.Delete(jsonFilePath);
}
catch (Exception ex)
{
Console.WriteLine("Error importing results to ElasticSearch: " + ex.Message);
}
}
private static async Task IndexWebsiteInElasticSearch(Result result)
{
try
{
// Convert the result to JSON
string jsonData = JsonConvert.SerializeObject(result);
// ElasticSearch endpoint - should be configurable
string elasticSearchUrl = Config.ElasticSearchUrl;
string indexUrl = $"{elasticSearchUrl}/artadosearch/_doc";
// Check if document exists to determine update or insert
bool exists = await CheckIfDocumentExists(elasticSearchUrl, result.URL);
// Use appropriate URL for update or insert
string finalUrl = exists
? $"{indexUrl}/{WebUtility.UrlEncode(result.URL)}/_update"
: $"{indexUrl}/{WebUtility.UrlEncode(result.URL)}";
// Format data for update if needed
string requestData = exists
? $"{{\"doc\":{jsonData}}}"
: jsonData;
// Use WebClient to send data to ElasticSearch
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
if (exists)
{
// For update
string response = client.UploadString(finalUrl, "POST", requestData);
Console.WriteLine($"Updated document in ElasticSearch: {result.URL}");
}
else
{
// For insert
string response = client.UploadString(finalUrl, "PUT", requestData);
Console.WriteLine($"Indexed new document in ElasticSearch: {result.URL}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error indexing document in ElasticSearch: {ex.Message}");
}
}
private static async Task<bool> CheckIfDocumentExists(string elasticSearchBaseUrl, string url)
{
try
{
string encodedUrl = WebUtility.UrlEncode(url);
string requestUrl = $"{elasticSearchBaseUrl}/artadosearch/_doc/{encodedUrl}";
WebClient client = new WebClient();
try
{
string response = client.DownloadString(requestUrl);
// Parse the response to check if document exists
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
return jsonResponse.found == true;
}
catch (WebException ex)
{
// 404 indicates document doesn't exist
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
{
return false;
}
throw;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error checking document existence: {ex.Message}");
return false;
}
}
public static bool IsUrlInElasticSearch(string url)
{
try
{
string encodedUrl = WebUtility.UrlEncode(url);
string elasticSearchUrl = Config.ElasticSearchUrl;
string requestUrl = $"{elasticSearchUrl}/artadosearch/_doc/{encodedUrl}";
WebClient client = new WebClient();
try
{
string response = client.DownloadString(requestUrl);
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
return jsonResponse.found == true;
}
catch (WebException ex)
{
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound)
{
return false;
}
throw;
}
}
catch (Exception)
{
return false;
}
}
public static bool IsUrlInVisitList(string url, string jsonContent)
{
if (!string.IsNullOrEmpty(jsonContent))
{
List<WebsiteLink> links = JsonConvert.DeserializeObject<List<WebsiteLink>>(jsonContent);
return links != null && links.Exists(link => link.Url == url);
}
return false;
}
public static bool IsUrlInLocal(string url, string jsonContent)
{
if (!string.IsNullOrEmpty(jsonContent))
{
List<Result> results = JsonConvert.DeserializeObject<List<Result>>(jsonContent);
return results != null && results.Exists(result => result.URL == url);
}
return false;
}
}
}