public async Task <WeatherData> GetWeatherData(double latitude, double longitude) { Task <DetailedWeather> asyncCurrentWeather = GetCurrentWeather(latitude, longitude); Task <ICollection <Forecast> > asyncFutureWeather = GetFutureWeather(latitude, longitude); await Task.WhenAll(asyncCurrentWeather, asyncFutureWeather); DetailedWeather currentWeather = asyncCurrentWeather.Result; Weather weather = currentWeather?.weather?.FirstOrDefault(); ICollection <Forecast> forecasts = asyncFutureWeather.Result; if (currentWeather != null && weather != null && forecasts != null) { return new WeatherData { Description = weather.description, Sunrise = currentWeather.systemInformation.sunriseTime, Sunset = currentWeather.systemInformation.sunsetTime, City = currentWeather.city, Country = currentWeather.systemInformation.country, WeatherForecasts = forecasts } } ; if (currentWeather == null) { throw new ArgumentNullException(nameof(currentWeather)); } if (weather == null) { throw new ArgumentNullException(nameof(weather)); } throw new ArgumentNullException(nameof(forecasts)); }
public List <WeatherInfo> GetWeatherInfoList(CityInfo city) { List <WeatherInfo> result = new List <WeatherInfo>(); using (var driver = new PhantomJSDriver()) { foreach (var day in _days) { var baseUri = new Uri(city.Url); driver.Navigate().GoToUrl(new Uri(baseUri, day)); var culture = driver.GetInnerElementAtributeValue("body", "class").Split(' ').Skip(2).FirstOrDefault(); var baseNode = driver.GetInnerElement("div", "class", "main") .GetInnerElement("div", "class", "column-wrap") .GetInnerElement("div", "class", "__frame_sm") .GetInnerElement("div", "class", "forecast_frame hw_wrap"); var baseDate = baseNode.GetInnerElement("div", "class", "tab tooltip") .GetInnerElement("div", "class", "tab_wrap") .GetInnerElement("div", "class", "tab-content"); var date = (baseDate.GetInnerElementText("div", "class", "date ") ?? baseDate.GetInnerElementText("div", "class", "date weekend")) ?.Split(',') .LastOrDefault() ?.Trim(); if (string.IsNullOrWhiteSpace(date)) { continue; } var detailsNode = baseNode.GetInnerElement("div", "class", "widget js_widget") .GetInnerElement("div", "class", "widget__body") .GetInnerElement("div", "class", "widget__container"); var hours = detailsNode.GetInnerElement("div", "class", "widget__row widget__row_time") .GetInnerElements("div", "class", "widget__item"); var icons = detailsNode.GetInnerElement("div", "class", "widget__row widget__row_table widget__row_icon") .GetInnerElements("div", "class", "widget__item"); var temps = detailsNode.GetInnerElement("div", "class", "widget__row widget__row_table widget__row_temperature") .GetInnerElement("div", "class", "templine w_temperature") .GetInnerElement("div", "class", "chart chart__temperature") .GetInnerElement("div", "class", "values") .GetInnerElements("div", "class", "value"); var winds = detailsNode.GetInnerElement("div", "class", "widget__row widget__row_table widget__row_wind-or-gust") .GetInnerElements("div", "class", "widget__item"); var precs = detailsNode.GetInnerElement("div", "class", "widget__row widget__row_table widget__row_precipitation") .GetInnerElements("div", "class", "widget__item"); var maxlen = new[] { hours.Count, icons.Count, temps.Count, winds.Count, precs.Count }.Max(); var wlist = new List <HourDetails>(Enumerable.Range(0, maxlen).Select(r => new HourDetails())); for (int i = 0; i < maxlen; i++) { var hr = hours.ByInd(i); if (hr != null) { var h = hr.GetInnerElement("div", "class", "w_time").FindElement(By.TagName("span")).Text; var m = hr.GetInnerElement("div", "class", "w_time").FindElement(By.TagName("sup")).Text; if (int.TryParse(h, out var hi) && int.TryParse(m, out var mi)) { wlist[i].Time = TimeSpan.FromMinutes(hi * 60 + mi); } } var ic = icons.ByInd(i); if (ic != null) { var svg = ic.GetInnerElement("div", "class", "widget__value w_icon") .GetInnerElement("span", "class", "tooltip") .GetAttribute("innerHTML"); // Сакральные знания... wlist[i].IconSvg = svg; } var tm = temps.ByInd(i); if (tm != null) { var t = tm.GetInnerElementText("span", "class", "unit unit_temperature_c"); if (double.TryParse(t, out var td)) { wlist[i].Temperature = td; } } var wn = winds.ByInd(i); if (wn != null) { var w = wn.GetInnerElement("div", "class", "w_wind") .GetInnerElement("div", "class", "w_wind__warning w_wind__warning_ ") .GetInnerElementText("span", "class", "unit unit_wind_m_s"); wlist[i].WindText = w; } var pc = precs.ByInd(i); if (pc != null) { var t = pc.GetInnerElement("div", "class", "w_prec") .GetInnerElementText("div", "class", "w_prec__value"); if (double.TryParse(t, out var td)) { wlist[i].Humidity = td; } } } var currDate = DateTime.ParseExact(date, new[] { "d MMM", "d MMM y", "d MMM yyyy" }, CultureInfo.GetCultureInfo("ru"), DateTimeStyles.None); var detailedWeather = new DetailedWeather() { WeatherByHours = new SortedDictionary <TimeSpan, HourDetails>(wlist.ToDictionary(x => x.Time)) }; result.Add(new WeatherInfo() { CurrDate = currDate, DetailedWeather = detailedWeather }); } driver.Quit(); return(result); } }