示例#1
0
        private IList <CitySiteData> GetPopularCitiesMainData(LoadHtmlSource loadHtmlSource, HtmlDocument htmlDocument, string mainDataSiteUrl)
        {
            var popularCitiesElements = htmlDocument.GetElementbyId(PopularCitiesElementId);
            var popularCitiesCount    = popularCitiesElements.SelectNodes("a").Count;
            var popularCities         = CreatePopularCities(popularCitiesCount);

            var popularCityIndex = 0;

            foreach (HtmlNode popularCitiesElement in popularCitiesElements.SelectNodes("a"))
            {
                var cityName = popularCitiesElement.GetAttributeValue("data-name", String.Empty);

                if (string.IsNullOrWhiteSpace(cityName))
                {
                    throw new Exception("City name can't be null or empty");
                }

                var cityDataUrl = popularCitiesElement.GetAttributeValue("data-url", String.Empty);

                if (string.IsNullOrWhiteSpace(cityName))
                {
                    throw new Exception("City data url can't be null or empty");
                }

                popularCities[popularCityIndex].Name       = cityName;
                popularCities[popularCityIndex].DataUrl    = CreateDataUrl(loadHtmlSource, mainDataSiteUrl, cityDataUrl);;
                popularCities[popularCityIndex++].IsActive = true;
            }

            return(popularCities);
        }
示例#2
0
        /// <summary>
        /// Get site weather data
        /// </summary>
        public async Task <IList <CitySiteData> > GrabSiteWeatherByCitiesAsync(LoadHtmlSource loadHtmlSource, string dataSource)
        {
            if (string.IsNullOrWhiteSpace(dataSource))
            {
                throw new Exception("Data source can't be empty");
            }

            try
            {
                var mainHtmlDocument = await LoadHtmlDocumentDataAsync(loadHtmlSource, dataSource);

                var popularCities = GetPopularCitiesMainData(loadHtmlSource, mainHtmlDocument, dataSource);

                foreach (var popularCity in popularCities)
                {
                    var internalHtmlDocument = await LoadHtmlDocumentDataAsync(loadHtmlSource, popularCity.DataUrl);

                    popularCity.CityWeatherInfos = GetPopularCityWeatherInfo(internalHtmlDocument, popularCity.DataUrl);
                }

                return(popularCities);
            }
            catch (Exception e)
            {
                _logger.LogError($"WeatherSiteDataGrabService GrabSiteWeatherByCitiesAsync error - [{e}]");
                return(new List <CitySiteData>());
            }
        }
示例#3
0
        private async Task <HtmlDocument> LoadHtmlDocumentDataAsync(LoadHtmlSource loadHtmlSource, string dataSource)
        {
            switch (loadHtmlSource)
            {
            case LoadHtmlSource.Web:
                return(await LoadHtmlDocumentFromWebAsync(dataSource));

            case LoadHtmlSource.File:
                return(await LoadHtmlDocumentFromFileAsync(dataSource));

            default:
                throw new Exception("Can't recognize load file source");
            }
        }
示例#4
0
        private string CreateDataUrl(LoadHtmlSource loadHtmlSource, string mainDataSiteUrl, string cityDataUrl)
        {
            switch (loadHtmlSource)
            {
            case LoadHtmlSource.Web:
                return(mainDataSiteUrl + cityDataUrl + TenDaysPrefix);

            case LoadHtmlSource.File:
                return(Directory.GetCurrentDirectory() + @"\TestData\testCityWeatherInfoData.html");

            default:
                throw new Exception("Can't recognize load source type");
            }
        }