예제 #1
0
        string GetBigWeatherStatusString(WeatherStatus weatherStatus)
        {
            string url = "http://www.webxml.com.cn/images/weather/";

            switch (weatherStatus)
            {
            case WeatherStatus.Weizhi: url += "a_nothing.gif"; break;

            case WeatherStatus.Qing: url += "a_0.gif"; break;

            case WeatherStatus.Duoyun: url += "a_1.gif"; break;

            case WeatherStatus.Yin: url += "a_2.gif"; break;

            case WeatherStatus.Zhenyu: url += "a_3.gif"; break;

            case WeatherStatus.Leizhenyu: url += "a_4.gif"; break;

            case WeatherStatus.LeizhenyuBingpao: url += "a_5.gif"; break;

            case WeatherStatus.Yujiaxue: url += "a_6.gif"; break;

            case WeatherStatus.Xiaoyu: url += "a_7.gif"; break;

            case WeatherStatus.Zhongyu: url += "a_8.gif"; break;

            case WeatherStatus.Dayu: url += "a_9.gif"; break;

            case WeatherStatus.Baoyu: url += "a_10.gif"; break;

            case WeatherStatus.Dabaoyu: url += "a_11.gif"; break;

            case WeatherStatus.Tedabaoyu: url += "a_12.gif"; break;

            case WeatherStatus.Zhenxue: url += "a_13.gif"; break;

            case WeatherStatus.Xiaoxue: url += "a_14.gif"; break;

            case WeatherStatus.Zhongxue: url += "a_15.gif"; break;

            case WeatherStatus.Daxue: url += "a_16.gif"; break;

            case WeatherStatus.Baoxue: url += "a_17.gif"; break;

            case WeatherStatus.Wu: url += "a_18.gif"; break;

            case WeatherStatus.Dongyu: url += "a_19.gif"; break;

            case WeatherStatus.Shachenbao: url += "a_20.gif"; break;

            case WeatherStatus.XiaoyuZhuangZhongyu: url += "a_21.gif"; break;

            case WeatherStatus.ZhongyuZhuangDayu: url += "a_22.gif"; break;

            case WeatherStatus.DayuZhuangBaoyu: url += "a_23.gif"; break;

            case WeatherStatus.BaoyuZhuangDabaoyu: url += "a_24.gif"; break;

            case WeatherStatus.DabaoyuZhuangTedabaoyu: url += "a_25.gif"; break;

            case WeatherStatus.XiaoxueZhuangZhongxue: url += "a_26.gif"; break;

            case WeatherStatus.ZhongxueZhuangDaxue: url += "a_27.gif"; break;

            case WeatherStatus.DaxueZhuangBaoxue: url += "a_28.gif"; break;

            case WeatherStatus.Fuchen: url += "a_29.gif"; break;

            case WeatherStatus.Yasha: url += "a_30.gif"; break;

            case WeatherStatus.Qiangshachenbao: url += "a_31.gif"; break;

            case WeatherStatus.Xiaodaozhongyu: url += "a_21.gif"; break;

            case WeatherStatus.Zhongdaodayu: url += "a_22.gif"; break;

            case WeatherStatus.Dadaobaoyu: url += "a_23.gif"; break;

            case WeatherStatus.Xiaodaozhongxue: url += "a_26.gif"; break;

            case WeatherStatus.Zhongdaodaxue: url += "a_27.gif"; break;

            case WeatherStatus.Dadaobaoxue: url += "a_28.gif"; break;

            case WeatherStatus.Xiaozhenyu: url += "a_3.gif"; break;

            case WeatherStatus.Yintian: url += "a_2.gif"; break;

            case WeatherStatus.Mai: url += "a_18.gif"; break;

            case WeatherStatus.Wumai: url += "a_18.gif"; break;

            default: url += "a_nothing.gif"; break;
            }
            return(url);
        }
