-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerRole.cs
More file actions
311 lines (280 loc) · 10.7 KB
/
WorkerRole.cs
File metadata and controls
311 lines (280 loc) · 10.7 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
//Kellan Nealy, #1331068, info 344 PA3
//This class scrapes pages recursively from the XML sitemap,
//and adds them to the Azure cloud table
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using System.Xml;
using System.Text.RegularExpressions;
namespace WebRole1
{
public class WorkerRole : RoleEntryPoint
{
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
private static CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
private static CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
//queues
private CloudQueue admin = queueClient.GetQueueReference("admin");
private CloudQueue urls = queueClient.GetQueueReference("urls");
//table
private static CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
private static CloudTable table = tableClient.GetTableReference("pages");
private static CloudTable statstable = tableClient.GetTableReference("stats");
//other useful state
private string crawlerState = "idle";
private List<string> disallows = new List<string>();
private HashSet<string> urlList = new HashSet<string>();
private List<string> last10 = new List<string>(10);
private List<string> errors = new List<string>();
private int tablesize = 0;
//Run Method
public override void Run()
{
while (true)
{
string adminMessage = getMessage(admin);
if (adminMessage != null)
{
if (adminMessage.Equals("start"))
{
if (last10.Count > 0)
{
this.crawlerState = "crawling";
}
else
{
//set loading state, crawl robots.txt, begin crawl
this.crawlerState = "loading";
}
}
else if (adminMessage.Equals("stop"))
{
//set idle state, stop crawling
this.crawlerState = "idle";
urls.Clear();
}
}
updateStats();
if (this.crawlerState.Equals("loading"))
{
crawlRobotsXML("http://www.cnn.com/robots.txt", urls, false);
crawlRobotsXML("http://www.bleacherreport.com/robots.txt", urls, true);
this.crawlerState = "crawling";
}
else if (this.crawlerState.Equals("crawling"))
{
crawlURLS();
}
}
}
//updates the stats of this Crawler
private void updateStats()
{
try
{
Stat mystats = new Stat(tablesize, crawlerState, string.Join(" | ", last10.ToArray()), string.Join(" | ", errors.ToArray()));
TableOperation insertOp = TableOperation.InsertOrReplace(mystats);
statstable.Execute(insertOp);
}
catch { }
}
//my own overload to getMessage to take advantage of null return
private string getMessage(CloudQueue queue)
{
try
{
CloudQueueMessage message = queue.GetMessage();
string messageString = message.AsString;
queue.DeleteMessage(message);
return messageString;
} catch {
return null;
}
}
//iterates through robots.txt URL and calls helper to recurse
private void crawlRobotsXML(string url, CloudQueue urls, Boolean isBleacherReport)
{
//go through robots.txt and store disallowed URLS
string[] lines = getPageContent(url);
for (int i = 0; i < lines.Length; i++)
{
string[] data = lines[i].Split(' ');
if (data[0].Equals("Sitemap:"))
{
crawlXMLHelper(data[1], isBleacherReport);
}
else if (data[0].Equals("Disallow:"))
{
disallows.Add(data[1]);
}
}
}
//recursively crawls xml sitemaps for urls
private void crawlXMLHelper(string url, Boolean isBleacherReport)
{
//try catch handles broken link errors so crawling can continue
try
{
if (url.EndsWith(".xml"))
{
XmlDocument xd = new XmlDocument();
xd.Load(url);
XmlNodeList nodelist = xd.DocumentElement.ChildNodes;
foreach (XmlNode node in nodelist)
{
if (!isBleacherReport && node.LastChild.Name.Equals("lastmod"))
{
string date = node.LastChild.InnerText;
if (date.StartsWith("2015-05") || date.StartsWith("2015-04"))
{
crawlXMLHelper(node.FirstChild.InnerText, isBleacherReport);
}
}
else
{
crawlXMLHelper(node.FirstChild.InnerText, isBleacherReport);
}
}
}
else
{
urls.AddMessage(new CloudQueueMessage(url));
}
}
catch { }
}
//recursively crawls html urls in queue
private void crawlURLS()
{
crawlHelper(getMessage(urls));
}
//helper method to recursively crawl urls
private void crawlHelper(string url)
{
if (url != null)
{
//try catch handles broken link errors so crawling can continue
try
{
//have to add this page to index before we crawl!
System.Net.WebClient wc = new System.Net.WebClient();
string content = wc.DownloadString(url);
string title = getTitle(content);
addToTable(url, title);
Regex regex = new Regex("(a href)=[\"|']?(.*?)[\"|'|>]+", RegexOptions.Singleline | RegexOptions.CultureInvariant);
if (regex.IsMatch(content))
{
foreach (Match match in regex.Matches(content))
{
string curUrl = match.Value.Substring(match.Value.IndexOf('\"') + 1);
if (isValid(curUrl))
{
urls.AddMessage(new CloudQueueMessage(url));
}
}
}
updateStats();
}
catch (Exception e) { errors.Add(url + " -- " + e.Message); }
}
}
//checks for valid HTML urls
private Boolean isValid(string url)
{
return !(url.EndsWith(".js") || url.EndsWith(".css") || url.EndsWith(".xml"))
&& (url.Contains("cnn.") || url.Contains("bleacherreport."));
}
//gets the title for an HTML page
private string getTitle(string pageContent)
{
Match m = Regex.Match(pageContent, @"<title>\s*(.+?)\s*</title>");
if (m.Success)
{
return m.Groups[1].Value;
} else
{
return null;
}
}
//checks to see if this URL is allowed
private Boolean isDisallow(string url)
{
foreach (string disallow in disallows)
{
if (url.EndsWith(disallow))
return true;
}
return false;
}
//Uses web client to download page content of passed URL as string
private string[] getPageContent(string url)
{
try
{
System.Net.WebClient wc = new System.Net.WebClient();
return wc.DownloadString(url).Split('\n');
}
catch
{
//do nothing, this is an invalid url!
}
return null;
}
//Checks for dupes before adding to index
private void addToTable(string url, string title)
{
try
{
if (!urlList.Contains(url))
{
urlList.Add(url);
WebPage page = new WebPage(url, title);
TableOperation insertOp = TableOperation.Insert(page);
table.Execute(insertOp);
tablesize++;
if (last10.Count == 10)
{
last10.RemoveAt(0);
}
last10.Add(url);
}
}
catch { }
}
//-----Other Worker Role Methods
private async Task RunAsync(CancellationToken cancellationToken)
{
await Task.Delay(50);
}
public override bool OnStart()
{
// Set the maximum number of concurrent connections
ServicePointManager.DefaultConnectionLimit = 12;
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
bool result = base.OnStart();
Trace.TraceInformation("WorkerRole1 has been started");
return result;
}
public override void OnStop()
{
Trace.TraceInformation("WorkerRole1 is stopping");
this.cancellationTokenSource.Cancel();
this.runCompleteEvent.WaitOne();
base.OnStop();
Trace.TraceInformation("WorkerRole1 has stopped");
}
}
}