-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
553 lines (453 loc) · 21.9 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using VaccineSlotter;
using System.Media;
using System.Net.Mail;
namespace VaccineSlotter
{
//======================================================================================
class Program
{
static private string LoginStatus = "";
//======================================================================================
private static string GetCookie()
{
string uri = string.Empty;
string cookie = string.Empty;
HttpWebResponse response = POST(uri);
if (response == null)
{
LoginStatus = "Not connected";
return "";
}
WebHeaderCollection headers = response.Headers;
if ((response.StatusCode == HttpStatusCode.Found) ||
(response.StatusCode == HttpStatusCode.Redirect) ||
(response.StatusCode == HttpStatusCode.Moved) ||
(response.StatusCode == HttpStatusCode.MovedPermanently))
{
if (headers["Set-Cookie"] != null)
{
string cookies = headers["Set-Cookie"];
String[] fields = Regex.Split(cookies, ";\\s*");
cookie = fields[0];
}
}
return cookie;
}
//======================================================================================
public static HttpWebResponse POST(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response;
}
catch
{
return (HttpWebResponse)null;
}
}
//======================================================================================
public static HttpWebResponse GET(string url)
{
//https://www.codeproject.com/Questions/621289/get-http-request-header-values-in-to-csharp
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response;
}
catch
{
return (HttpWebResponse)null;
}
}
//======================================================================================
public static HttpWebResponse POST(string postData, string url, string referer, string cookie)
{
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(cookie))
request.Headers.Add(HttpRequestHeader.Cookie, cookie);
if (!string.IsNullOrEmpty(referer))
request.Referer = referer;
request.ContentLength = byteArray.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
// request.Proxy = null;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
return (HttpWebResponse)request.GetResponse();
}
catch
{
return (HttpWebResponse)null;
}
}
catch
{
return (HttpWebResponse)null;
}
}
//======================================================================================
static string DisplayCenterData(ref CenterData data)
{
string result = "\n";
string endLineCharInMail = " <br />";
result += "------------------------------------------------------" + endLineCharInMail;
Console.WriteLine("==============================================\n");
result += endLineCharInMail + "Name: \t" + data.Name;
Console.WriteLine("\nName: \t" + data.Name);
result += endLineCharInMail + "PinCode: \t" + data.PinCode +"\n";
Console.WriteLine("\nPinCode \t" + data.PinCode);
List<SessonData> pSessionData = data.SessonsDataArray;
for (int sCount = 0; pSessionData != null && sCount < pSessionData.Count; ++sCount)
{
SessonData pObj = pSessionData[sCount];
int minAgeInData = pObj.Min_age_limit;
int slotAvailable = pObj.Available_Capacity;
result += endLineCharInMail + "Min Age Limit \t" + minAgeInData.ToString() + endLineCharInMail;
Console.WriteLine("\nMin Age Limit: \t" + minAgeInData.ToString());
result += "Slot Available: \t" + slotAvailable.ToString() + endLineCharInMail;
Console.WriteLine("\nSlot Available: \t" + slotAvailable.ToString());
}
result += "===========================================" + "\n";
Console.WriteLine("===========================================\n");
return result;
}
//=======================================================================
static HttpStatusCode ReadDataByDistrictName(int minAge, int distCode, ref string emailId, ref string mailBody)
{
try
{
DateTime date = DateTime.Now;
// converting to string format
string date_str = date.ToString("dd-MM-yyyy");
string URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=" + distCode.ToString() +
"&date=" + date_str;
HttpWebResponse response = Program.GET(URL);
if (response == null)
return HttpStatusCode.BadRequest;
HttpStatusCode statusCode = response.StatusCode;
if (statusCode == HttpStatusCode.OK)
{
//var responseVal = new StreamReader(stream: response.GetResponseStream()).ReadToEnd();
ResponseHandler pResponseHandler = new ResponseHandler();
string converted = pResponseHandler.ConvertResponseStreamToString(response);
List<CenterData> centerData = null;
ParseStringByDist.GetCentersData(converted, out centerData);
List<CenterData> shortedData = new List<CenterData>();
for (int count = 0; count < centerData.Count; ++count)
{
CenterData pData = centerData[count];
List<SessonData> pSessionData = pData.SessonsDataArray;
for (int sCount = 0; pSessionData != null && sCount < pSessionData.Count; ++sCount)
{
SessonData pSessonDataInArray = pSessionData[sCount];
int minAgeInData = pSessonDataInArray.Min_age_limit;
int slotAvailable = pSessonDataInArray.Available_Capacity;
if (minAgeInData == minAge)
{
if (slotAvailable > 0)
{
shortedData.Add(pData);
}
}
}
}// Data
if (shortedData.Count > 0)
{
string mailBodyToSend = "";
for (int count = 0; count < shortedData.Count; ++count)
{
Console.Beep();
Console.Beep();
CenterData pData = shortedData[count];
string result = DisplayCenterData(ref pData);
mailBodyToSend += result;
}// Vaild data
// Send mail if found different info
if (string.Equals(mailBodyToSend, mailBody) == false)
{
mailBody = mailBodyToSend;
Email(ref mailBody, ref emailId);
}
}// checker
}
return statusCode;
}
catch
{
return HttpStatusCode.BadRequest;
}
}
//======================================================================================
public static void Email(ref string htmlString, ref string emailIdOfReceiver)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress(emailIdOfReceiver));
message.Subject = "Vaccine slot available in your area";
message.IsBodyHtml = true; //to make message body as html
message.Body = htmlString;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com"; //for gmail host
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("[email protected]", "PASSWORD");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception)
{
}
}
//======================================================================================
static void ReadDownloadedFile(string filename, ref List<DriveTextData> distData)
{
try
{
// Read Text file data
StreamReader sr = new StreamReader(filename);
string readedLine = sr.ReadLine();
string emailData = "Email:";
string districtData = "DistrictCode:";
string minAgeData = "MinAge:";
string startData = "<";
string endData = ">";
DriveTextData tempData = null;
while (readedLine != null)
{
int indexEmail = readedLine.IndexOf(emailData);
int indexStart = readedLine.IndexOf(startData);
int indexEnd = readedLine.IndexOf(endData);
int indexMinEdge = readedLine.IndexOf(minAgeData);
int indexDistCode = readedLine.IndexOf(districtData);
if (indexStart >= 0)
{
string startString = readedLine.Substring(indexStart + startData.Length);
startString = startString.Trim();
if (startString.Length == 0)
tempData = new DriveTextData();
}
if (indexEnd >= 0)
{
string endString = readedLine.Substring(indexEnd + endData.Length);
endString = endString.Trim();
if (endString.Length == 0)
{
distData.Add(tempData);
tempData = null;
}
}
if (indexEmail >= 0)
{
string emailId = readedLine.Substring(indexEmail + emailData.Length);
emailId = emailId.Trim();
tempData.EmailID = emailId;
}
else if (indexMinEdge >= 0)
{
string age = readedLine.Substring(indexMinEdge + minAgeData.Length);
age = age.Trim();
bool tempBool = age.Contains(".");
if (tempBool == false)
tempData.MinAge = int.Parse(age);
}
else if (indexDistCode >= 0)
{
string dist = readedLine.Substring(indexDistCode + districtData.Length);
dist = dist.Trim();
bool tempBool = dist.Contains(".");
if (tempBool == false)
tempData.DistCode = int.Parse(dist);
}
//Read the next line
readedLine = sr.ReadLine();
}
//close the file
sr.Close();
}
catch
{
}
}
//======================================================================================
static void DownloadFileAndReadData(ref List<DriveTextData> driveData)
{
// Download drive file
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "text.txt");
string tempPath = System.IO.Path.GetTempPath();
path = tempPath + "DownloadedFile.txt";
FileDownloader fileDownloader = new FileDownloader();
fileDownloader.DownloadFile("https://drive.google.com/uc?id=1DOTrAk4QtlTVsUg9CE2iQo8Xvao7GVDL&export=download", path);
ReadDownloadedFile(path, ref driveData);
}
//======================================================================================
static void FindVaccineByDriveLink()
{
// Download and read drive file
List<DriveTextData> driveData = new List<DriveTextData>();
DownloadFileAndReadData(ref driveData);
DateTime messageTime = DateTime.Now;
HttpStatusCode statusCode = HttpStatusCode.NotImplemented;
DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now;
TimeSpan ts = endTime - startTime;
int sec = 3;
// Make mail body same as email we got
List<string> mailBodySendOnEmail = new List<string>();
for (int countD = 0; countD < driveData.Count; ++countD)
mailBodySendOnEmail.Add("");
Console.WriteLine("\n------------------Vaccine-Spotter is Running--------------------------\n");
while (true)
{
if (sec >= 3) // 3 Sec
{
for (int countD = 0; countD < driveData.Count; ++countD )
{
DriveTextData pDriveData = driveData[countD];
string mailSended = mailBodySendOnEmail[countD];
string emailId = pDriveData.EmailID;
statusCode = ReadDataByDistrictName(pDriveData.MinAge, pDriveData.DistCode,
ref emailId, ref mailSended);
mailBodySendOnEmail[countD] = mailSended;
if (statusCode != HttpStatusCode.OK)
break;
}// User Loop
if (statusCode != HttpStatusCode.OK)
{
continue; // Sometime network is week so avoid it for now. and try again immediately
}
startTime = DateTime.Now;
endTime = DateTime.Now;
}
endTime = DateTime.Now;
ts = endTime - startTime;
sec = ts.Seconds;
TimeSpan tempTS = endTime - messageTime;
if (tempTS.Minutes == 5) // 5 Min-- Download data And update users
{
Console.WriteLine("\n------------------Vaccine-Spotter is Running--------------------------\n");
mailBodySendOnEmail.Clear();
driveData.Clear();
// Download and read data
DownloadFileAndReadData(ref driveData);
// Make mail body same as email we got
for (int countD = 0; countD < driveData.Count; ++countD)
mailBodySendOnEmail.Add("");
messageTime = DateTime.Now;
}
} // While Loop
}
//======================================================================================
static void FindVaccineByUserPreferance()
{
Console.WriteLine("Enter min age to set an alert");
string ageString = Console.ReadLine();
if (ageString == null || ageString.Length == 0)
{
Console.WriteLine("!!! Invalid Input Age. Run Software Again !!!!\n");
Console.ReadLine();
return;
}
int age = Convert.ToInt32(ageString);
Console.WriteLine("Give me your email ID to alert");
string emailID = Console.ReadLine();
Console.WriteLine("\nGive me your District code, ex 367 for Buldhana, 376 for Satara");
string distCodeString = Console.ReadLine();
if (distCodeString == null || distCodeString.Length == 0)
{
Console.WriteLine("!!! Invalid Input District Code. Run Software Again !!!!\n");
Console.ReadLine();
return;
}
int distCode = Convert.ToInt32(distCodeString);
Console.WriteLine("\n------------------Vaccine-Spotter is Running--------------------------\n");
DateTime startTime = DateTime.Now;
DateTime endTime = DateTime.Now;
TimeSpan ts = endTime - startTime;
int sec = 3;
DateTime messageTime = DateTime.Now;
HttpStatusCode statusCode = HttpStatusCode.NotImplemented;
string mailBodySendOnEmail = "";
while (true)
{
if (sec >= 3) // 3 Sec
{
startTime = DateTime.Now;
endTime = DateTime.Now;
statusCode = ReadDataByDistrictName(age, distCode, ref emailID, ref mailBodySendOnEmail);
if (statusCode != HttpStatusCode.OK)
break;
}
endTime = DateTime.Now;
ts = endTime - startTime;
sec = ts.Seconds;
TimeSpan tempTS = endTime - messageTime;
if (tempTS.Minutes == 3) // 3 Min
{
Console.WriteLine("\n -----------------Vaccine-Spotter is Running to check Available Slot. -------------\n");
messageTime = DateTime.Now;
}
} // While Loop
if (statusCode != HttpStatusCode.OK)
{
Console.WriteLine(statusCode.ToString());
string tempResponseString = Console.ReadLine();
}
}
//======================================================================================
static void Main(string[] args)
{
Console.WriteLine(" Welcome!! \n I can alert you when slot is available. \n\n");
Console.WriteLine(" Option 1. Search vaccine Manually \n");
Console.WriteLine(" Option 2. Search vaccine Using Drive Link \n");
int opetion = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
if (opetion == 1)
{
FindVaccineByUserPreferance();
}
else if (opetion == 2)
{
FindVaccineByDriveLink();
}
}
//======================================================================================
}
}