예제 #2
0
        string GetSmallWeatherStatusString(WeatherStatus weatherStatus)
        {
            string url = "http://www.webxml.com.cn/images/weather/";

            switch (weatherStatus)
            {
            case WeatherStatus.Weizhi: url += "nothing.gif"; break;

            case WeatherStatus.Qing: url += "0.gif"; break;

            case WeatherStatus.Duoyun: url += "1.gif"; break;

            case WeatherStatus.Yin: url += "2.gif"; break;

            case WeatherStatus.Zhenyu: url += "3.gif"; break;

            case WeatherStatus.Leizhenyu: url += "4.gif"; break;

            case WeatherStatus.LeizhenyuBingpao: url += "5.gif"; break;

            case WeatherStatus.Yujiaxue: url += "6.gif"; break;

            case WeatherStatus.Xiaoyu: url += "7.gif"; break;

            case WeatherStatus.Zhongyu: url += "8.gif"; break;

            case WeatherStatus.Dayu: url += "9.gif"; break;

            case WeatherStatus.Baoyu: url += "10.gif"; break;

            case WeatherStatus.Dabaoyu: url += "11.gif"; break;

            case WeatherStatus.Tedabaoyu: url += "12.gif"; break;

            case WeatherStatus.Zhenxue: url += "13.gif"; break;

            case WeatherStatus.Xiaoxue: url += "14.gif"; break;

            case WeatherStatus.Zhongxue: url += "15.gif"; break;

            case WeatherStatus.Daxue: url += "16.gif"; break;

            case WeatherStatus.Baoxue: url += "17.gif"; break;

            case WeatherStatus.Wu: url += "18.gif"; break;

            case WeatherStatus.Dongyu: url += "19.gif"; break;

            case WeatherStatus.Shachenbao: url += "20.gif"; break;

            case WeatherStatus.XiaoyuZhuangZhongyu: url += "21.gif"; break;

            case WeatherStatus.ZhongyuZhuangDayu: url += "22.gif"; break;

            case WeatherStatus.DayuZhuangBaoyu: url += "23.gif"; break;

            case WeatherStatus.BaoyuZhuangDabaoyu: url += "24.gif"; break;

            case WeatherStatus.DabaoyuZhuangTedabaoyu: url += "25.gif"; break;

            case WeatherStatus.XiaoxueZhuangZhongxue: url += "26.gif"; break;

            case WeatherStatus.ZhongxueZhuangDaxue: url += "27.gif"; break;

            case WeatherStatus.DaxueZhuangBaoxue: url += "28.gif"; break;

            case WeatherStatus.Fuchen: url += "29.gif"; break;

            case WeatherStatus.Yasha: url += "30.gif"; break;

            case WeatherStatus.Qiangshachenbao: url += "31.gif"; break;

            case WeatherStatus.Xiaodaozhongyu: url += "21.gif"; break;

            case WeatherStatus.Zhongdaodayu: url += "22.gif"; break;

            case WeatherStatus.Dadaobaoyu: url += "23.gif"; break;

            case WeatherStatus.Xiaodaozhongxue: url += "26.gif"; break;

            case WeatherStatus.Zhongdaodaxue: url += "27.gif"; break;

            case WeatherStatus.Dadaobaoxue: url += "28.gif"; break;

            case WeatherStatus.Xiaozhenyu: url += "3.gif"; break;

            case WeatherStatus.Yintian: url += "2.gif"; break;

            case WeatherStatus.Mai: url += "18.gif"; break;

            case WeatherStatus.Wumai: url += "18.gif"; break;

            default: url += "nothing.gif"; break;
            }
            return(url);
        }
