Exemplo n.º 1
0
        public bool GetWeather(WebProxy useProxy = null)
        {
            if (!_isValidPlace)
            {
                return(false);
            }

            string getWeatherUrl = _getWeatherUrl + _placeWoeid;

            HttpWebRequest  webRequest  = null;
            HttpWebResponse webResponse = null;
            XmlReader       xmlReader   = null;

            YahooWeatherCondition condition = new YahooWeatherCondition();

            try
            {
                webRequest = (HttpWebRequest)HttpWebRequest.Create(getWeatherUrl);
                if (useProxy != null)
                {
                    webRequest.Proxy = useProxy;
                }

                webRequest.Timeout          = Utils.Timeout;
                webRequest.ReadWriteTimeout = Utils.Timeout;

                webResponse = (HttpWebResponse)webRequest.GetResponse();
                xmlReader   = XmlReader.Create(webResponse.GetResponseStream());
                if (xmlReader.ReadToFollowing("yweather:location"))
                {
                    YahooLocation fLocation = new YahooLocation();
                    fLocation.City     = xmlReader.GetAttribute("city");
                    fLocation.Region   = xmlReader.GetAttribute("region");
                    fLocation.Country  = xmlReader.GetAttribute("country");
                    condition.Location = fLocation;
                }
                else
                {
                    return(false);
                }

                string unitTemp, unitDistance, unitPressure, unitSpeed;
                if (xmlReader.ReadToFollowing("yweather:units"))
                {
                    unitTemp     = xmlReader.GetAttribute("temperature");
                    unitDistance = xmlReader.GetAttribute("distance");
                    unitPressure = xmlReader.GetAttribute("pressure");
                    unitSpeed    = xmlReader.GetAttribute("speed");
                }
                else
                {
                    return(false);
                }

                if (xmlReader.ReadToFollowing("yweather:wind"))
                {
                    YahooWindConditions fWind = new YahooWindConditions();
                    fWind.Chill              = xmlReader.GetAttribute("chill");
                    fWind.Direction          = double.Parse(xmlReader.GetAttribute("direction"));
                    fWind.Speed              = xmlReader.GetAttribute("speed") + " " + unitSpeed;
                    condition.WindConditions = fWind;
                }
                else
                {
                    return(false);
                }

                if (xmlReader.ReadToFollowing("yweather:atmosphere"))
                {
                    YahooAtmosphereConditions fAtmoshpere = new YahooAtmosphereConditions();
                    fAtmoshpere.Humidity = xmlReader.GetAttribute("humidity");

                    string pressureString = xmlReader.GetAttribute("pressure");
                    double pressureValue  = double.Parse(pressureString);
                    double deltaPressure  = pressureValue - 1013.25;
                    fAtmoshpere.Pressure           = pressureString + " " + unitPressure + " (" + (deltaPressure >= 0 ? "+" : string.Empty) + deltaPressure.ToString("f2") + ")";
                    fAtmoshpere.Visibility         = xmlReader.GetAttribute("visibility") + " " + unitDistance;
                    condition.AtmosphereConditions = fAtmoshpere;
                }
                else
                {
                    return(false);
                }

                if (xmlReader.ReadToFollowing("yweather:astronomy"))
                {
                    YahooAstronomy fAstronomy = new YahooAstronomy();
                    fAstronomy.SunRise  = xmlReader.GetAttribute("sunrise");
                    fAstronomy.SunSet   = xmlReader.GetAttribute("sunset");
                    condition.Astronomy = fAstronomy;
                }
                else
                {
                    return(false);
                }

                if (xmlReader.ReadToFollowing("yweather:condition"))
                {
                    condition.State       = xmlReader.GetAttribute("text");
                    condition.StateImage  = GetImageForState(xmlReader.GetAttribute("code").Trim(), useProxy);
                    condition.Temperature = xmlReader.GetAttribute("temp") + "°" + unitTemp;
                }
                else
                {
                    return(false);
                }

                ArrayList forecasts = new ArrayList();
                while (xmlReader.ReadToFollowing("yweather:forecast"))
                {
                    YahooForecast fForecast = new YahooForecast();
                    fForecast.Day             = xmlReader.GetAttribute("day");
                    fForecast.TemperatureLow  = xmlReader.GetAttribute("low") + "°";
                    fForecast.TemperatureHigh = xmlReader.GetAttribute("high") + "°";
                    fForecast.State           = xmlReader.GetAttribute("text");
                    fForecast.StateImage      = GetImageForState(xmlReader.GetAttribute("code").Trim(), useProxy);

                    forecasts.Add(fForecast);
                }

                condition.Forecasts = (YahooForecast[])forecasts.ToArray(typeof(YahooForecast));

                condition.GetTime = DateTime.Now;
                _weatherCondition = condition;

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                    xmlReader.Dispose();
                }
                if (webResponse != null)
                {
                    webResponse.Dispose();
                    webResponse.Close();
                }
                if (webRequest != null)
                {
                    webRequest.Dispose();
                }
            }
        }
        public bool GetWeather(WebProxy useProxy = null)
        {
            if (!_isValidPlace) return false;

            string getWeatherUrl = _getWeatherUrl + _placeWoeid;

            HttpWebRequest webRequest = null;
            HttpWebResponse webResponse = null;
            XmlReader xmlReader = null;

            YahooWeatherCondition condition = new YahooWeatherCondition();
            try
            {
                webRequest = (HttpWebRequest)HttpWebRequest.Create(getWeatherUrl);
                if (useProxy != null) webRequest.Proxy = useProxy;

                webRequest.Timeout = Utils.Timeout;
                webRequest.ReadWriteTimeout = Utils.Timeout;

                webResponse = (HttpWebResponse)webRequest.GetResponse();
                xmlReader = XmlReader.Create(webResponse.GetResponseStream());
                if (xmlReader.ReadToFollowing("yweather:location"))
                {
                    YahooLocation fLocation = new YahooLocation();
                    fLocation.City = xmlReader.GetAttribute("city");
                    fLocation.Region = xmlReader.GetAttribute("region");
                    fLocation.Country = xmlReader.GetAttribute("country");
                    condition.Location = fLocation;
                }
                else return false;

                string unitTemp, unitDistance, unitPressure, unitSpeed;
                if (xmlReader.ReadToFollowing("yweather:units"))
                {
                    unitTemp = xmlReader.GetAttribute("temperature");
                    unitDistance = xmlReader.GetAttribute("distance");
                    unitPressure = xmlReader.GetAttribute("pressure");
                    unitSpeed = xmlReader.GetAttribute("speed");
                }
                else return false;

                if (xmlReader.ReadToFollowing("yweather:wind"))
                {
                    YahooWindConditions fWind = new YahooWindConditions();
                    fWind.Chill = xmlReader.GetAttribute("chill");
                    fWind.Direction = double.Parse(xmlReader.GetAttribute("direction"));
                    fWind.Speed = xmlReader.GetAttribute("speed") + " " + unitSpeed;
                    condition.WindConditions = fWind;
                }
                else return false;

                if (xmlReader.ReadToFollowing("yweather:atmosphere"))
                {
                    YahooAtmosphereConditions fAtmoshpere = new YahooAtmosphereConditions();
                    fAtmoshpere.Humidity = xmlReader.GetAttribute("humidity");

                    string pressureString = xmlReader.GetAttribute("pressure");
                    double pressureValue = double.Parse(pressureString);
                    double deltaPressure = pressureValue - 1013.25;
                    fAtmoshpere.Pressure = pressureString + " " + unitPressure + " (" + (deltaPressure >= 0 ? "+" : string.Empty) + deltaPressure.ToString("f2") + ")";
                    fAtmoshpere.Visibility = xmlReader.GetAttribute("visibility") + " " + unitDistance;
                    condition.AtmosphereConditions = fAtmoshpere;
                }
                else return false;

                if (xmlReader.ReadToFollowing("yweather:astronomy"))
                {
                    YahooAstronomy fAstronomy = new YahooAstronomy();
                    fAstronomy.SunRise = xmlReader.GetAttribute("sunrise");
                    fAstronomy.SunSet = xmlReader.GetAttribute("sunset");
                    condition.Astronomy = fAstronomy;
                }
                else return false;

                if (xmlReader.ReadToFollowing("yweather:condition"))
                {
                    condition.State = xmlReader.GetAttribute("text");
                    condition.StateImage = GetImageForState(xmlReader.GetAttribute("code").Trim(), useProxy);
                    condition.Temperature = xmlReader.GetAttribute("temp") + "°" + unitTemp;
                }
                else return false;

                ArrayList forecasts = new ArrayList();
                while (xmlReader.ReadToFollowing("yweather:forecast"))
                {
                    YahooForecast fForecast = new YahooForecast();
                    fForecast.Day = xmlReader.GetAttribute("day");
                    fForecast.TemperatureLow = xmlReader.GetAttribute("low") + "°";
                    fForecast.TemperatureHigh = xmlReader.GetAttribute("high") + "°";
                    fForecast.State = xmlReader.GetAttribute("text");
                    fForecast.StateImage = GetImageForState(xmlReader.GetAttribute("code").Trim(), useProxy);

                    forecasts.Add(fForecast);
                }

                condition.Forecasts = (YahooForecast[])forecasts.ToArray(typeof(YahooForecast));

                condition.GetTime = DateTime.Now;
                _weatherCondition = condition;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlReader.Close();
                    xmlReader.Dispose();
                }
                if (webResponse != null)
                {
                    webResponse.Dispose();
                    webResponse.Close();
                }
                if (webRequest != null) webRequest.Dispose();
            }
        }