Example #1
0
        public async Task SaveWeatherMsg(List <IWeatherMsg> msglist, string name)
        {
            List <string> sqllist = new List <string>();
            int           id      = db.GetAreaIDByName(name);

            if (id == 0)
            {
                MessageBox.Show("该地区暂无天气信息");
                return;
            }
            for (int i = 0; i < msglist.Count; i++)
            {
                string sql;
                if (msglist[i].flag == 1)//小时
                {
                    WeatherHoursMsg hoursMsg = msglist[i] as WeatherHoursMsg;
                    sql =
                        String.Format(
                            "INSERT INTO HoursRainInfo ([day],[time],[temperature],[rains],[wind],[windL],[humidity],[AreaID]) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
                            hoursMsg.day, hoursMsg.time, hoursMsg.temperature, hoursMsg.rains, hoursMsg.wind, hoursMsg.windL,
                            hoursMsg.humidity, id);
                }
                else
                {
                    WeatherDayMsg dayMsg = msglist[i] as WeatherDayMsg;
                    sql =
                        String.Format(
                            "INSERT INTO DayRainInfo ([time],[maxTemp],[minTemp],[wind],[windL],[alarmmsg],[weatherStatus],[AreaID]) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
                            dayMsg.time, dayMsg.maxTemp, dayMsg.minTemp, dayMsg.wind, dayMsg.windL, dayMsg.alarmmsg,
                            dayMsg.weatherStatus, id);
                }
                sqllist.Add(sql);
            }
            db.insertToAccessByBatch(sqllist);
        }
Example #2
0
        public async Task GetWeatherByUrl(string urlcode)
        {
            WeatherMsgs = null;//先初始化为空

            /*
             * string code = db.GetCodeByName(name);
             * if (code==null)
             * {
             *  MessageBox.Show("当前地区无天气数据");
             *  return null;
             * }*/
            url = url + urlcode;
            List <IWeatherMsg> List = new List <IWeatherMsg>();
            WeatherDayMsg      dayMsg;
            HtmlWeb            web = new HtmlWeb();
            HtmlDocument       doc = web.Load(url);
            //记录每天的数据(一周)
            var htmls = doc.DocumentNode.SelectNodes("//div[@class='today']");

            do
            {
                doc   = web.Load(url);
                htmls = doc.DocumentNode.SelectNodes("//div[@class='today']");
            } while (htmls == null);

            /*   //删除注释,script,style
             * htmls.Descendants()
             *  .Where(n => n.Name == "script" || n.Name == "style" || n.Name == "#comment")
             *  .ToList().ForEach(n => n.Remove());*/
            for (int k = 0; k < htmls.Count; k++)
            {
                dayMsg = new WeatherDayMsg();
                var tr = htmls[k].ChildNodes[1].ChildNodes[1].ChildNodes;
                dayMsg.alarmmsg = doc.DocumentNode.SelectNodes("//a[@id='realWarn']").First().InnerHtml;
                if (k == 0 && tr[5].ChildNodes.Count == 3)
                {
                    dayMsg.time          = DateTime.Parse(tr[1].ChildNodes[3].InnerText.Trim());
                    dayMsg.weatherStatus = tr[5].ChildNodes[1].InnerText.Trim();
                    dayMsg.minTemp       = tr[7].ChildNodes[1].InnerText.Trim();
                    dayMsg.wind          = tr[9].ChildNodes[1].InnerText.Trim();
                    dayMsg.windL         = tr[11].ChildNodes[1].InnerText.Trim();
                    List.Add(dayMsg);
                    continue;
                }
                for (int i = 0; i < tr.Count; i++)
                {
                    switch (i)
                    {
                    case 1:
                        dayMsg.time = k < 3
                                ? DateTime.Parse(tr[i].ChildNodes[3].InnerText.Trim())
                                : DateTime.Parse(tr[i].ChildNodes[1].InnerText.Trim());
                        break;

                    case 5:
                        dayMsg.weatherStatus = tr[i].ChildNodes[1].InnerText.Trim() + " 转 " +
                                               tr[i].ChildNodes[3].InnerText.Trim();
                        break;

                    case 7:
                        dayMsg.maxTemp = tr[i].ChildNodes[1].InnerText.Trim();
                        dayMsg.minTemp = tr[i].ChildNodes[3].InnerText.Trim();
                        break;

                    case 9:
                        dayMsg.wind = tr[i].ChildNodes[1].InnerText.Trim() + " 转 " +
                                      tr[i].ChildNodes[3].InnerText.Trim();
                        break;

                    case 11:
                        dayMsg.windL = tr[i].ChildNodes[1].InnerText.Trim() + " 转 " +
                                       tr[i].ChildNodes[3].InnerText.Trim();
                        break;
                    }
                }
                List.Add(dayMsg);
            }


            //记录每隔3小时的数据
            htmls = doc.DocumentNode.SelectNodes("//*[@id='hour3']/div[@class='hour3']");
            do
            {
                doc   = web.Load(url);
                htmls = doc.DocumentNode.SelectNodes("//*[@id='hour3']/div[@class='hour3']");
            } while (htmls == null);
            //    List<WeatherHoursMsg> hoursList = new List<WeatherHoursMsg>();
            WeatherHoursMsg hoursMsg;

            for (int k = 0; k < htmls.Count; k++)
            {
                var div  = htmls[k].ChildNodes;
                int flag = 9; //临时变量 用来判断时间是否已经变了一天
                for (int j = 1; j < 9; j++)
                {
                    hoursMsg     = new WeatherHoursMsg();
                    hoursMsg.day = DateTime.Parse(div[1].ChildNodes[3].InnerText.Trim()).AddDays(k).Date;
                    if (div[1].ChildNodes[1 + j * 2].InnerText.Trim().Contains("日"))
                    {
                        hoursMsg.time =
                            hoursMsg.day.AddDays(1)
                            .AddHours(DateTime.Parse(div[1].ChildNodes[1 + j * 2].InnerText.Trim()).Hour);
                        flag = j;
                    }
                    else
                    {
                        hoursMsg.time = flag > j
                            ? DateTime.Parse(div[1].ChildNodes[1 + j * 2].InnerText.Trim()).AddDays(k)
                            : DateTime.Parse(div[1].ChildNodes[1 + j * 2].InnerText.Trim()).AddDays(k + 1);
                    }
                    hoursMsg.temperature = div[5].ChildNodes[1 + j * 2].InnerText.Trim();
                    hoursMsg.rains       = div[7].ChildNodes[1 + j * 2].InnerText.Trim().Contains("无降水")
                        ? 0.0f
                        : float.Parse(div[7].ChildNodes[1 + j * 2].InnerText.Replace("毫米", String.Empty).Trim());
                    hoursMsg.windL    = div[9].ChildNodes[1 + j * 2].InnerText.Trim();
                    hoursMsg.wind     = div[11].ChildNodes[1 + j * 2].InnerText.Trim();
                    hoursMsg.humidity = div[15].ChildNodes[1 + j * 2].InnerText.Trim();
                    List.Add(hoursMsg);
                }
            }
            WeatherMsgs = List;
        }