-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoesCrawler.cs
600 lines (512 loc) · 25.8 KB
/
GoesCrawler.cs
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Net.Mail;
using System.Net;
using System.Data.Common;
using System.Threading;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Collections.ObjectModel;
namespace GOES_Application
{
public static class GoesCrawler
{
// Server Application entry point
public static void AppCrawlSite(Object pObj)
{
Int32 pUser_id = (Int32)pObj;
LogStatus(pUser_id, "Started processing for " + pUser_id.ToString());
// lblLastCrawlTime.Text = DateTime.Now.ToShortTimeString();
GoesUser goes_user = new GoesUser();
if (goes_user.GetDataForUser(pUser_id) == true)
{
goes_user.mAppType = GoesUser.AppType.Server;
CrawlSite(goes_user);
}
else
{
LogStatus(pUser_id, "The Userprofile is not complete. Check the profile page and/or https://www.globalentryinterview.com/interview-as-a-service/", "");
RaiseCrawlComplete(null);
return;
}
}
// Desktop Application entry point
public static void DesktopCrawlSite(Object pObj)
{
GoesUser goes_user = (GoesUser)pObj;
goes_user.mAppType = GoesUser.AppType.Desktop;
CrawlSite(goes_user);
}
// Main thread for the site crawl
// It's a monolithic function but follows a simple pattern
// each step tries to find an HTML object and throws an exception when the object is not found
public static void CrawlSite(GoesUser pUser)
{
using (IWebDriver driver = new FirefoxDriver())
{
if (pUser.mAppType == GoesUser.AppType.Server)
LogStatus(pUser.mUser_id, "App Driver opened for " + pUser.mUsername);
// Start the Browser
driver.Navigate().GoToUrl("https://goes-app.cbp.dhs.gov/goes/");
// Find the username object.
// When this fails, the site didn't load. Most likely because the computer is offline.
try
{
driver.FindElement(By.Id("j_username")).SendKeys(pUser.mUsername);
}
catch (Exception ex)
{
if (pUser.mAppType == GoesUser.AppType.Server)
LogStatus(pUser.mUser_id, "The GOES website didn't load as expected. Check your internet connection and retry.", ex.Message);
else
ShowMessage("The GOES website didn't load as expected. Check your internet connection and retry.");
RaiseCrawlComplete(null);
return;
}
// When we find the username object, we can then populate the other fields.
driver.FindElement(By.Id("j_password")).Clear();
driver.FindElement(By.Id("j_password")).SendKeys(pUser.mPassword);
driver.FindElement(By.Id("SignIn")).Click();
// Get past the "I'm a Human" checkbox thingy
try
{
driver.FindElement(By.Id("checkMe")).Click();
} catch (Exception ex)
{
String strErrorMessage = "GOES Username or password is not valid.";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Click the Manage Appointment button
// Get past the "I'm a Human" thingy
try
{
driver.FindElement(By.Name("manageAptm")).Click();
}
catch (Exception ex)
{
String strErrorMessage = "Manage Appointment button is not found. Has your application been approved?";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
DateTime dateInterview;
String CurrentInterview;
// Capture my current Date of the interview
try
{
IWebElement elem = driver.FindElement(By.XPath("//*[contains(.,'Interview Date:')]"));
if (elem == null)
{
String strErrorMessage = "The current inteview date is not found. Has your application been approved?";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, "None");
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
int pos = elem.Text.IndexOf("Interview Date:");
CurrentInterview = elem.Text.Substring(pos + 16, 12);
dateInterview = Convert.ToDateTime(CurrentInterview);
}
catch (Exception ex)
{
String strErrorMessage = "The current inteview date is not found. Has your application been approved?";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Check for interview date in the past. This happens when someone misses their appointment.
if (DateTime.Compare(dateInterview, DateTime.Now) < 0)
{
String strErrorMessage = "Your current inteview date is in the past. You need to manually schedule your interview the first time.";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage);
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Click the Reschedule button
try
{
driver.FindElement(By.Name("reschedule")).Click();
}
catch (Exception ex)
{
String strErrorMessage = "Reschedule button is not found. Has your application been approved?";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendErrorEmail(strErrorMessage, pUser.mEmail);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Pick the proper location
try
{
bool bLocationFound = false;
// new SelectElement(driver.FindElement(By.Id("selectedEnrollmentCenter"))).SelectByText(pUser.mLocation_Name);
ReadOnlyCollection<IWebElement> we = driver.FindElements(By.Name("selectedEnrollmentCenter"));
foreach (var title in we)
{
String text = title.GetAttribute("title");
if (text.Trim().CompareTo(pUser.mLocation_Name.Trim()) == 0)
{
title.Click();
bLocationFound = true;
break;
}
}
// new SelectElement(driver.FindElement(By.Name("selectedEnrollmentCenter"))).SelectByText(pUser.mLocation_Name);
// String strPath = "//*[@title='" + pUser.mLocation_Name + "']";
// IWebElement we = driver.FindElement(By.XPath(strPath));
// new SelectElement(we);
if (bLocationFound)
driver.FindElement(By.Name("next")).Click();
else
{
String strErrorMessage = "Can''t find your selected Enrollment Center. Usually means the site had an error.";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage);
SendSupportEmail(strErrorMessage);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
}
catch (Exception ex)
{
String strErrorMessage = "Can''t find your selected Enrollment Center. Usually means the site had an error.";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendSupportEmail(strErrorMessage);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Find and parse the current next available date
DateTime dateAvailable;
try
{
String CurrentDate = driver.FindElement(By.ClassName("header")).Text;
String[] stringDateParts = CurrentDate.Split('\r');
String stringModifiedDate = stringDateParts[0] + " " + stringDateParts[2].Substring(1);
dateAvailable = Convert.ToDateTime(stringModifiedDate);
}
catch (Exception ex)
{
String strErrorMessage = "Cant find header with current interview date. Check the site for changes.";
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, strErrorMessage, ex.Message);
SendSupportEmail(strErrorMessage);
}
else
ShowMessage(strErrorMessage);
RaiseCrawlComplete(null);
return;
}
// Now compare the dates and book the new if earlier than current interview date
if (DateTime.Compare(dateAvailable, dateInterview) < 0)
{
// This block schedules the new appointment
driver.FindElement(By.ClassName("entry")).Click();
driver.FindElement(By.Id("comments")).Clear();
driver.FindElement(By.Id("comments")).SendKeys("Looking for an earlier interview");
driver.FindElement(By.Name("Confirm")).Click();
// Find the New Interview Time
Int32 intTimePos = driver.PageSource.IndexOf("New Interview Time:");
String strNewTime = driver.PageSource.Substring(intTimePos + 20, 8);
SendEmail(dateAvailable.ToShortDateString(), strNewTime, pUser.mEmail);
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, dateAvailable, "Congratulations! We found an earlier date. The process has stopped, please visit the site if you want to continue searching for an earlier date.");
SetUserStatus(pUser.mUser_id, 3);
}
else
{
ShowMessage("Congratulations! We found an earlier date. The process has stopped, please visit the site if you want to continue searching for an earlier date.");
}
}
else
{
if (pUser.mAppType == GoesUser.AppType.Server)
{
LogStatus(pUser.mUser_id, dateAvailable, "Searched the site. Earliest date is " + dateAvailable.ToShortDateString());
SetUserStatus(pUser.mUser_id, 1);
}
GoesEventArgs ge = new GoesEventArgs();
ge.GoesResult = dateAvailable.ToShortDateString();
RaiseCrawlComplete(ge);
return;
}
return;
}
}
// Displays an error message
static void ShowMessage(String pMsg)
{
MessageBox.Show(pMsg, "GOES Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// When a crawl completes, this notifies the UI
public static event CrawlCompleteEventHandler CrawlComplete;
private static void RaiseCrawlComplete(GoesEventArgs e)
{
if (CrawlComplete != null)
CrawlComplete(null, e);
}
// The server application log is in the database, there are three implementations of this function
private static Boolean LogStatus(int pUser_id, DateTime pDateAvailable, String pStatus)
{
MySqlConnection connMySQL;
string myConnectionString;
myConnectionString = DecryptSetting(Properties.Settings.Default.MySQL);
try
{
connMySQL = new MySqlConnection();
connMySQL.ConnectionString = myConnectionString;
connMySQL.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
ShowMessage(ex.Message);
return false;
}
// SQL Statement to log the message
String strSQL = "INSERT INTO goes_log (user_id, last_date_found, last_run_date, thread_id, server_id, status) ";
strSQL += "VALUES ('" + pUser_id.ToString() + "', '" + pDateAvailable.ToString("yyyy-MM-dd HH:mm:ss") + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '" + Thread.CurrentThread.ManagedThreadId + "', '" + System.Environment.MachineName + "', '" + pStatus + "')";
// Execute the SQL
MySqlCommand cmd = connMySQL.CreateCommand();
cmd.CommandText = strSQL;
cmd.ExecuteNonQuery();
return true;
}
// The server application log is in the database, there are three implementations of this function
private static Boolean LogStatus(int pUser_id, String pStatus)
{
MySqlConnection connMySQL;
string myConnectionString;
myConnectionString = DecryptSetting(Properties.Settings.Default.MySQL);
try
{
connMySQL = new MySqlConnection();
connMySQL.ConnectionString = myConnectionString;
connMySQL.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
ShowMessage(ex.Message);
return false;
}
// SQL Statement to retrieve the Order Header
String strSQL = "INSERT INTO goes_log (user_id, last_run_date, thread_id, server_id, status) ";
strSQL += "VALUES ('" + pUser_id.ToString() + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '" + Thread.CurrentThread.ManagedThreadId + "', '" + System.Environment.MachineName + "', '" + pStatus + "')";
// Execute the SQL
MySqlCommand cmd = connMySQL.CreateCommand();
cmd.CommandText = strSQL;
cmd.ExecuteNonQuery();
return true;
}
// The server application log is in the database, there are three implementations of this function
private static Boolean LogStatus(int pUser_id, String pStatus, String pException)
{
MySqlConnection connMySQL;
string myConnectionString;
myConnectionString = DecryptSetting(Properties.Settings.Default.MySQL);
try
{
connMySQL = new MySqlConnection();
connMySQL.ConnectionString = myConnectionString;
connMySQL.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
ShowMessage(ex.Message);
return false;
}
// SQL Statement to Update the Log File
String strSQL = "INSERT INTO goes_log (user_id, last_run_date, thread_id, server_id, status, exception) ";
strSQL += "VALUES ('" + pUser_id.ToString() + "', '" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "', '" + Thread.CurrentThread.ManagedThreadId + "', '" + System.Environment.MachineName + "', '" + pStatus + "', '" + pException + "')";
// Execute the SQL
MySqlCommand cmd = connMySQL.CreateCommand();
cmd.CommandText = strSQL;
cmd.ExecuteNonQuery();
SetUserStatus(pUser_id, 2);
return true;
}
// When the server completes a crawl, we have to track the results as "UserStatus"
// This function updates the status in the database
private static Boolean SetUserStatus(int pUser_id, int pStatus)
{
MySqlConnection connMySQL;
string myConnectionString;
myConnectionString = DecryptSetting(Properties.Settings.Default.MySQL);
try
{
connMySQL = new MySqlConnection();
connMySQL.ConnectionString = myConnectionString;
connMySQL.Open();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
ShowMessage(ex.Message);
return false;
}
// Find if the status row exists
String strSQL = "SELECT count(umeta_id) FROM wp_xleb_usermeta WHERE meta_key = 'goes_status' and user_id = " + pUser_id;
// Execute the SQL
MySqlCommand cmd = connMySQL.CreateCommand();
cmd.CommandText = strSQL;
long statusCount = (long)cmd.ExecuteScalar();
if (statusCount < 1)
{
strSQL = "INSERT INTO wp_xleb_usermeta (user_id, meta_key, meta_value) VALUES (" + pUser_id + ", 'goes_status', '" + pStatus +"');";
}
else
{
// SQL Statement to Change User Status
strSQL = "UPDATE wp_xleb_usermeta SET meta_value = '" + pStatus + "' WHERE meta_key = 'goes_status' and user_id = " + pUser_id;
}
// Execute the SQL
cmd.CommandText = strSQL;
cmd.ExecuteNonQuery();
return true;
}
public static String DecryptSetting(String pValue)
{
// Create a new TripleDESCryptoServiceProvider object
// to generate a key and initialization vector (IV).
TrippleDESCSPCoder coder = new TrippleDESCSPCoder();
byte[] mEncryptedData = System.Convert.FromBase64String(pValue);
String myString = TrippleDESCSPCoder.DecryptTextFromMemory(mEncryptedData, coder.mEncryptKey, coder.mEncryptVector);
return myString;
}
// When we get a hit, we need to notify the user.
// This function sends mail to the user.
public static Boolean SendEmail(String pDate, String pTime, String pAddress)
{
MailMessage mailMsg = new MailMessage();
//Setting From , To and CC
mailMsg.From = new MailAddress(Properties.Settings.Default.MailFromAddress, Properties.Settings.Default.MailFromName);
mailMsg.To.Add(new MailAddress(pAddress));
mailMsg.Bcc.Add(new MailAddress("goes@globalentryinterview.com"));
mailMsg.Subject = Properties.Settings.Default.MailSubject;
mailMsg.Body = Properties.Settings.Default.MailHeader;
String msg = Properties.Settings.Default.MailBodyHit.Replace("<==here==>", pDate);
// mailMsg.Body += msg.Replace("<==date==>", pDate + " at " + pTime);
mailMsg.Body += msg.Replace("<==date==>", pDate);
mailMsg.Body += Properties.Settings.Default.MailFooter;
mailMsg.IsBodyHtml = true;
//gmailClient.Send(mail);
using (SmtpClient smtpClient = new SmtpClient(Properties.Settings.Default.MailSMTPServer, Properties.Settings.Default.MailPort))
{
smtpClient.EnableSsl = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(Properties.Settings.Default.MailUsername, DecryptSetting(Properties.Settings.Default.MailPassword));
try
{
smtpClient.Send(mailMsg);
}
catch(Exception ex)
{
SendErrorEmail("Error Sending Email", "bob@leroynet.com");
}
}
return true;
}
// When we get a hit, we need to notify the user.
// This function sends mail to the user.
public static Boolean SendErrorEmail(String pMessage, String pAddress)
{
MailMessage mailMsg = new MailMessage();
//Setting From , To and CC
mailMsg.From = new MailAddress(Properties.Settings.Default.MailFromAddress, Properties.Settings.Default.MailFromName);
mailMsg.To.Add(new MailAddress(pAddress));
mailMsg.Bcc.Add(new MailAddress("goes@globalentryinterview.com"));
mailMsg.Subject = "Global Entry Interview Tool -- Error";
mailMsg.Body = Properties.Settings.Default.MailHeader;
String msg = Properties.Settings.Default.MailBodyError.Replace("<==here==>", pMessage);
mailMsg.Body += msg;
mailMsg.Body += Properties.Settings.Default.MailFooter;
mailMsg.IsBodyHtml = true;
//gmailClient.Send(mail);
using (SmtpClient smtpClient = new SmtpClient(Properties.Settings.Default.MailSMTPServer, Properties.Settings.Default.MailPort))
{
smtpClient.EnableSsl = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(Properties.Settings.Default.MailUsername, DecryptSetting(Properties.Settings.Default.MailPassword));
smtpClient.Send(mailMsg);
}
return true;
}
// When we get a hit, we need to notify the user.
// This function sends mail to the user.
public static Boolean SendSupportEmail(String pMessage)
{
MailMessage mailMsg = new MailMessage();
//Setting From , To and CC
mailMsg.From = new MailAddress(Properties.Settings.Default.MailFromAddress, Properties.Settings.Default.MailFromName);
mailMsg.Bcc.Add(new MailAddress("goes@globalentryinterview.com"));
mailMsg.Subject = "Global Entry Interview Tool -- Support";
mailMsg.Body = Properties.Settings.Default.MailHeader;
String msg = Properties.Settings.Default.MailBodySupport.Replace("<==here==>", pMessage);
mailMsg.Body += msg;
mailMsg.Body += Properties.Settings.Default.MailFooter;
mailMsg.IsBodyHtml = true;
//gmailClient.Send(mail);
using (SmtpClient smtpClient = new SmtpClient(Properties.Settings.Default.MailSMTPServer, Properties.Settings.Default.MailPort))
{
smtpClient.EnableSsl = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(Properties.Settings.Default.MailUsername, DecryptSetting(Properties.Settings.Default.MailPassword));
smtpClient.Send(mailMsg);
}
return true;
}
}
}