Пример #1
0
 public WeatherData()
 {
     Location     = new LocationData();
     Curent       = new ForecastData();
     ForecastList = new List <ForecastData>();
 }
Пример #2
0
        public static WeatherData GetWeatherReport(CultureInfo culture, LocationData location, TemperatureScale tempScale)
        {
            var url = string.Format(tempScale == TemperatureScale.Celsius ? RequestForCelsius : RequestForFahrenheit, culture.Name, location.Code);

            XDocument doc;

            try {
                doc = XDocument.Load(url);
            }
            catch (Exception) {
                return(null);
            }

            var weather = from x in doc.Descendants("weather")
                          let xElement = x.Element("current")
                                         where xElement != null
                                         select new {
                feelslike  = xElement.Attribute("feelslike").Value,
                windspeed  = xElement.Attribute("windspeed").Value,
                humidity   = xElement.Attribute("humidity").Value,
                temp       = xElement.Attribute("temperature").Value,
                text       = xElement.Attribute("skytext").Value,
                skycode    = xElement.Attribute("skycode").Value,
                windstring = xElement.Attribute("winddisplay").Value
            };

            var result         = new WeatherData();
            var currentWeather = weather.FirstOrDefault();

            if (currentWeather == null)
            {
                return(null);
            }

            var t = 0;

            int.TryParse(currentWeather.temp, out t);
            result.Temperature = t;

            int.TryParse(currentWeather.feelslike, out t);
            result.FeelsLike = t;

            result.Curent = new ForecastData()
            {
                Text = currentWeather.text,
                //SkyCode = GetWeatherPic(Convert.ToInt32(currentWeather.skycode), 4, 22)
            };

            result.Location = location;

            if (doc.Descendants("weather").FirstOrDefault() == null)
            {
                return(null);
            }

            var locString = doc.Descendants("weather").FirstOrDefault().Attribute("weatherlocationname").Value;

            if (locString.Contains(","))
            {
                result.Location.City    = locString.Substring(0, locString.IndexOf(","));
                result.Location.Country = locString.Substring(locString.IndexOf(",") + 2);
            }
            else
            {
                result.Location.City = locString;
            }

            //parse coordinates
            var coords = from x in doc.Descendants("weather")
                         select new {
                lon = x.Attribute("long").Value,
                lat = x.Attribute("lat").Value
            };

            double lat, lon = double.MinValue;

            double.TryParse(coords.FirstOrDefault().lat, NumberStyles.Float, CultureInfo.GetCultureInfo("en-US").NumberFormat, out lat);
            double.TryParse(coords.FirstOrDefault().lon, NumberStyles.Float, CultureInfo.GetCultureInfo("en-US").NumberFormat, out lon);

            result.Location.Lat = lat;
            result.Location.Lon = lon;

            var sunrise = DateTime.Today.AddHours(4);
            var sunset  = DateTime.Today.AddHours(22);

            result.Curent.SkyCode  = GetWeatherPic(Convert.ToInt32(currentWeather.skycode));
            result.Curent.Humidity = int.Parse(currentWeather.humidity);

            //parse forecast
            var forecastUrl = string.Format(tempScale == TemperatureScale.Celsius ? ForecastUrlForCelsius : ForecastUrlForFahrenheit, location.Code);

            var f = new List <ForecastData>();

            foreach (var day in doc.Descendants("forecast"))
            {
                f.Add(new ForecastData());
                var temp = 0;
                int.TryParse(day.Attribute("high").Value, out temp);
                f[f.Count - 1].HighTemperature = temp;
                int.TryParse(day.Attribute("low").Value, out temp);
                f[f.Count - 1].LowTemperature = temp;
                f[f.Count - 1].Text           = day.Attribute("skytextday").Value;
                f[f.Count - 1].SkyCode        = GetWeatherPic(Convert.ToInt32(day.Attribute("skycodeday").Value));
                f[f.Count - 1].Humidity       = -1;
                try { f[f.Count - 1].Precip = int.Parse(day.Attribute("precip").Value); }
                catch { f[f.Count - 1].Precip = -1; }
                f[f.Count - 1].Url = forecastUrl;
                f[f.Count - 1].Day = day.Attribute("day").Value;
            }

            if (f.Count > 0)
            {
                result.Curent.HighTemperature = f[0].HighTemperature;
                result.Curent.LowTemperature  = f[0].LowTemperature;
                result.Curent.Precip          = f[0].Precip;
            }

            result.ForecastList = f;

            return(result);
        }