コード例 #1
0
ファイル: HtmlService.cs プロジェクト: AwkEng/UtahSnowReport
        public List <TPlus24hrData> GetTPlus24HrData()
        {
            var htmlDoc       = new HtmlDocument();
            var chromeOptions = new ChromeOptions();

            chromeOptions.AddArguments("headless");
            using (var browser = new ChromeDriver(chromeOptions))
            {
                browser.Url = @"https://www.weather.gov/slc/winter#tab-2";
                var source = browser.PageSource;
                htmlDoc.LoadHtml(source);
            }

            //find snowtable node
            var snowTableNode = htmlDoc.GetElementbyId(WeatherGov_SnowTable_Id);

            //find timestamp node
            var timeStampNode  = htmlDoc.GetElementbyId(WeatherGov_Timestamp_Id);
            var timeStampWords = timeStampNode.InnerText.Split(" ");

            TPlus24hrData.RangeStartTime = DateTime.TryParse(timeStampWords[0] + " 05:00:00 AM", out DateTime timestamp1) ? timestamp1 : DateTime.UnixEpoch;
            TPlus24hrData.RangeStopTime  = DateTime.TryParse(timeStampWords[3] + " 05:00:00 AM", out DateTime timestamp2) ? timestamp2 : DateTime.UnixEpoch;

            List <TPlus24hrData> data = new List <TPlus24hrData>();

            foreach (KeyValuePair <string, string> resort_value in WeatherGov_Resorts_Values)
            {
                TPlus24hrData datum = new TPlus24hrData
                {
                    ResortName = resort_value.Key
                };

                //find resort node
                var resortNode = snowTableNode.Descendants("td").Where(d => d.InnerText == resort_value.Value).First();

                //find 24 hr snow data
                var    snow24Node = resortNode.NextSibling.NextSibling;
                string snow24Text = snow24Node.InnerHtml;
                datum.Snow24hr_in = int.TryParse(snow24Text, out int temp1) ? temp1 : 0;

                //timestamp this sample
                datum.SampledTime = DateTime.Now;

                data.Add(datum);
            }

            return(data);
        }
コード例 #2
0
        public string ToHtml()
        {
            var builder = new StringBuilder();

            builder.Append("<html>");
            builder.AppendLine();
            builder.Append("<head>");
            builder.AppendLine();
            builder.Append("<style>");
            builder.AppendLine();
            builder.Append("table, th, td {");
            builder.AppendLine();
            builder.Append("border: 1px solid black;");
            builder.AppendLine();
            builder.Append("border-collapse: collapse;");
            builder.AppendLine();
            builder.Append("}");
            builder.AppendLine();
            builder.Append("</style>");
            builder.AppendLine();
            builder.Append("</head>");
            builder.AppendLine();
            builder.Append("<body>");
            builder.AppendLine();

            builder.Append("<h2>");
            builder.AppendLine();
            builder.Append(TimeStamp.ToString());
            builder.Append("</h2>");
            builder.AppendLine();

            builder.AppendLine();
            builder.AppendLine();

            builder.Append(TMinus24hrData.HtmlHeader());
            foreach (var tMinusData in TMinus24HrData)
            {
                if (tMinusData.IsValid())
                {
                    builder.Append(tMinusData.ToHtml());
                }
            }
            builder.Append(TMinus24hrData.HtmlFooter());

            builder.AppendLine();
            builder.AppendLine();

            builder.Append(TPlus24hrData.HtmlHeader());
            foreach (var tPlusData in TPlus24HrData)
            {
                if (tPlusData.IsValid())
                {
                    builder.Append(tPlusData.ToHtml());
                }
            }
            builder.Append(TPlus24hrData.HtmlFooter());

            builder.Append("</body></html>");

            return(builder.ToString());
        }