コード例 #1
0
        public async Task ParseAsync()
        {
            var forecast = new Dal.Context.Models.Forecast()
            {
                Date          = DateTime.Now,
                CityForecasts = new List <CityForecast>()
            };

            var web = new HtmlWeb();
            var doc = await web.LoadFromWebAsync(_parserSettings.Resource);

            foreach (var cityData in doc.DocumentNode.GetCities())
            {
                var cityName = cityData.GetCityName();
                var city     = await _forecastRepository.GetCityAsync(cityName) ?? new City
                {
                    Name = cityName
                };

                var cityDoc =
                    await web.LoadFromWebAsync(
                        $"{_parserSettings.Resource}{cityData.GetCityUrl()}{_parserSettings.Period}");

                var forecastData = cityDoc.DocumentNode.GetForecast();

                var describes      = forecastData.GetDescribes();
                var temperatures   = forecastData.GetTemperatures();
                var winds          = forecastData.GetWinds();
                var precipitations = forecastData.GetPrecipitations();

                var cityForecast = GetCityForecast(forecast, city);

                for (var i = 0; i < temperatures.Length; i++)
                {
                    cityForecast.DayForecasts.Add(GetDayForecast(i, describes, winds, precipitations, temperatures));
                }

                forecast.CityForecasts.Add(cityForecast);
            }

            await _forecastRepository.AddForecastAsync(forecast);
        }