Exemple #1
0
        private static void GetRegionalWeatherFromPostalCode(int postalCode, Action<RegionalWeatherResult, Exception> callback)
        {
            if (callback == null)
                throw new ArgumentNullException("callback");

            var client = HttpWebRequest.Create(GetRegionalContentFromPostalCode(postalCode));
            client.DownloadStringAsync(html =>
            {
                var input = HttpUtility.HtmlDecode(html);

                var textPattern = @"<td class=""broedtekst"">(?<text>.*?)</td>";
                var textRegex = new Regex(textPattern, RegexOptions.Singleline);
                var textMatches = textRegex.Matches(input);

                var namePattern = @"<font class=""mellemrubrik"">(?<text>.*?)</font>";
                var nameRegex = new Regex(namePattern, RegexOptions.Singleline);
                var nameMatches = nameRegex.Matches(input);

                var region = new RegionalWeatherResult();

                region.Image = new Uri(GetRegionalImageFromPostalCode(postalCode));

                if (nameMatches.Count >= 1)
                    region.Name = nameMatches[0].Groups["text"].Value;

                if (textMatches.Count >= 3)
                    region.Content = textMatches[2].Groups["text"].Value;

                callback(region, null);
            });
        }
        private void UpdateCurrentLocation()
        {
            if (CurrentAddress != null &&
                CurrentAddress.CountryRegion == Greenland.Name)
            {
                RegionalWeather = new RegionalWeatherResult()
                {
                    Name = Properties.Resources.Country_Greenland,
                    Content = Properties.Resources.NoRegionalForecast
                };

                Greenland.Instance.GetCityWeather(CurrentGeoCoordinate, CurrentAddress.PostalCode,
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            ThreeDaysImage = result.CityWeatherThreeDaysImage;
                            SevenDaysImage = result.CityWeatherSevenDaysImage;
                            FourteenDaysImage = result.CityWeatherFourteenDaysImage;
                        });
                    });

                Greenland.Instance.GetCountryWeather(
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            CountryWeather = result;
                            CountryImage = result.Image;
                        });
                    });
            }
            else if (CurrentAddress != null &&
                     CurrentAddress.CountryRegion == FaroeIslands.Name)
            {
                RegionalWeather = new RegionalWeatherResult()
                {
                    Name = Properties.Resources.Country_FaroeIslands,
                    Content = Properties.Resources.NoRegionalForecast
                };

                FaroeIslands.Instance.GetCityWeather(CurrentGeoCoordinate, CurrentAddress.PostalCode,
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            ThreeDaysImage = result.CityWeatherThreeDaysImage;
                            SevenDaysImage = result.CityWeatherSevenDaysImage;
                        });
                    });

                FaroeIslands.Instance.GetCountryWeather(
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            CountryWeather = result;
                            CountryImage = result.Image;
                        });
                    });
            }
            else if (CurrentAddress != null)
            {
                Denmark.Instance.GetCityWeather(CurrentGeoCoordinate, CurrentAddress.PostalCode,
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            ThreeDaysImage = result.CityWeatherThreeDaysImage;
                            SevenDaysImage = result.CityWeatherSevenDaysImage;
                            FourteenDaysImage = result.CityWeatherFourteenDaysImage;
                            FifteenDaysImage = result.CityWeatherFifteenDaysImage;
                        });
                    });

                Denmark.Instance.GetPollenData(CurrentGeoCoordinate, CurrentAddress.PostalCode,
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            PollenImage = result.Image;
                            PollenData = result;
                        });
                    });

                Denmark.Instance.GetRegionalWeather(CurrentGeoCoordinate, CurrentAddress.PostalCode,
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            RegionalWeather = result;
                            RegionalImage = result.Image;
                        });
                    });

                Denmark.Instance.GetCountryWeather(
                    (result, exception) =>
                    {
                        SmartDispatcher.BeginInvoke(() =>
                        {
                            CountryWeather = result;
                            CountryImage = result.Image;
                        });
                    });
            }
        }