示例#1
0
        private static Post GetPost(string postId)
        {
            HtmlDocument document = new HtmlWeb().Load("http://thecodinglove.com/post/" + postId);

            HtmlNode someNode = document.GetElementbyId("main");

            var post = new Post();

            if (someNode != null)
            {
                var h3 = someNode.SelectNodes("div[2]/div/h3");

                var innerText = h3.First().InnerText;
                post.Title = innerText;
                Console.WriteLine(innerText);

                var img = someNode.SelectNodes("div[2]/div[2]/p/p/img");

                var src = img.First().Attributes["src"].Value;
                post.ImageUrl = src;
                Console.WriteLine("src: " + src);
            }

            return post;
        }
示例#2
0
        public void ReadPower()
        {
            try {
                var doc = new HtmlWeb().Load(_currentIP);
                if (doc == null) {
                    _logger.Log("The page was not found!", LogType.ERROR);
                    _ipFinder.FindIP();
                    ReadingCancelled(this, new EventArgs());
                    return;
                }

                var element = doc.GetElementbyId("Power");
                if (element == null) {
                    _logger.Log("The \"Power\" element was not found!", LogType.ERROR);
                    ReadingCancelled(this, new EventArgs());
                    return;
                }

                var power = element.InnerHtml;
                if (String.IsNullOrEmpty(power)) {
                    _logger.Log("Power value is empty!", LogType.ERROR);
                    ReadingCancelled(this, new EventArgs());
                    return;
                }

                power = power.Trim();
                if (power.ToLowerInvariant().Contains("kw")) {
                    power = Regex.Replace(power, @" .*", "");
                    power = Regex.Replace(power, @"\.", ",");

                    float kwPower;
                    if (float.TryParse(power, out kwPower)) {
                        kwPower *= 1000;
                        PowerChanged(new PowerChangedEventArgs((int) kwPower));
                    } else {
                        Logger.Instance.Value.Log("Cannot convert the power to float - " + power, LogType.ERROR);
                    }
                } else {
                    power = Regex.Replace(power, @"[^\d]", "");

                    int intPower;
                    if (int.TryParse(power, out intPower)) {
                        PowerChanged(new PowerChangedEventArgs(intPower));
                    } else {
                        Logger.Instance.Value.Log("Cannot convert the power to int - " + power, LogType.ERROR);
                    }
                }
            } catch (Exception e) {
                _logger.Log("Exception occured: " + e.Message + Environment.NewLine + e.StackTrace, LogType.ERROR);
                _ipFinder.FindIP();
                ReadingCancelled(this, new EventArgs());
            }
        }