public static async Task RunAsync() { try { var isFileExists = SaveFile(); if (isFileExists) { var format = "yyy-M-d"; CultureInfo provider = CultureInfo.InvariantCulture; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); StreamReader file = new System.IO.StreamReader(@$"{filePath}"); string d; while ((d = file.ReadLine()) != null) { string[] newDateSplitted = { }; _ = new DateTime(); bool formttedDate; DateTime newDate; if (d.Contains("/")) { newDateSplitted = d.Split("/"); newDate = new DateTime(Convert.ToInt32(newDateSplitted[2].Length < 4 ? ("20" + newDateSplitted[2]) : newDateSplitted[2]), Convert.ToInt32(newDateSplitted[0]), Convert.ToInt32(newDateSplitted[1])); formttedDate = true; } else { formttedDate = DateTime.TryParse(d, out newDate); } if (formttedDate) { NasaData nsaData = await GetNasaDataAsync(newDate.ToString(format, provider)); await SaveImage(nsaData.Url); ShowNasaImg(nsaData); } } } } catch (Exception e) { Console.WriteLine(e.Message); } }
static async Task<NasaData> GetNasaDataAsync(string date) { NasaData nsaData = null; HttpResponseMessage response = await client.GetAsync(string.Format(apiUrl, apiKey, date)); if (response.IsSuccessStatusCode) { nsaData = await response.Content.ReadAsAsync<NasaData>(); } return nsaData; }
static void ShowNasaImg(NasaData nsaData) { Process.Start(new ProcessStartInfo("chrome.exe") { UseShellExecute = true, Arguments = nsaData.Url }); }