Exemplo n.º 1
0
        public Weather GetWeather()
        {
            Weather weather = new Weather();

            try
            {
                string weatherNow = "", weatherToday = "";

                HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
                string page = MainModel.GetHtmlPageText("https://yandex.ru" + Href);
                if (page != null)
                {
                    html.LoadHtml(page);
                    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(page);

                    HtmlNode cityCollection = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'time fact__time')]");
                    string   timeNow        = cityCollection.InnerText;
                    cityCollection = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'temp fact__temp')]/*[contains(@class,'temp__value')]");
                    string tempNow = cityCollection.InnerText;
                    cityCollection = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'fact__props')]/*[contains(@class,'wind-speed')]");
                    string windSpeed = cityCollection.InnerText;
                    cityCollection = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'fact__props')]/*[contains(@class,'term term_orient_v fact__pressure')]/*[contains(@class,'term__value')]");
                    string pressure = cityCollection.InnerText;
                    cityCollection = doc.DocumentNode.SelectSingleNode("//*[contains(@class,'fact__props')]/*[contains(@class,'term term_orient_v fact__humidity')]/*[contains(@class,'term__value')]");
                    string humidity = cityCollection.InnerText;

                    HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//*[contains(@class,'sunrise-sunset__value')]");
                    string             sun_rise   = collection[0]?.InnerText;
                    string             sunset     = collection[1]?.InnerText;


                    weatherNow = timeNow + String.Format("\nТемпература: {0} \n", tempNow) +
                                 String.Format("{0}\n", windSpeed) +
                                 String.Format("Влажность: {0}\n", humidity) +
                                 String.Format("Давление: {0}\n", pressure) +
                                 String.Format("Восход: {0} Закат: {1}", sun_rise, sunset);
                }


                /*if (this.Id != 0)
                 * {
                 *   string weatherNow = "", weatherToday = "";
                 *   String URLString = "https://export.yandex.ru/bar/reginfo.xml?region=" + this.Id.ToString() + ".xml";
                 *   XDocument docx = XDocument.Load(URLString);
                 *   IEnumerable<XElement> elem = docx.Root.Elements().Descendants("day_part");
                 *   string temp = "";
                 *   //if (elem.Elements().Count() > 0)
                 *       temp = elem.ElementAt(0).Descendants("temperature").SingleOrDefault()?.Value.ToString();
                 *   string windDir = docx.Root.Elements().Descendants("wind_direction").SingleOrDefault()?.Value.ToString();
                 *   string windSpeed = docx.Root.Elements().Descendants("wind_speed").SingleOrDefault()?.Value.ToString();
                 *   string dampness = docx.Root.Elements().Descendants("dampness").SingleOrDefault()?.Value.ToString();
                 *   string pressure = docx.Root.Elements().Descendants("torr").SingleOrDefault()?.Value.ToString();
                 *   string sun_rise = docx.Root.Elements().Descendants("sun_rise").SingleOrDefault()?.Value.ToString();
                 *   string sunset = docx.Root.Elements().Descendants("sunset").SingleOrDefault()?.Value.ToString();
                 *
                 *   weatherNow = String.Format("Температура: {0} \n", temp) +
                 *                   String.Format("Ветер: {0}, {1} м/сек\n", windDir, windSpeed) +
                 *                   String.Format("Влажность: {0}%\n", dampness) +
                 *                   String.Format("Давление: {0} мм рт.ст.\n", pressure) +
                 *                   String.Format("Восход: {0} Закат: {1}", sun_rise, sunset);
                 *
                 *   IEnumerable<XElement> elemtoday = docx.Root.Elements().Descendants("day_part");
                 *   for (int i = 1; i < elemtoday.Count(); i++)
                 *   {
                 *       XElement daypart = elemtoday.ElementAt(i);
                 *
                 *       string name = daypart.Attribute("type").Value.ToUpper();
                 *       string tempdaypart = daypart.Descendants("temperature").SingleOrDefault()?.Value.ToString();
                 *       if (tempdaypart == null)
                 *       {
                 *           tempdaypart = daypart.Descendants("temperature_from").SingleOrDefault()?.Value.ToString() + daypart.Descendants("temperature_to").SingleOrDefault()?.Value.ToString();
                 *       }
                 *       weatherToday += String.Format("{0}: {1}\n", name, tempdaypart);
                 *   }
                 *
                 *   weatherNow = String.Format("Температура: {0} \n", temp) +
                 *                   String.Format("Ветер: {0}, {1} м/сек\n", windDir, windSpeed) +
                 *                   String.Format("Влажность: {0}%\n", dampness) +
                 *                   String.Format("Давление: {0} мм рт.ст.\n", pressure) +
                 *                   String.Format("Восход: {0} Закат: {1}", sun_rise, sunset);
                 *
                 *
                 *   weather.Now = weatherNow;
                 *   weather.Today = weatherToday;
                 * }*/
                weather.Now   = weatherNow;
                weather.Today = weatherToday;
            }
            catch (Exception e)
            {
                weather.Now   = "Для выбранного города нет данных";
                weather.Today = "";
            }
            return(weather);
        }
Exemplo n.º 2
0
 public ViewModel()
 {
     m                  = new MainModel();
     CityList           = m.Tree;
     m.PropertyChanged += M_PropertyChanged;
 }