Esempio n. 1
0
        public void get_current_weather(WeatherInfo w)
        {
            string current = _sitereader.current();

            if (current == null)
            {
                throw new Exception("incorrect current weather structure ");
            }

            XmlDocument pg = new XmlDocument();

            pg.LoadXml(current);

            XmlNode pgd_current = pg.DocumentElement;

            // weather character
            string wt = get_weather_type(pgd_current, "./div/a//img");

            w.WeatherType = weather_type_encoding.Keys.Contains(wt) ? weather_type_encoding[wt] : WeatherType.Undefined;

#if STATISTICS
            if (!stat_weather_type.Keys.Contains(wt))
            {
                class_name = "link__condition";
                XmlNode weather_type = pgd_current.SelectSingleNode($"./div/a/div/div[starts-with(@class, '{class_name}')]");
                if (weather_type == null)
                {
                    throw new Exception("incorrect current weather structure-- cant find weather type string");
                }

                string wts = weather_type.InnerText;
                write_statistics(wt, wts);
            }
#endif
            // air temperature
            w.TemperatureHigh = w.TemperatureLow = get_air_temperature(pgd_current, "./div/a//div/span[@class = 'temp__value temp__value_with-unit']");

            // wind
            w.WindSpeed = null;
            XmlNode wind_speed = pgd_current.SelectSingleNode($"./div[@class = 'fact__props']//span[@class = 'wind-speed']");
            if (wind_speed == null)
            {
                throw new Exception("incorrect current weather structure -- cant find wind speed");
            }

            if (double.TryParse(wind_speed.InnerText.Replace(',', '.').Replace('−', '-').Trim(),
                                NumberStyles.Number, new CultureInfo("en"), out double windspeed))
            {
                w.WindSpeed = windspeed;
            }

            w.WindDirection = WindDirection.Undefined;
            XmlNode wind_dir = pgd_current.SelectSingleNode($"./div[@class = 'fact__props']//span[@class = 'fact__unit']/abbr");
            if (wind_dir == null)
            {
                throw new Exception("incorrect current weather structure -- cant find wind direction");
            }

            string wind_dir_name = wind_dir.InnerText;
            if (wind_direction_encoding.Keys.Contains(wind_dir_name))
            {
                w.WindDirection = wind_direction_encoding[wind_dir_name];
            }

            // humidity
            w.Humidity = null;
            XmlNode humidity = pgd_current.SelectSingleNode($"./div[@class = 'fact__props']//div[@class = 'term term_orient_v fact__humidity']/div[@class='term__value']");
            if (humidity == null)
            {
                throw new Exception("incorrect current weather structure -- cant find humidity");
            }

            if (double.TryParse(humidity.InnerText.Replace(',', '.').Replace('−', '-').Replace('%', ' ').Trim(),
                                NumberStyles.Number, new CultureInfo("en"), out double hum))
            {
                w.Humidity = hum;
            }

            // pressure
            w.Pressure = null;
            XmlNode pressure = pgd_current.SelectSingleNode($"./div[@class = 'fact__props']//div[@class = 'term term_orient_v fact__pressure']");
            if (pressure == null)
            {
                throw new Exception("incorrect current weather structure -- cant find pressure");
            }

            if (double.TryParse(pressure.InnerText.Split(' ')[0].Replace(',', '.').Replace('−', '-').Trim(),
                                NumberStyles.Number, new CultureInfo("en"), out double press))
            {
                w.Pressure = press;
            }

            // hourly temperature forecast
            var hour_spans = pgd_current.SelectNodes("//span[@class = 'fact__hour-elem']");
            if (hour_spans.Count == 0)
            {
                hour_spans = pgd_current.SelectNodes("//div[@class = 'fact__hour-elem']");
            }

            foreach (XmlNode hour_span in hour_spans)
            {
                wt = get_weather_type(hour_span, "./img");
                if (!wt.Equals("sunset") && !wt.Equals("sunrise"))
                {
                    ShortWeatherInfo swi  = new ShortWeatherInfo();
                    double?          temp = get_air_temperature(hour_span, "./div[@class = 'fact__hour-temp']");

                    XmlNode hour = hour_span.SelectSingleNode($"./div[@class = 'fact__hour-label']");
                    if (hour != null && temp.HasValue)
                    {
                        if (int.TryParse(hour.InnerText.Trim().Split(':')[0],
                                         NumberStyles.Number, new CultureInfo("en"), out int hr))
                        {
                            w.HourlyWeather.Add(new ShortWeatherInfo()
                            {
                                Hour        = hr,
                                Temperature = temp.Value,
                                WeatherType = weather_type_encoding.Keys.Contains(wt) ? weather_type_encoding[wt] : WeatherType.Undefined
                            });
                        }
                    }
                }

#if STATISTICS
                if (!wt.Equals("sunset") && !wt.Equals("sunrise") && !stat_weather_type.Keys.Contains(wt))
                {
                    string wts = hour_span.SelectSingleNode("@aria-label").Value;
                    wts = wts.Substring(0, wts.IndexOf(','));
                    for (int i = 0; i < 3; i++)
                    {
                        wts = wts.Substring(wts.IndexOf(' ') + 1);
                    }

                    write_statistics(wt, wts);
                }
#endif
            }
        }