예제 #3
0
        public void HandleWeather()
        {
            using (WebClient webClient = new WebClient()
            {
                Encoding = Encoding.UTF8
            })
            {
                string url = $"http://www.weather.com.cn/weather/";
                if (city.ID == "00")
                {
                    url += $"{province.ID}{district.ID}{city.ID}.shtml";
                }
                else if (district.ID.Length > 2)
                {
                    url += $"{district.ID}.shtml";
                }
                else
                {
                    url += $"{province.ID}{city.ID}{district.ID}.shtml";
                }

                byte [] contentBytes = webClient.DownloadData(url);
                using (MemoryStream stream = new MemoryStream(contentBytes))
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        string          content = reader.ReadToEnd();
                        MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline);
                        if (matches.Count > 0)
                        {
                            matches = Regex.Matches(matches[0].Value, "<li class=\"sky skyid.*?</li>", RegexOptions.Singleline);
                            if (matches.Count == 7)
                            {
                                List <string>        dayList    = new List <string>();
                                List <string>        infoList   = new List <string>();
                                List <string>        tempList   = new List <string>();
                                List <string>        windList   = new List <string>();
                                List <WeatherStatus> statusList = new List <WeatherStatus>();
                                MatchCollection      matches_temp;
                                foreach (Match match in matches)
                                {
                                    //日期:31日(今日)
                                    matches_temp = Regex.Matches(match.Value, "<h1>.*?</h1>");
                                    if (matches_temp.Count > 0)
                                    {
                                        dayList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    else
                                    {
                                        dayList.Add("");
                                    }
                                    //天气信息:晴
                                    matches_temp = Regex.Matches(match.Value, "<p.*class=\"wea\".*?</p>");
                                    if (matches_temp.Count > 0)
                                    {
                                        infoList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                        WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                        if (status == WeatherStatus.Weizhi)
                                        {
                                            status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                        }
                                        statusList.Add(status);
                                    }
                                    else
                                    {
                                        dayList.Add("");
                                        statusList.Add(WeatherStatus.Weizhi);
                                    }
                                    //温度信息:4/-1℃
                                    //风力信息:3-4级转3级
                                    matches_temp = Regex.Matches(match.Value, "<span>.*?</span>");
                                    string temperature = "";
                                    string wind        = "";
                                    if (matches_temp.Count > 0)
                                    {
                                        temperature = RemoveAngleBrackets(matches_temp[0].Value) + "℃";
                                    }
                                    matches_temp = Regex.Matches(match.Value, "<i>.*?</i>");
                                    if (matches_temp.Count >= 1)
                                    {
                                        temperature = string.IsNullOrEmpty(temperature) ? RemoveAngleBrackets(matches_temp[0].Value) : $"{temperature}/{RemoveAngleBrackets(matches_temp[0].Value)}";
                                    }
                                    if (matches_temp.Count >= 2)
                                    {
                                        wind = RemoveAngleBrackets(matches_temp[1].Value);
                                    }
                                    tempList.Add(temperature);
                                    windList.Add(wind);
                                }
                                this.Day_1To7           = dayList.ToArray();
                                this.Info_1To7          = infoList.ToArray();
                                this.Temperature_1To7   = tempList.ToArray();
                                this.Wind_1To7          = windList.ToArray();
                                this.WeatherStatus_1To7 = statusList.ToArray();
                            }
                        }
                    }

                //More
                url = $"http://www.weather.com.cn/weather15d/";
                if (city.ID == "00")
                {
                    url += $"{province.ID}{district.ID}{city.ID}.shtml";
                }
                else if (district.ID.Length > 2)
                {
                    url += $"{district.ID}.shtml";
                }
                else
                {
                    url += $"{province.ID}{city.ID}{district.ID}.shtml";
                }

                contentBytes = webClient.DownloadData(url);
                using (MemoryStream stream = new MemoryStream(contentBytes))
                    using (StreamReader reader = new StreamReader(stream))
                    //----
                    {
                        string          content = reader.ReadToEnd();
                        MatchCollection matches = Regex.Matches(content, "<ul class=\"t clearfix\">.*?</ul>", RegexOptions.Singleline);
                        if (matches.Count > 0)
                        {
                            matches = Regex.Matches(matches[0].Value, "<li.*?</li>", RegexOptions.Singleline);
                            if (matches.Count == 8)
                            {
                                List <string>        dayList    = new List <string>();
                                List <string>        infoList   = new List <string>();
                                List <string>        tempList   = new List <string>();
                                List <string>        wind1List  = new List <string>();
                                List <string>        wind2List  = new List <string>();
                                List <WeatherStatus> statusList = new List <WeatherStatus>();
                                MatchCollection      matches_temp;
                                foreach (Match match in matches)
                                {
                                    //日期:31日(今日)
                                    matches_temp = Regex.Matches(match.Value, "<span class=\"time\">.*?</span>");
                                    if (matches_temp.Count > 0)
                                    {
                                        dayList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    else
                                    {
                                        dayList.Add("");
                                    }
                                    //天气信息:晴
                                    matches_temp = Regex.Matches(match.Value, "<span class=\"wea\">.*?</span>");
                                    if (matches_temp.Count > 0)
                                    {
                                        infoList.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                        WeatherStatus status = GetWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                        if (status == WeatherStatus.Weizhi)
                                        {
                                            status = GetUnkownWeatherStatus(RemoveAngleBrackets(matches_temp[0].Value));
                                        }
                                        statusList.Add(status);
                                    }
                                    else
                                    {
                                        dayList.Add("");
                                        statusList.Add(WeatherStatus.Weizhi);
                                    }
                                    //温度信息:4/-1℃
                                    //风力信息:3-4级转3级
                                    matches_temp = Regex.Matches(match.Value, "<span class=\"tem\".*?</span>");
                                    if (matches_temp.Count > 0)
                                    {
                                        tempList.Add(RemoveAngleBrackets(RemoveAngleBrackets(matches_temp[0].Value)));
                                    }
                                    else
                                    {
                                        tempList.Add("");
                                    }

                                    matches_temp = Regex.Matches(match.Value, "<span class=\"wind\">.*?</span>");
                                    if (matches_temp.Count > 0)
                                    {
                                        wind1List.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    else
                                    {
                                        wind1List.Add("");
                                    }

                                    matches_temp = Regex.Matches(match.Value, "<span class=\"wind1\">.*?</span>");
                                    if (matches_temp.Count > 0)
                                    {
                                        wind2List.Add(RemoveAngleBrackets(matches_temp[0].Value));
                                    }
                                    else
                                    {
                                        wind2List.Add("");
                                    }
                                }
                                this.Day_7To15           = dayList.ToArray();
                                this.Info_7To15          = infoList.ToArray();
                                this.Temperature_7To15   = tempList.ToArray();
                                this.Wind1_7To15         = wind1List.ToArray();
                                this.Wind2_7To15         = wind2List.ToArray();
                                this.WeatherStatus_7To15 = statusList.ToArray();
                            }
                        }
                    }
                //----
            }
        }