private List <OneDayAstro> GetAstroFromApixu() { string apixuURL = @"http://api.apixu.com/v1/forecast.json?key=ae97e7b3f0894de58ea153737171909&q=Odessa,ua&days=6"; dynamic data = JsonFromUrl(apixuURL, "Apixu"); if (data == null) { return(null); } List <OneDayAstro> sixDayAstro = new List <OneDayAstro>(); try { DateTime timeValue; string format = "yyyy-MM-dd hh:mm tt"; foreach (var day in data.forecast.forecastday) { OneDayAstro astro = new OneDayAstro(); if (DateTime.TryParseExact(day["date"].ToString() + " " + day.astro.sunrise.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeValue)) { astro.Sunrise = timeValue; } if (DateTime.TryParseExact(day["date"].ToString() + " " + day.astro.sunset.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeValue)) { astro.Sunset = timeValue; } if (DateTime.TryParseExact(day["date"].ToString() + " " + day.astro.moonrise.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeValue)) { astro.Moonrise = timeValue; } if (DateTime.TryParseExact(day["date"].ToString() + " " + day.astro.moonset.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out timeValue)) { astro.Moonset = timeValue; } sixDayAstro.Add(astro); } if (sixDayAstro.Count > 0) { return(sixDayAstro); } else { Logger.Log.Error(String.Format("An error occurred while trying to receive astro data on schedule from the source: {0}; in the method : {1}; by the address {2}", "Apixu", "GetAstroFromApixu", apixuURL)); return(null); } } catch (Exception ex) { Logger.Log.Error(String.Format("An error occurred while trying to receive astro data on schedule from the source: {0}; in the method : {1}; by the address {2}", "Apixu", "GetAstroFromApixu", apixuURL), ex); return(null); } }
public static OneDayAstro GetAstroByDate(DateTime date)//returns Astro by date { if (AllDaysAstro == null) { return(null); } OneDayAstro dayAstro = AllDaysAstro.Where(x => x.Sunrise.HasValue && x.Sunrise.Value.DayOfYear == date.DayOfYear).FirstOrDefault(); //looking for the object of list by date if (dayAstro != null) { return(dayAstro); } else //there is no record on this date { Logger.Log.Error(String.Format("An error occurred while trying to receive astro data by date. In the method : {0}", "GetAstroByDate")); return(null); } }