Esempio n. 2
0
        private void read_ngs_current_weather(WeatherInfo w)
        {
            bool success = true;

            _succeeded = true;

            try
            {
                string current = _sitereader.current();
                if (current == null)
                {
                    throw new Exception("incorrect current weather structure ");
                }

                XmlDocument pg = new XmlDocument();
                pg.LoadXml(current);

                XmlNode pgd_current = pg.DocumentElement;

                // weather character
                string  class_name   = "icon-weather-big ";
                XmlNode icon_weather = pgd_current.SelectSingleNode(string.Format("./div/div[starts-with(@class, '{0}')]", class_name));
                if (icon_weather == null)
                {
                    throw new Exception("incorrect current weather structure-- cant find weather type icon");
                }

                string wt = icon_weather.SelectSingleNode("@class").Value.Substring(class_name.Length);
                w.WeatherType = weather_type_encoding.Keys.Contains(wt) ? weather_type_encoding[wt] : WeatherType.Undefined;

                XmlNode today = pgd_current.SelectSingleNode("./div[@class = 'today-panel__info__main']");
                if (today == null)
                {
                    throw new Exception("incorrect current weather structure -- cant find today weather panel");
                }

                XmlNode curr = today.SelectSingleNode("./div[starts-with(@class, 'today-panel__info__main__item')]");
                if (curr == null)
                {
                    throw new Exception("incorrect current weather structure -- cant find weather panel");
                }

                // temperature
                XmlNode temp = curr.SelectSingleNode("./div/span/span[@class = 'value__main']");
                if (temp == null)
                {
                    throw new Exception("incorrect current weather structure -- cant find current temperature");
                }

                string st = temp.InnerText.Trim().Replace(',', '.').Replace('−', '-');
                if (string.IsNullOrEmpty(st))
                {
                    throw new Exception("incorrect current weather structure -- incorrect current temperature string");
                }

                double t = double.Parse(st, new CultureInfo("en"));
                w.TemperatureHigh = w.TemperatureLow = t;

                //File.WriteAllText(@"D:\Projects\YetAnotherPictureSlideshow\PictureSlideshowScreensaver\samples\XMLFile2.xml", curr.OuterXml);

                // wind
                string  cn = "icon-small icon-wind-";
                XmlNode ei = curr.SelectSingleNode(string.Format("./dl/dd/i[starts-with(@class, '{0}')]", cn));
                if (ei == null)
                {
                    throw new Exception("incorrect current weather structure -- cant find wind direction");
                }

                string wd = ei.SelectSingleNode("@class").Value.Substring(cn.Length);
                w.WindDirection = wind_direction_encoding.Keys.Contains(wd) ? wind_direction_encoding[wd] : WindDirection.Undefined;

                XmlNode edt = ei.ParentNode.NextSibling;
                if (edt == null || edt.Name.ToLower() != "dt")
                {
                    throw new Exception("incorrect structure -- 2.1");
                }

                string wind = edt.InnerText.TrimStart('\n', '\r', '\t', ' ');
                double ws;
                if (double.TryParse(wind.Substring(0, wind.IndexOf(' ')), NumberStyles.Number, new CultureInfo("en"), out ws))
                {
                    w.WindSpeed = ws;
                }

                // pressure
                ei = curr.SelectSingleNode("./dl/dd/i[@class = 'icon-small icon-pressure']");
                if (ei == null || ei.Name.ToLower() != "i")
                {
                    throw new Exception("incorrect structure -- 2.2");
                }

                double p;
                string pr = ei.SelectSingleNode("@title").Value;
                if (double.TryParse(pr.Substring(0, pr.IndexOf(' ')), NumberStyles.Number, new CultureInfo("ru"), out p))
                {
                    w.Pressure = p;
                }

                // humidity
                ei = curr.SelectSingleNode("./dl/dd/i[@class = 'icon-small icon-humidity']");
                if (ei == null || ei.Name.ToLower() != "i")
                {
                    throw new Exception("incorrect structure -- 2.3");
                }

                double h;
                string humidity = ei.SelectSingleNode("@title").Value;
                if (double.TryParse(humidity.Substring(0, humidity.IndexOf('%')), NumberStyles.Number, new CultureInfo("ru"), out h))
                {
                    w.Humidity = h;
                }
            }
            catch (Exception e)
            {
                success      = false;
                _error_descr = e.Message;

                string fname = string.Format(@"d:\LOG\{0} -- {1}", DateTime.Now.ToString("yyyy_MM_dd HH-mm-ss"), _error_descr);
            }

            finally
            {
                if (!success)
                {
                    lock (_locker)
                    {
                        _succeeded = false;
                    }
                }
            }
        }