-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
579 lines (502 loc) · 22.5 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using System.Globalization;
namespace TicketTracker
{
public class DateTimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return ((DateTime)value).ToString("dd/MM/yyyy");
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return DateTime.Parse(((string)value));
}
}
public class ValidToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
string valid = (string)value;
string path = @"C:\Users\Jacob Zaidi\Desktop\Careers\Projects\.projects\Ticket Tracker\Icons\";
if (valid == "-1") { return path + "Unchecked.png"; }
if (valid == "0") { return path + "Invalid.png"; }
if (valid == "1") { return path + "Valid.png"; }
if (valid == "2") { return path + "Loading.png"; }
return path + "Error.png";
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return DateTime.Parse(((string)value));
}
}
public class ValidDate
{
public string from { get; set; }
public string to { get; set; }
public DateTime date { get; set; }
public string valid { get; set; }
public string status { get; set; }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public bool initialized = false;
public string from;
public string to;
public static string connectionString = "Data Source=.;Initial Catalog=BusDB;Integrated Security=True;Pooling=False";
public static SqlConnection conn;
public static SqlCommand cmd;
public static SqlDataAdapter adapter;
public static DataSet ds;
public static SqlDataReader reader;
public IList<ValidDate> co1;
public Thread t1;
public Thread t2;
IWebDriver driver;
WebDriverWait wait;
public List<string> ComboBoxCitiesList { get; set; }
public MainWindow()
{
InitializeComponent();
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
driver = new ChromeDriver(driverService, chromeOptions);
conn = new SqlConnection(connectionString);
conn.Open();
valid_dates_ReconfigureTable();
conn.Close();
InitializeComboBoxes("");
LabelStatus.Content = "Status: Not Initialized";
co1 = new List<ValidDate>();
ListBoxDates.ItemsSource = co1;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource);
view.Filter = UserFilter;
}
void InitializeComboBoxes(string text)
{
ComboBoxCitiesList = new List<string>();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string query = "SELECT [stop_Name] FROM [dbo].[stops]";
if (!string.IsNullOrEmpty(text)) { query += " WHERE [stop_name] LIKE '%{text}%'"; }
using (SqlCommand command = new SqlCommand(query, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
ComboBoxCitiesList.Add( (string)reader.GetString(0) );
}
}
}
}
ComboBoxFrom.ItemsSource = ComboBoxCitiesList;
ComboBoxTo.ItemsSource = ComboBoxCitiesList;
}
static void valid_dates_ReconfigureTable()
{
string query = "DELETE FROM [dbo].[valid_dates] WHERE [date] < (SELECT CAST(GETDATE() AS DATE))";
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.ExecuteNonQuery();
IDataRecord record;
query = "SELECT * FROM [dbo].[trips]";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
int day = 0;
while (reader.Read())
{
record = ((IDataRecord)reader);
bool[] days_of_the_week =
{
Convert.ToBoolean(record[2]),
Convert.ToBoolean(record[3]),
Convert.ToBoolean(record[4]),
Convert.ToBoolean(record[5]),
Convert.ToBoolean(record[6]),
Convert.ToBoolean(record[7]),
Convert.ToBoolean(record[8])
};
DateTime date = DateTime.Today.AddYears(1).AddDays(-1);
DateTime end = DateTime.Today;
bool runSql = true;
while (runSql && date >= end)
{
day = (int)date.DayOfWeek;
if (days_of_the_week[day])
{
SqlCommand check_User_Name = new SqlCommand(
$"SELECT * FROM[dbo].[valid_dates] " +
$"WHERE route_id = {record[0]} " +
$"AND date = '{date.ToString("yyyy-MM-dd")}'", conn);
bool rowExists = check_User_Name.ExecuteScalar() != null;
if (rowExists)
{
runSql = false;
break;
}
else
{
string new_query =
$"INSERT INTO [dbo].[valid_dates] " +
$"VALUES ({record[0]}, '{date.ToString("yyyy-MM-dd")}', NULL);";
myCommand = new SqlCommand(new_query, conn);
myCommand.ExecuteNonQuery();
}
}
date = date.AddDays(-1);
}
}
reader.Close();
}
}
public bool UserFilter(object item)
{
bool inSearch = (!string.IsNullOrEmpty((item as ValidDate).status));
if (CheckBoxInvalidDates.IsChecked == true && CheckBoxNewDates.IsChecked == false)
{
return (inSearch || (item as ValidDate).valid == "-1" || (item as ValidDate).valid == "0" || (item as ValidDate).valid == "1");
}
else if (CheckBoxInvalidDates.IsChecked == false && CheckBoxNewDates.IsChecked == true)
{
return (inSearch || (item as ValidDate).valid == "-1");
}
else if (CheckBoxNewDates.IsChecked == false && CheckBoxNewDates.IsChecked == false)
{
return (inSearch || (item as ValidDate).valid == "-1" || (item as ValidDate).valid == "1");
}
return true;
}
public void menuItemClose_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
return;
}
public void buttonGo_Click(object sender, RoutedEventArgs e)
{
from = ComboBoxFrom.Text;
to = ComboBoxTo.Text;
DataTable dt = CheckIfValid(from, to);
if (dt.Rows.Count == 0) { MessageBox.Show($"Sorry, {from} - {to} is not a valid route!"); }
else
{
if (t1 != null && t1.IsAlive) t1.Abort();
if (t2 != null && t2.IsAlive) t2.Abort();
ListBoxDates_Display2(dt);
InitializeRoute();
}
}
public DataTable CheckIfValid(string from, string to)
{
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("usp_get_valid_dates", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@origin", SqlDbType.VarChar).Value = from;
cmd.Parameters.Add("@destination", SqlDbType.VarChar).Value = to;
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
conn.Close();
}
}
return dt;
}
static void ValidDates_InsertValidDate(string From, string To, string Date, string Valid)
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
string query = $"IF EXISTS ( " +
$"SELECT * FROM [dbo].[ValidDates] " +
$"WHERE [From] = '{From}' " +
$"AND [To] = '{To}' " +
$"AND [Date] = '{Date}' " +
$") BEGIN " +
$"UPDATE [dbo].[ValidDates] " +
$"SET [Valid] = {Valid} " +
$"WHERE [From] = '{From}' " +
$"AND [To] = '{To}' " +
$"AND [Date] = '{Date}' " +
$"END " +
$"ELSE BEGIN " +
$"INSERT INTO [dbo].[ValidDates] ([From], [To], [Date], [Valid]) " +
$"VALUES ('{From}', '{To}', '{Date}', {Valid} ) " +
$"END;";
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { conn.Close(); }
}
static void valid_dates_Update(string Route_Id, string Date, string Valid)
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
string query = $"IF EXISTS ( " +
$"SELECT * FROM [dbo].[valid_dates] " +
$"WHERE [route_id] = {Route_Id} " +
$"AND [date] = '{Date}' " +
$") BEGIN " +
$"UPDATE [dbo].[valid_dates] " +
$"SET [is_valid] = {Valid} " +
$"WHERE [route_id] = {Route_Id} " +
$"AND [date] = '{Date}' " +
$"END " +
$"ELSE BEGIN " +
$"INSERT INTO [dbo].[valid_dates] ([route_id], [date], [is_valid]) " +
$"VALUES ({Route_Id}, '{Date}', {Valid} ) " +
$"END;";
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { conn.Close(); }
}
static void Buses_RemoveDate(string from, string to, string date)
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
string query = $"" +
$"DELETE FROM [dbo].[Buses] " +
$"WHERE [From] = '{from}' " +
$"AND [To] = '{to}' " +
$"AND [Departure] >= '{date}' " +
$"AND [Departure] < dateadd(day, 1, '{date}'";
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { conn.Close(); }
}
static void Buses_InsertValidTrip(string from, string to, string departure, string arrival, string transfers)
{
try
{
conn = new SqlConnection(connectionString);
conn.Open();
string query = $"INSERT INTO [dbo].[Buses] ([From], [To], [Departure], [Arrival], [Transfers]) " +
$"VALUES ('{from}', '{to}', '{departure}', '{arrival}', {transfers})";
SqlCommand myCommand = new SqlCommand(query, conn);
myCommand.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { conn.Close(); }
}
void ListBoxDates_Display2(DataTable dt)
{
co1 = new List<ValidDate>();
foreach (DataRow dr in dt.Rows)
{
string valid = "-1";
if (!string.IsNullOrEmpty(dr["is_valid"].ToString()))
valid = dr["is_valid"].ToString();
co1.Add(new ValidDate
{
to = to,
from = from,
date = (DateTime)dr["date"],
valid = valid,
status = ""
});
}
ListBoxDates.ItemsSource = co1;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource);
view.Filter = UserFilter;
}
void CheckBoxNewDates_Checked(object sender, EventArgs e)
{
CheckBoxInvalidDates.IsChecked = false;
CheckBoxInvalidDates.IsEnabled = false;
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
}
void CheckBoxNewDates_Unchecked(object sender, EventArgs e)
{
CheckBoxInvalidDates.IsEnabled = true;
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
}
void CheckBoxInvalidDates_Checked(object sender, EventArgs e)
{
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
}
void CheckBoxInvalidDates_Unchecked(object sender, EventArgs e)
{
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
}
void InitializeRoute()
{
t1 = new Thread(() =>
{
Application.Current.Dispatcher.Invoke(() => { LabelStatus.Content = "Status: Initializing..."; });
try
{
driver.Url = "https://www.intercity.co.nz/";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
IWebElement a = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("BookTravelForm_getBookTravelForm_from")));
a.Click();
string value = (string)js.ExecuteScript($"document.getElementById('BookTravelForm_getBookTravelForm_from').setAttribute('value', '{from}')");
IWebElement b = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("BookTravelForm_getBookTravelForm_to")));
b.Click();
string value2 = (string)js.ExecuteScript($"document.getElementById('BookTravelForm_getBookTravelForm_to').setAttribute('value', '{to}')");
a = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("BookTravelForm_getBookTravelForm_from")));
a.Click();
string today = DateTime.Now.ToString("dd");
IWebElement d = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//div[" +
$"((contains(text(), '{today}')) " +
"and not(contains(@class, 'invalid')))]")));
d.Click();
IWebElement e = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("BookTravelForm_getBookTravelForm_action_submit")));
e.Click();
Application.Current.Dispatcher.Invoke(() => { LabelStatus.Content = "Status: Initialized"; });
}
catch (Exception ex)
{
Application.Current.Dispatcher.Invoke(() =>
{
LabelStatus.Content = "Status: Not Initialized";
MessageBox.Show($"Sorry, something went wrong!");
});
}
});
t1.Name = "InitializingThread";
t1.Start();
}
string CheckDate(DateTime date)
{
Application.Current.Dispatcher.Invoke(() => { LabelStatus.Content = "Status: Searching..."; });
string intercityDate = date.ToString("ddd, d MMM yyyy");
Uri url = new Uri($"https://www.intercity.co.nz/book-a-trip/bookdepart?date={intercityDate}");
driver.Navigate().GoToUrl(url);
Thread.Sleep(5000);
var prices = driver.FindElements(By.XPath("//div[contains(@class,'price')]/ancestor::form[contains(@method, 'post')]"));
string valid = "0";
Buses_RemoveDate(from, to, date.ToString("yyyy-MM-dd"));
//REVERSE TO AVOID GOLD TICKET PROBLEMS
foreach (var price in prices.Reverse())
{
var route_ids = price.FindElements(By.XPath(".//div[@class='service-number']"));
string fare;
try { fare = price.FindElement(By.XPath(".//div[@class='price']")).Text; }
catch
{
fare = "SOLD OUT";
if (route_ids.Count == 1)
{
string why = route_ids[0].GetAttribute("outerHTML");
why = why.Substring(31, 4);
valid_dates_Update(why, date.ToString("yyyy-MM-dd"), "0");
}
}
var times = price.FindElements(By.XPath(".//div[@class='fare-time']"));
if (fare == "$1.00" || fare == "$1")
{
foreach (var route_id in route_ids)
{
string why = route_id.GetAttribute("outerHTML");
why = why.Substring(31, 4);
valid_dates_Update(why, date.ToString("yyyy-MM-dd"), "1");
}
string departure = times[0].Text;
departure = DateTime.Parse(date.ToString("dd/MM/yyyy") + " " + departure).ToString("yyyy-MM-dd HH:mm:ss.000");
string arrival = times[1].Text;
arrival = DateTime.Parse(date.ToString("dd/MM/yyyy") + " " + arrival).ToString("yyyy-MM-dd HH:mm:ss.000");
string transfers = price.FindElement(By.XPath(".//p[@class='txt transfers']")).Text;
transfers = transfers == "Direct Service" ? "0" : transfers[0].ToString();
Buses_InsertValidTrip(from, to, departure, arrival, transfers);
valid = "1";
}
else
{
if (route_ids.Count == 1)
{
string why = route_ids[0].GetAttribute("outerHTML");
why = why.Substring(31, 4);
valid_dates_Update(why, date.ToString("yyyy-MM-dd"), "0");
}
}
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
}
ValidDates_InsertValidDate(from, to, date.ToString("yyyy-MM-dd"), valid);
Application.Current.Dispatcher.Invoke(() => { LabelStatus.Content = "Status: Initialized"; });
return valid;
}
void ButtonSearch_Click(object sender, EventArgs e)
{
if ((string)LabelStatus.Content == "Status: Initialized")
{
if (t1 != null && t1.IsAlive) t1.Abort();
if (t2 != null && t2.IsAlive) t2.Abort();
t2 = new Thread(() =>
{
for (int i = 0; i < co1.Count; i++)
{
if (Application.Current.Dispatcher.Invoke(() => { return UserFilter(co1.ElementAt(i)); }))
{
co1[i].status = "loading";
co1[i].valid = "2";
Application.Current.Dispatcher.Invoke(() =>
{
ListBoxDates.ItemsSource = co1;
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
});
co1[i].valid = CheckDate(co1[i].date);
Application.Current.Dispatcher.Invoke(() =>
{
ListBoxDates.ItemsSource = co1;
CollectionViewSource.GetDefaultView(ListBoxDates.ItemsSource).Refresh();
});
}
}
});
t2.Name = "SearchingThread";
t2.Start();
}
else if ((string)LabelStatus.Content == "Status: Searching...") { MessageBox.Show($"If you want to search again, please reinitialize this route!"); }
else { MessageBox.Show($"You have not initialized a route!"); }
/*
foreach (ValidDate item in ListBoxDates.Items)
{
CheckDate(item.date);
}*/
}
}
}