public List <string> GetWeather(string city) { if ((!city.Equals("呼市郊区")) && (!city.Equals("津市")) && (!city.Equals("沙市"))) { city = city.Replace("省", ""); city = city.Replace("市", ""); } city = city.Replace(" ", ""); city = city.Replace("\r", ""); city = city.Replace("\n", ""); string ans = ""; var result = new List <string>(); string url, temp; url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text=\"" + city + "\") and%20u=%22c%22&format=json"; temp = QQClient.Get(url); JsonYahooWeatherModel weather = (JsonYahooWeatherModel)JsonConvert.DeserializeObject(temp, typeof(JsonYahooWeatherModel)); if (weather.query.results == null) { // return ; result.Add("未查询到指定城市 " + city + " 的天气信息"); } else { var count = weather.query.results.channel.item.forecast.Count >= 7 ? 7 : weather.query.results.channel.item.forecast.Count; for (int i = 0; i < count; i++) { var waittoadd = System.Environment.NewLine + "周" + getYahooWeak(weather.query.results.channel.item.forecast[i].day) + ":" + getYahooWeatherCode(weather.query.results.channel.item.forecast[i].code) + ",最高气温:" + weather.query.results.channel.item.forecast[i].high + "摄氏度,最低气温:" + weather.query.results.channel.item.forecast[i].low + "摄氏度"; if (ans.Length + waittoadd.Length < 400) { ans += waittoadd; } else { result.Add(ans); ans = waittoadd; } } result.Add(ans); } return(result); }
public void Answer(AnswerContext context) { if (!context.CanAnswer) { return; } var anwer = new SearchResult(); if (CommandKey.Any(x => context.Message.StartsWith(x))) { bool EnableFlag = false; if (context.State != null && context.GetState <bool>(SettingKey)) { EnableFlag = true; } else if (context.MessageType == "message") { EnableFlag = true; } if (EnableFlag) { string[] tmp = context.Message.Trim().Split(' '); if (tmp.Length == 2) { var keyword = tmp[1]; // anwer = GetWiki(tmp[1], ""); switch (tmp[0]) { case "互动百科": case "互动": { string url = "http://www.baike.com/wiki/" + keyword; string temp = QQClient.Get(url); if (temp.Contains("尚未收录")) { anwer.Answer = "没有找到这个词条哦~"; break; } temp = temp.Replace("<meta content=\"", "&"); temp = temp.Replace("\" name=\"description\">", "&"); string[] result = temp.Split('&'); if (!result[1].Equals("")) { anwer.Answer = result[1]; anwer.Url = "http://www.baike.com/wiki/" + HttpUtility.UrlEncode(keyword); } break; } case "维基百科": case "维基": { string url = "https://zh.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exsentences=2&exintro=&explaintext=&exsectionformat=plain&exvariant=zh&titles=" + keyword; string temp = QQClient.Get(url); JsonWikipediaModel temp1 = (JsonWikipediaModel)JsonConvert.DeserializeObject(temp, typeof(JsonWikipediaModel)); string[] result = temp1.query.pages.ToString().Split("{}".ToCharArray()); JsonWikipediaPageModel pages = (JsonWikipediaPageModel)JsonConvert.DeserializeObject("{" + tmp[2] + "}", typeof(JsonWikipediaPageModel)); if (pages.extract != null) { anwer.Answer = pages.extract; anwer.Url = "https://zh.wikipedia.org/wiki/" + HttpUtility.UrlEncode(keyword); } else { anwer.Answer = "没有找到这个Wiki哦~"; } break; } case "百度百科": case "百科": default: { string url = "http://wapbaike.baidu.com/item/" + keyword; string temp = QQClient.Get(url); if (temp.Contains("您所访问的页面不存在")) { anwer.Answer = "没有找到这个词条哦~"; } if (temp.Contains("百科名片")) { temp = temp.Replace(""", ""); temp = temp.Replace("&", ""); temp = temp.Replace("百科名片", "&"); string[] result = temp.Split('&'); temp = result[1]; temp = temp.Replace("<p>", "&"); temp = temp.Replace("</p>", "&"); result = temp.Split('&'); temp = result[1].Replace("</a>", ""); temp = temp.Replace("<b>", ""); temp = temp.Replace("</b>", ""); temp = temp.Replace("<i>", ""); temp = temp.Replace("</i>", ""); temp = temp.Replace("<a", "&"); temp = temp.Replace("\">", "&"); result = temp.Split('&'); temp = ""; for (int i = 0; i < tmp.Length; i += 2) { if ((!tmp[i].Contains("card-info")) && (!tmp[i].Contains("div class"))) { temp += tmp[i]; } } if (!temp.Equals("")) { anwer.Answer = temp; anwer.Url = "http://wapbaike.baidu.com/item/" + HttpUtility.UrlEncode(keyword); } else { anwer.Answer = "词条 " + keyword + " 请查看http://wapbaike.baidu.com/item/" + HttpUtility.UrlEncode(keyword); } } else { anwer.Answer = "没有找到这个词条哦~"; } break; } } } else { context.Alerts.AddRange(Example()); } if (!string.IsNullOrEmpty(anwer.Answer)) { if (anwer.Answer.Length + anwer.Url.Length >= 400) { context.Answers.Add(anwer.Answer.Substring(0, 400 - anwer.Url.Length - 6) + "..." + anwer.Url); } else { context.Answers.Add(anwer.Answer + anwer.Url); } // context.Answers.Add(anwer); var newlog = new AutoAnswerMessageLog(); newlog.FromUin = context.SendToId; newlog.ToUin = context.FromUin; newlog.MessageType = "wiki"; newlog.P1 = tmp[1]; newlog.Data = anwer.Answer + anwer.Url; OrmManager.Insert(newlog); } } } }
public static string GetStock(string p1, string p2 = "") { string url = ""; p1 = p1.Replace(" ", ""); p1 = p1.Replace("\r", ""); p1 = p1.Replace("\n", ""); if (!p2.Equals("")) { p2 = p2.Replace(" ", ""); p2 = p2.Replace("\r", ""); p2 = p2.Replace("\n", ""); } switch (p1) { case ("上证指数"): url = "http://hq.sinajs.cn/list=s_sh000001"; break; case ("深证综指"): url = "http://hq.sinajs.cn/list=s_sz399106"; break; case ("中小板指数"): url = "http://hq.sinajs.cn/list=s_sz399005"; break; case ("创业板指数"): url = "http://hq.sinajs.cn/list=s_sz399006"; break; case ("深证成指"): url = "http://hq.sinajs.cn/list=s_sz399001"; break; case ("中小板综指"): url = "http://hq.sinajs.cn/list=s_sz399101"; break; case ("创业板综指"): url = "http://hq.sinajs.cn/list=s_sz399102"; break; default: { if (p1.ToCharArray()[0] == '6') { url = "http://hq.sinajs.cn/list=s_sh" + p1; } else if (p1.ToCharArray()[0] == '0' || p1.ToCharArray()[0] == '3') { url = "http://hq.sinajs.cn/list=s_sz" + p1; } else if (p1.Equals("上海") || p1.Equals("沪市") || p1.Equals("上证")) { url = "http://hq.sinajs.cn/list=s_sh" + p2; } else if (p1.Equals("深圳") || p1.Equals("深市") || p1.Equals("深证") || p1.Equals("创业板") || p1.Equals("中小板")) { url = "http://hq.sinajs.cn/list=s_sz" + p2; } else { return("参数错误"); } break; } } string dat = QQClient.Get(url, "", 100000, Encoding.GetEncoding("GB2312")); string[] tmp = dat.Split('\"'); tmp = tmp[1].Split(','); if (tmp.Length == 1) { return("参数错误"); } string ans = "根据新浪财经的信息," + tmp[0] + ":现价," + tmp[1] + ";涨跌" + tmp[2] + "," + tmp[3] + "%;成交量," + tmp[4] + "手," + tmp[5] + "万元。"; return(ans); }