public async Task ByState( [Option("s", "State name")] string state) { if (string.IsNullOrEmpty(state)) { _logger.LogError($"Invalid state {state}"); return; } _logger.LogInformation("Fetching states.."); var allstates = await _cowinHttpClient.GetAllStates(); if (default == allstates) { return; } if (allstates.IsEmpty()) { _logger.LogError($"No states found."); return; } var matchingState = allstates.GetState(state); if (default == matchingState) { return; } if (matchingState.IsEmpty()) { _logger.LogError($"Invalid State {state}"); return; } _logger.LogInformation($"Fetching districts for state {state}"); var districtsInState = await _cowinHttpClient.GetAllDistrictsInState(matchingState); if (default == districtsInState) { return; } if (districtsInState.IsEmpty()) { _logger.LogError("No districts found."); return; } Console.Clear(); await _notificationService.ShowDistrictsToUser(districtsInState, matchingState.Name); }
public async Task Start( [Option("s", "State name")] string state, [Option("d", "District nam (example: BBMP)")] string district, [Option("a", "Minimum Age limit (18, 45 or 60)")] int age, [Option("Starting date from which to track in dd-mm-yyyy format. Example: 03-11-2021. If skipped will start from current week.")] string date = null, [Option("w", "Number of future weeks to check for availability")] int futureWeeks = 4, [Option("c", "If true, will continuously check for vaccine availability.")] bool continuous = false) { if (age < 18 || age > 200) { _logger.LogError($"Invalid Age {age}"); return; } if (futureWeeks < 1 || futureWeeks > 53) { _logger.LogError("Invalid number of weeks to check for availability."); return; } _logger.LogInformation("Fetching states.."); var allstates = await _cowinHttpClient.GetAllStates(); if (default == allstates) { return; } if (allstates.IsEmpty()) { _logger.LogError($"No states found."); return; } var matchingState = allstates.GetState(state); if (default == matchingState) { return; } if (matchingState.IsEmpty()) { _logger.LogError($"Invalid State {state}"); return; } _logger.LogInformation($"Fetching districts for state {state}"); var districtsInState = await _cowinHttpClient.GetAllDistrictsInState(matchingState); if (default == districtsInState) { return; } if (districtsInState.IsEmpty()) { _logger.LogError("No districts found."); return; } var matchingDistrict = districtsInState.GetDistrict(district); if (default == matchingDistrict) { return; } if (matchingDistrict.IsEmpty()) { _logger.LogError($"Invalid District {district}"); return; } var nextWeeks = DateHelpers.GetStartDatesForUpcomingWeeks(date, futureWeeks, false); if (null == nextWeeks || nextWeeks.Count < 1) { _logger.LogError($"Invalid date {date}"); return; } if (!continuous) { try { Console.Clear(); foreach (var weekStart in nextWeeks) { var vaccineCenters = await _cowinHttpClient .FindMatchingVaccineCenter( matchingDistrict.Id, weekStart, age, true); if (null == vaccineCenters || vaccineCenters.Count < 1) { _logger.LogDebug($"No vaccine center found in district {district} for week starting on {weekStart} for age >= {age}."); continue; } await _notificationService.NotifyUser(vaccineCenters, weekStart, age); await Task.Delay(TimeSpan.FromSeconds(10)); } Console.WriteLine("..Done"); //Console.ReadLine(); } catch (Exception ex) { _logger.LogError(ex, "Error found while checking for vaccine."); } } else { int iteration = 0; try { while (!this.Context.CancellationToken.IsCancellationRequested) { try { Console.Clear(); Console.WriteLine($"iteration {++iteration}.."); foreach (var weekStart in nextWeeks) { var vaccineCenters = await _cowinHttpClient .FindMatchingVaccineCenter( matchingDistrict.Id, weekStart, age, true); if (null == vaccineCenters || vaccineCenters.Count < 1) { _logger.LogDebug($"No vaccine center found in district {district} for week starting on {weekStart} for age >= {age}."); continue; } await _notificationService.NotifyUser(vaccineCenters, weekStart, age); await Task.Delay(TimeSpan.FromSeconds(10)); } } catch (Exception ex) { _logger.LogError(ex, "Error found while checking for vaccine."); } // wait for next time await Task.Delay(TimeSpan.FromMinutes(1), this.Context.CancellationToken); } } catch (Exception ex) when(!(ex is OperationCanceledException)) { _logger.LogError(ex, $"Error encountered during Continuous tracking by district {district}"); } finally { } } }