天气时段规定
//var weather = weatherService.Search("http://m.weather.com.cn/data/101010100.html"); string temp = string.Empty;//温度 string desct = string.Empty;//气象文字描述 string pic = string.Empty;//气象图片描述 string humi = string.Empty;//湿度 string copy = string.Empty;//信息版权 DateTime nowTime = DateTime.Now;
var weatherTime = weatherService.Search(string.Format("http://www.weather.com.cn/data/cityinfo/{0}.html", regionCode));//实时天气接口 var dr = JObject.Parse(weatherTime); if (dr["weatherinfo"] != null) { var firstDayTime = DateTime.Parse(string.Format("{0:yyyy-MM-dd 08:00:00}", nowTime));//今日白天08:00到今日晚上20:00 计12个小时 var firstLightTime = DateTime.Parse(string.Format("{0:yyyy-MM-dd 20:00:00}", nowTime)); var secondFirstTime = DateTime.Parse(string.Format("{0:yyyy-MM-dd 08:00:00}", nowTime.AddDays(1)));//今天夜间20:00到次日早上08:00 计12个小时
var img = string.Empty; if ((DateTime.Compare(firstDayTime, nowTime) < 0) && (DateTime.Compare(nowTime, firstLightTime) <= 0))//白天时段 { temp = DataConvert.ToString(dr["weatherinfo"]["temp1"], string.Empty);
var aImg = dr["weatherinfo"]["img1"].ToString(); var bImg = aImg.Split('.'); var cImg = bImg[0].ToLower();
if (cImg.Contains("d")) { img = cImg.Substring(cImg.IndexOf("d") + 1); } else if (cImg.Contains("n")) { img = cImg.Substring(cImg.IndexOf("n") + 1); } Util.LogTemp(String.Format("白天时段:{0:yyyy-MM-dd HH:mm:ss} temper:{1}", DateTime.Now, temp)); } else if ((DateTime.Compare(firstLightTime, nowTime) < 0) && (DateTime.Compare(nowTime, secondFirstTime) <= 0))//晚上时段 { temp = DataConvert.ToString(dr["weatherinfo"]["temp2"],string.Empty); var mImg = dr["weatherinfo"]["img2"].ToString(); var nImg = mImg.Split('.'); var dImg = nImg[0].ToLower();
if (dImg.Contains("d")) { img = dImg.Substring(dImg.IndexOf("d") + 1); } else if (dImg.Contains("n")) { img = dImg.Substring(dImg.IndexOf("n") + 1); } Util.LogTemp(String.Format("晚上时段:{0:yyyy-MM-dd HH:mm:ss} temper:{1}", DateTime.Now, temp)); } desct = DataConvert.ToString(dr["weatherinfo"]["weather"], string.Empty); pic = string.Format("http://m.weather.com.cn/img/b{0}.gif", img); }
var weatherSk = weatherService.Search(string.Format("http://www.weather.com.cn/data/sk/{0}.html", regionCode));//简要天气接口 var ds = JObject.Parse(weatherSk); if (ds["weatherinfo"] != null) { humi = DataConvert.ToString(ds["weatherinfo"]["SD"], string.Empty); } copy = "数据来源:中国环境保护部 更新于" + string.Format("{0}年{1}月{2}日 {3}:{4}", nowTime.Year, nowTime.Month, nowTime.Day, nowTime.Hour < 10 ? "0" + nowTime.Hour.ToString() : nowTime.Hour.ToString(), nowTime.Minute < 10 ? "0" + nowTime.Minute.ToString() : nowTime.Minute.ToString()); return new { humidity = humi, temper = temp, descript = desct, image = pic, right = copy };
邮箱:steven9801@163.com
QQ: 48039387