private double getRate(string cur) { string strUrl, strData; double dbRate; dbRate = 1; if (cur == "USD") { strUrl = "https://rate.bot.com.tw/xrt?Lang=zh-TW"; strData = SingletonClass.singletonClass.RequestUrl(strUrl, System.Text.Encoding.UTF8); NSoup.Nodes.Document htmlDoc = NSoup.NSoupClient.Parse(strData); NSoup.Select.Elements ele = htmlDoc.GetElementsByTag("td"); for (int i = 0; i < ele.Count; i++) { if ((ele[i].Dataset.ContainsKey("data-table")) && (ele[i].Dataset["data-table"] == "本行即期買入")) { dbRate = SingletonClass.singletonClass.ConvertStringToDouble(ele[i].Text().Trim()); break; } } } return(dbRate); }
public static List <ToDoTask> getToDoTasks(string html) { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); NSoup.Select.Elements ele = doc.GetElementsByTag("span"); List <ToDoTask> result = new List <ToDoTask>(); foreach (var i in ele) { if (i.Attr("style") == "padding:1px 0 0 2px") { var title = i.GetElementsByTag("a")[0]; string content = title.Attr("title"); if (content.Equals("")) { content = title.Text(); } var urgencyElement = title.GetElementsByTag("b")[0]; string urgency = urgencyElement.Html(); var nextSpan = i.NextElementSibling; string time = nextSpan.Html(); ToDoTask toDoTask = new ToDoTask(urgency, content, time); result.Add(toDoTask); } } return(result); }
private NSoup.Select.Elements GetLinks(string url) { WSH.Options.Common.Result result = this.Request(url); if (result.IsSuccess) { string htmlString = result.Msg; NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(htmlString); NSoup.Select.Elements links = doc.GetElementsByTag("a"); NSoup.Select.Elements h3s = doc.GetElementsByTag("h3"); NSoup.Select.Elements spans = doc.GetElementsByTag("span"); if (links != null) { if (h3s != null) { links.AddRange(h3s); } if (spans != null) { links.AddRange(spans); } } return(links); } else { throw new Exception(result.Msg); } }
/// <summary> /// 请求主页面 /// </summary> /// <param name="urlAddress"></param> /// <returns></returns> public override Result Request(string index) { Result result = base.Request(this.PageUrl(index)); if (result.IsSuccess && !string.IsNullOrWhiteSpace(result.Msg)) { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(result.Msg); var els = doc.Select("div.well-sm"); if (els != null && els.Count > 0) { foreach (var el in els) { var link = Utils.GetElementFirst(el.GetElementsByTag("a")); if (link != null) { var subUrl = Utils.GetAttr(link, "href"); var title = Utils.GetText(Utils.GetElementFirst(link.GetElementsByTag("span"))); var img = Utils.GetAttr(Utils.GetElementFirst(link.GetElementsByTag("img")), "src"); var number = Utils.GetText(Utils.GetElementFirst(el.GetElementsByTag("font"))).Split(' ')[0]; var rating = Utils.GetText(Utils.GetElementFirst(el.GetElementsByTag("b"))); DataTableHelper.AddRow(this.DataSource, title, subUrl, "", "", img, number, rating, null); } } } } return(result); }
public List <String> FindImages(String question, String userAgent) { List <String> imagesList = new List <String>(); try { String googleUrl = "https://www.google.com/search?tbm=isch&q=" + question.Replace(",", ""); NSoup.Nodes.Document htmlDoc = NSoupClient.Connect(googleUrl).UserAgent(userAgent).Timeout(10 * 1000).Get(); //Handling correctly auto redirects... checkForRedirectsOnHTMLDocument(ref htmlDoc, userAgent); /* * //This is old method * NSoup.Select.Elements images = htmlDoc.Select("div.rg_di.rg_el.ivg-i img"); //div with class="rg_di rg_el ivg-i" containing img * foreach (NSoup.Nodes.Element img in images) { * NSoup.Select.Elements links = img.Parent.Select("a[href]"); * if (links.Count() > 0) { //is there a link around img? * NSoup.Nodes.Element link = img.Parent.Select("a[href]").First(); * String href = img.Parent.Attr("abs:href"); //link which needs to be parsed to get the full img url * Regex regex = new Regex("imgurl=(.*?)&imgrefurl="); //Everything between "imgurl=" and "&imgrefurl=" * var v = regex.Match(href); * if (v != null && v.Groups.Count == 2) { * if (v.Groups[1].Value != String.Empty) { * String imgURL = v.Groups[1].ToString(); * imagesList.Add(imgURL); * } * } * } * } */ NSoup.Select.Elements div_with_images = htmlDoc.Select("div.y.yi div.rg_di.rg_bx.rg_el.ivg-i"); //div with class="y yi" containing div with class="rg_di rg_bx rg_el ivg-i" foreach (NSoup.Nodes.Element div_with_image in div_with_images) { NSoup.Nodes.Element rg_meta_div = div_with_image.Select("div.rg_meta").First(); String text_where_the_img_is = rg_meta_div.ToString(); Regex regex = new Regex("ou":"(.*?)""); //Everything between "ou":"" and """ var v = regex.Match(text_where_the_img_is); if (v != null && v.Groups.Count == 2) { if (v.Groups[1].Value != String.Empty) { String imgURL = v.Groups[1].ToString(); imagesList.Add(imgURL); } } } } catch (Exception ex) { this.Error = ex; } return(imagesList); }
/// <summary> /// 解析主页的访问用户 /// </summary> /// <returns></returns> public void VisitParser() { this.Login(); Result result = this.Request(this.HomePageUrl); string pageHtml = result.Msg; if (string.IsNullOrWhiteSpace(pageHtml)) { return; } NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(pageHtml); NSoup.Nodes.Element wrapElement = doc.GetElementById("show_style_01"); NSoup.Select.Elements userElementNodes = wrapElement.GetElementsByTag("li"); if (userElementNodes != null && userElementNodes.Count > 0) { //倒序排列,最新的在最后面 IEnumerable <NSoup.Nodes.Element> userElements = userElementNodes.Reverse(); foreach (NSoup.Nodes.Element userElement in userElements) { NSoup.Nodes.Element picElement = GetElementFirst(userElement.GetElementsByClass("pic")); NSoup.Nodes.Element nameElement = GetElementFirst(userElement.GetElementsByClass("user_name")); NSoup.Nodes.Element userInfoElement = GetElementFirst(userElement.GetElementsByClass("user_info")); NSoup.Nodes.Element dateElement = GetElementFirst(userElement.GetElementsByClass("date")); string userName = nameElement == null ? "" : nameElement.Child(0).Text(); string homePage = UriHelper.RemoveParams(nameElement == null ? "" : nameElement.Child(0).Attr("href")); string pic = picElement.Child(0).Child(0).Attr("src"); DateTime date = Convert.ToDateTime(dateElement.Text().Replace("到访:", "")); string[] userInfo = StringHelper.SplitWhiteSpace(userInfoElement.Child(0).Text()); int age = Convert.ToInt32(userInfo[0].Replace("岁", "")); string addr = userInfo.Length > 1 ? userInfo[1] : string.Empty; string userCode = homePage.Substring(homePage.LastIndexOf('/') + 1); if (addr.Contains("广州") && !string.IsNullOrWhiteSpace(userCode)) { FateUserInfo user = FateUserInfoManager.GetUser(userCode); if (user == null) { user = new FateUserInfo() { CreateTime = DateTime.Now }; } user.ModifyTime = DateTime.Now; user.UserCode = userCode; user.Address = addr; user.Age = age; user.HeadFileName = pic; user.UserName = userName; FateUserInfoManager.SaveOrUpdateUser(user); } } } }
/// <summary> /// 判断文书编辑状态(返回bool) /// </summary> /// <returns></returns> public bool GetEditState() { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(webBrowser.Document.Body.InnerHtml); var editState = doc.Select("#editState input"); if (editState.Count > 0 && editState[0].Attr("value") == "false") { return(false); } return(true); }
public void RequestSubPage(ProgressHandler handler) { var lastColumn = this.DataSource.Columns.Count - 1; if (this.DataSource != null && this.DataSource.Rows.Count > 0) { int i = 1; if (!this.Login()) { MsgBox.Alert("登陆失败"); return; } foreach (DataRow row in DataSource.Rows) { handler(row, new ProgressEventArgs() { Value = i }); i++; //防止网络慢,登录过期 if (i % 500 == 0) { this.Login(); } var urlAddress = BasePath + row[1]; Result result = base.Request(urlAddress); if (result.IsSuccess && !string.IsNullOrWhiteSpace(result.Msg)) { try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(result.Msg); var links = doc.GetElementsByTag("a"); foreach (var item in links) { var text = item.Text(); if (text.Contains("下载视频") && text.Contains("鼠标右键另存为")) { var downloadUrl = Utils.GetAttr(item, "href"); row[2] = downloadUrl; row[3] = Path.GetFileName(downloadUrl); break; } } } catch (Exception ex) { row[lastColumn] = ex.Message; } } } } }
private static String getBaseURI(NSoup.Nodes.Document htmlDoc) { String baseURI = htmlDoc.BaseUri; Regex regex = new Regex("(.*)/"); //e.g. https://google.pl var v = regex.Match(baseURI); if (v != null && v.Groups.Count == 2) { if (v.Groups[1].Value != String.Empty) { return(v.Groups[1].ToString()); } } return(String.Empty); }
/// <summary> /// 根据HTML内容,获取所有img标签 /// </summary> /// <param name="content">HTML内容</param> /// <returns></returns> public static List <string> GetImageTagList(string content) { List <string> imgList = null; //解析 try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(content); imgList = doc.GetElementsByTag("img").Select(a => a.ToString()).ToList(); } catch { imgList = new List <string>(); } return(imgList); }
private void RequestData(FundValue fundValue) { //https://www.fundrich.com.tw/fund/116011.html?id=116011#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD //https://www.fundrich.com.tw/fund/062003.html?id=062003#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD //https://www.fundrich.com.tw/fund/CIT004.html?id=CIT004#%E5%9F%BA%E9%87%91%E7%B8%BD%E8%A6%BD /* * 庫存 * 淨值 * 匯率 * 69.189 * 35.91 * 30.8825 = 76730 * * => 利潤: 76730 / 75000 = 102.3% * */ try { string strPath, strData; strPath = string.Format("{0}\\..\\..\\..\\WindowsFormsApplication1\\Data\\MyData.json", System.AppDomain.CurrentDomain.BaseDirectory); strData = SingletonClass.singletonClass.RequestUrl(fundValue.strUrl, System.Text.Encoding.UTF8); NSoup.Nodes.Document htmlDoc = NSoup.NSoupClient.Parse(strData); NSoup.Select.Elements ele = htmlDoc.GetElementsByTag("span"); for (int i = 0; i < ele.Count; i++) { if ((ele[i].Dataset.ContainsKey("reactid")) && (ele[i].Dataset["reactid"] == "491")) { fundValue.netValue = SingletonClass.singletonClass.ConvertStringToDouble(ele[i].Text().Trim()); } if ((ele[i].Dataset.ContainsKey("reactid")) && (ele[i].Dataset["reactid"] == "492")) { fundValue.cur = ele[i].Text().Trim(); } } fundValue.rate = getRate(fundValue.cur); } catch (Exception ex) { SingletonClass.singletonClass.WriteLog(ex.ToString()); } }
/// <summary> /// 获取界面的数据(返回Json数据) /// </summary> /// <returns></returns> public List <JsonData> GetViewData() { List <JsonData> list = new List <JsonData>(); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(webBrowser.Document.Body.InnerHtml); var ret = doc.Select("#datastorage input"); for (int i = 0; i < ret.Count; i++) { if (ret[i].Attr("acceptflag") == "true")//只有属性acceptflag设置为true才保存到数据库 { JsonData data = new JsonData(); data.DataId = ret[i].Attr("id"); data.JsonValue = ret[i].Attr("value"); list.Add(data); } } return(list); }
private void button3_Click(object sender, EventArgs e) { PublicValue.arrText.Clear(); #region q new Thread(() => { for (int k = 1; k <= 1; k++) { string Url = "https://www.xicidaili.com/nn/" + k; try { string strHtml = Utils.GetHtml(Url); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(strHtml); NSoup.Select.Elements tableEle = doc.GetElementsByTag("table"); foreach (var tableItem in tableEle) { if (tableItem.Id == "ip_list") { NSoup.Select.Elements trEle = tableItem.GetElementsByTag("tr"); foreach (var trItem in trEle) { NSoup.Select.Elements tdEle = trItem.GetElementsByTag("td"); if (tdEle.Count > 3) { string ip = tdEle[1].Text(); int port = 0; string portStr = tdEle[2].Text(); int.TryParse(portStr, out port); PublicValue.arrText.Add(ip + ":" + port); listBox1.Items.Add(ip + ":" + port); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message, TitleInfo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }).Start(); #endregion }
/// <summary> /// 传入整个页面的html,返回只有任务列表那部分html /// 即<ul></ul>之间的html /// 并把所有点击的链接都去除 /// </summary> /// <param name="html"></param> /// <returns></returns> public static string getToDoTaskHtmlUl(string html) { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); NSoup.Select.Elements ele = doc.GetElementsByClass("con_left"); if (ele.IsEmpty) { NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); logger.Error("无法解析html:items_ul"); return(null); } ele[0].GetElementsByTag("a").RemoveAttr("href");//把所有点击的链接都去除 StringBuilder sb = new StringBuilder("<div class=\"con_left\">"); sb.Append(ele[0].Html()); sb.Append("</div>"); string ulHtml = sb.ToString(); return(ulHtml); }
/// <summary> /// 根据HTML内容,获取所有img标签Url /// </summary> /// <param name="content">HTML内容</param> /// <param name="isUri">是否补充URL</param> /// <returns></returns> public static List <string> GetImageUrlList(string content, bool isUri = true) { List <string> imgList = null; //解析 try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(content); imgList = doc.GetElementsByTag("img").Select(a => a.Attributes["src"]).ToList(); if (isUri) { imgList = imgList.Select(a => a.ToImageUrl()).ToList(); } } catch { imgList = new List <string>(); } return(imgList); }
private static void checkForRedirectsOnHTMLDocument(ref NSoup.Nodes.Document htmlDoc, String userAgent) { //Checking if http-equiv=refresh foreach (NSoup.Nodes.Element refresh in htmlDoc.Select("html head meta[http-equiv=refresh]")) { String matcher = refresh.Attr("content"); Regex regex = new Regex("url=(.*)"); var v = regex.Match(matcher); if (v != null && v.Groups.Count == 2) { if (v.Groups[1].Value != String.Empty) { //Need to know the base uri: String baseURI = getBaseURI(htmlDoc); String urlToFetch = baseURI + v.Groups[1].ToString(); htmlDoc = NSoupClient.Connect(urlToFetch).UserAgent(userAgent).Timeout(10 * 1000).Get(); checkForRedirectsOnHTMLDocument(ref htmlDoc, userAgent); } } } //Javascript based redirection handling foreach (NSoup.Nodes.Element javascript in htmlDoc.Select("html head script[type=text/javascript]")) { String matcher = javascript.ToString(); Regex regex = new Regex("window.google.gbvu='(.*?)'"); var v = regex.Match(matcher); if (v != null && v.Groups.Count == 2) { if (v.Groups[1].Value != String.Empty) { //Need to know the base uri: String baseURI = getBaseURI(htmlDoc); String urlToFetch = baseURI + v.Groups[1].ToString().Replace("\\75", "=").Replace("\\075", "=").Replace("\\x3d", "=").Replace("\\46", "&").Replace("\\046", "&").Replace("\\x26", "&"); //converting ASCII octet codes to characters htmlDoc = NSoupClient.Connect(urlToFetch).UserAgent(userAgent).Timeout(10 * 1000).Get(); checkForRedirectsOnHTMLDocument(ref htmlDoc, userAgent); } } } }
public void ReceiveCompleted(String body) { if (String.IsNullOrEmpty(body)) { return; } try { var result = new PageBuilder().BuildPage(body, 0); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(result); result = doc.OuterHtml(); Signal_Excute("GlobalHub", x => { x.Invoke("CompileEvent", 0, result); }); } catch (Exception ex) { Signal_Excute("GlobalHub", x => { x.Invoke("CompileEvent", 1, ex.Message); }); } }
public Thread hThread = null; // Reference to the parser thread that is running private void ParserThread() { ++RunningParserThreads; NSoup.Nodes.Document _steamPage = null; try { _steamPage = NSoup.NSoupClient.Connect("https://steamcommunity.com/" + this.SteamURL).Get(); } catch { --RunningParserThreads; this.SetText("Failed! (HttpClient)"); return; } if (_steamPage == null) { --RunningParserThreads; this.SetText("Failed! (Null)"); return; } this.SetText(this.Name = _steamPage.Select(".actual_persona_name").Text ?? "", 2); if (String.IsNullOrWhiteSpace(this.Name)) { --RunningParserThreads; this.SetText("Invalid URL!"); return; } this.SetText((this.Banned = !(_steamPage.Select(".profile_ban")).IsEmpty).ToString(), 4); this.LastInfoUpdate = DateTime.Now; this.SetText("Finished!"); --RunningParserThreads; }
public string Search(string search) { string url = "https://search.mcmod.cn/s?key=" + search.Split('(')[0] + "&site=&filter=1&mold=0"; try { NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(web.getHtml(url)); var List = doc.GetElementsByClass("search-result-list"); var item = NSoup.NSoupClient.Parse(List.ToString()).GetElementsByClass("result-item"); foreach (var i in item) { var j = NSoup.NSoupClient.Parse(i.GetElementsByClass("head").ToString()); var a = j.GetElementsByTag("a"); var title = a.Text.Replace("<em>", "").Replace("</em>", "").Replace("\"", "").Replace(" ", ""); if (title.IndexOf(search.Split('(')[0].Replace(" ", "")) > 0) { return(a[1].Attr("href").ToString()); } } } catch (Exception ex) { } return(null); }
public void ReceiveCompleted(String body) { if (String.IsNullOrEmpty(body)) { return; } var globalHub = ConfigurationManager.AppSettings["GlobalHub"]; var compileEvent = ConfigurationManager.AppSettings["CompileEvent"]; try { var result = new PageBuilder().BuildPage(body, 0); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(result); result = doc.OuterHtml(); Signal_Excute(globalHub, x => { x.Invoke(compileEvent, 0, result); }); } catch (Exception ex) { Signal_Excute(globalHub, x => { x.Invoke(compileEvent, 1, ex.Message); }); } }
public void Execute() { SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=GZMgzm123"); con.Open(); string sql = "select UserID,UserName from [User]"; SqlCommand sc = new SqlCommand(sql, con); SqlDataReader sr = sc.ExecuteReader(); Dictionary <string, int> unload = new Dictionary <string, int>(); Dictionary <string, int> loaded = new Dictionary <string, int>(); while (sr.Read()) { unload.Add(sr[1].ToString(), Convert.ToInt32(sr[0])); } sr.Close(); while (unload.Count > 0) { string name = unload.First().Key; string url = "https://social.msdn.microsoft.com/Profile/" + name + "/activity/"; int id = unload.First().Value; try { //Log.SaveNote(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":AutoTask is Working!"); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.Accept = "text/html"; req.UserAgent = "Mozilla/5.0 (Windows NT 10.0;Win64;x64)"; string html = null; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); using (StreamReader reader = new StreamReader(res.GetResponseStream())) { html = reader.ReadToEnd(); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(html); SqlCommand findc = new SqlCommand(); findc.CommandType = CommandType.Text; findc.Connection = con; DateTime newDateTime = DateTime.Now; SqlDataReader scc; NSoup.Select.Elements ele = doc.Select("div#Activities>div"); int i = 0; foreach (NSoup.Nodes.Element ee in ele) { string dt = ee.Select("div.activity-date").Attr("title"); string content = ee.Select("div.activity-detail").Text; string href = ee.Select("div.activity-detail>a").First().Attr("href"); //href = href.Substring(href.IndexOf("<a"), href.IndexOf("/a>")); dt = dt.Substring(dt.IndexOf("Date(") + 5); dt = dt.Substring(0, dt.IndexOf(")")); double unixDate = Convert.ToDouble(dt); DateTime start1 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); DateTime date = new DateTime(); date = start1.AddMilliseconds(unixDate).ToLocalTime(); TimeSpan ts = newDateTime - date; if (content.Contains("'")) { content = content.Replace("'", "''"); } //string insql = "select * from [Member] where reply_content='" + content + "' and reply_time='" + date + "' and reply_id=" + id; string insql = "select * from [Member] where reply_url='" + href + "'and reply_id=" + id; findc.CommandText = insql; scc = findc.ExecuteReader(); if (scc.Read()) { scc.Close(); i++; continue; /*if (i < 3) * { * continue; * } * else * { * i = 0; * break; * }*/ } else { scc.Close(); insql = "INSERT INTO [Member](reply_id,member_name,reply_content,reply_time,reply_url) VALUES('" + id + "','" + name + "',N'" + content + "','" + date + "','" + href + "')"; SqlCommand ff = new SqlCommand(insql, con); ff.ExecuteNonQuery(); } } unload.Remove(name); loaded.Add(name, id); } } catch (WebException we) { con.Close(); Console.WriteLine(we.Message); } } sr.Close(); con.Close(); }
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = await result as Activity; string ikoktest = activity.Text; //byte[] utf8bytes = System.Text.Encoding.Default.GetBytes(ikoktest); //byte[] utf8bytes2 = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.UTF8, utf8bytes); //utf8bytes2.ToString(); //当前文有重复的基本意图的时候,取到最近的基本意图 if (intention.Split(',').Length > 1 && !intention.Equals("")) { intention = intention.Substring(intention.LastIndexOf(",") + 1); } //@@@intention_head MatchCollection mc_study_words = Regex.Matches(ikoktest, ".*教.*你(一些)*(部分)*(一点)*(?<100>.{1,2})词"); bool is_study = Regex.IsMatch(ikoktest, ".*教.*你(一些)*(部分)*(一点)*(?<100>.{1,2})词"); if (is_study) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_study; talking_depth = 0; } MatchCollection mc_action = Regex.Matches(ikoktest, "[.]*放(一首){0,1}(?<musician>.*)(的){0,1}歌[\\.。!]*"); bool is_action = Regex.IsMatch(ikoktest, "[.]*放(一首){0,1}(.*)(的){0,1}歌[\\.。!]*"); if (is_action) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_action; talking_depth = 0; } MatchCollection mc_check = Regex.Matches(ikoktest, "(.*查(一下){0,1}(?<check>.+)(是什么){0,1}|(?<check>.+)是(什么|啥))|(?<check>^怎么{0,1}样{0,1}.+)"); bool is_check = Regex.IsMatch(ikoktest, "(.*查(一下){0,1}(?<check>.+)(是什么){0,1}|(?<check>.+)是(什么|啥))|(?<check>^怎么{0,1}样{0,1}.+)"); if (is_check) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_check; talking_depth = 0; } MatchCollection mc_train = Regex.Matches(ikoktest, ".*(训练模式|训练你)"); bool is_train = Regex.IsMatch(ikoktest, ".*(训练模式|训练你)"); if (is_train) { //当前文已经有基本意图的时候,为后文追加的基本意图进行格式的准备 //if (intention != null && !intention.Equals("")) intention += ","; intention = new Intention().intention_train; talking_depth = 0; } if (intention.Equals("train") || intention == "train") { foreach (Match item in mc_train) { await context.PostAsync("请问您是要开启训练模式吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlCommand mycmd_s = new MySqlCommand("select value from Ssubject", conn); MySqlCommand mycmd_p = new MySqlCommand("select value from predicate", conn); MySqlCommand mycmd_a = new MySqlCommand("select value from accusative", conn); string result_s = ""; string result_p = ""; string result_a = ""; MySqlDataReader reader = null; reader = mycmd_s.ExecuteReader(); while (reader.Read()) { allSsubjects.Add(reader[0].ToString()); result_s += reader[0].ToString() + ","; } reader.Close(); reader = mycmd_p.ExecuteReader(); while (reader.Read()) { allPredicate.Add(reader[0].ToString()); result_p += reader[0].ToString() + ","; } reader.Close(); reader = mycmd_a.ExecuteReader(); while (reader.Read()) { allAccusative.Add(reader[0].ToString()); result_a += reader[0].ToString() + ","; } result_s = result_s.Remove(result_s.LastIndexOf(","), 1); result_p = result_p.Remove(result_s.LastIndexOf(","), 1); result_a = result_a.Remove(result_s.LastIndexOf(","), 1); talking_depth++; await context.PostAsync("好的,我已经学会的主语有: " + result_s + "。谓语有:" + result_p + "。宾语有:" + result_a + "。您可以在此基础上训练我,也可以继续让我学习哦~~"); reader.Close(); conn.Close(); } } if (temp == 2) { foreach (string every in allPredicate) { await context.PostAsync(every); MatchCollection mc_train_words_confirm = Regex.Matches(ikoktest, "(?<Ssubjective>.{0,3})" + every + "(?<accusative>.{1,3})[\\.;。;]*"); bool confirm = Regex.IsMatch(ikoktest, "(?<Ssubjective>.{0,3})" + every + "(?<accusative>.{1,3})[\\.;。;]*"); if (confirm) { current_Predicate = every; foreach (Match item in mc_train_words_confirm) { current_Ssubject = item.Groups["Ssubjective"].Value; current_Accusative = item.Groups["accusative"].Value; } //改变基本元素的权值 MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlCommand mycmd_1 = new MySqlCommand("update Ssubject set authority = authority+1 where value = '" + current_Ssubject + "';", conn); MySqlCommand mycmd_2 = new MySqlCommand("update predicate set authority= authority +1 where value = '" + current_Predicate + "';", conn); MySqlCommand mycmd_3 = new MySqlCommand("update accusative set authority= authority +1 where value = '" + current_Accusative + "';", conn); if (mycmd_1.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from Ssubject where value = '" + current_Ssubject + "';", conn).ExecuteReader(); while (mr.Read()) { current_Ssubject_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } if (mycmd_3.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from accusative where value = '" + current_Accusative + "';", conn).ExecuteReader(); while (mr.Read()) { current_Accusative_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } if (mycmd_2.ExecuteNonQuery() > 0) { MySqlDataReader mr = new MySqlCommand("select id from predicate where value = '" + current_Predicate + "';", conn).ExecuteReader(); while (mr.Read()) { current_Predicate_id = Int16.Parse(mr[0].ToString()); } mr.Close(); } //改变对应关系的权值 MySqlConnection conn1 = new Init_DB().get_Init_DB(); conn1.Open(); if (current_Ssubject_id != 0) { MySqlCommand currentcmd_1 = new MySqlCommand("select * from s_p where s_id = " + current_Ssubject_id + " and p_id = " + current_Predicate_id, conn1); if (currentcmd_1.ExecuteNonQuery() > 0) { MySqlCommand thecmd = new MySqlCommand("update s_p set authority = authority+1 where s_id =" + current_Ssubject_id + " and p_id =" + current_Predicate_id, conn1); thecmd.ExecuteNonQuery(); } else { MySqlCommand thecmd = new MySqlCommand("insert into s_p(s_id,p_id) values(" + current_Ssubject_id + "," + current_Predicate_id + ")", conn1); thecmd.ExecuteNonQuery(); } } if (current_Accusative_id != 0) { MySqlCommand currentcmd_2 = new MySqlCommand("select p_id,a_id from p_a where p_id =" + current_Predicate_id + " and a_id =" + current_Accusative_id, conn1); if (currentcmd_2.ExecuteNonQuery() > 0) { MySqlCommand thecmd = new MySqlCommand("update p_a set authority = authority+1 where p_id =" + current_Predicate_id + " and a_id = " + current_Accusative_id, conn1); thecmd.ExecuteNonQuery(); } else { MySqlCommand thecmd = new MySqlCommand("insert into p_a(p_id,a_id) values(" + current_Predicate_id + "," + current_Accusative_id + ")", conn1); thecmd.ExecuteNonQuery(); } } conn1.Close(); await context.PostAsync("本次训练完成!"); break; } } } } if (intention.Equals("check") || intention == "check") { foreach (Match item in mc_check) { await context.PostAsync("请问您是要查" + item.Groups["check"].ToString() + "吗?"); thecache = item.Groups["check"].ToString(); await context.PostAsync(thecache); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm_check = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm_check) { await context.PostAsync("正在为您执行命令...."); talking_depth++; if (thecache != null && !thecache.Equals("")) { await context.PostAsync("正在为您查询...."); //thecache=System.Web.HttpUtility.UrlEncode(thecache, Encoding.UTF8); if (Regex.IsMatch(thecache, "^怎么{0,1}样{0,1}")) { org.jsoup.nodes.Document docsource = org.jsoup.Jsoup.connect("https://jingyan.baidu.com/search?word=" + thecache).get(); // await context.PostAsync(HtmlString); //NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect("https://jingyan.baidu.com/search?word=" + thecache).Get(); //await context.PostAsync(doc.ToString()); //NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString); //await context.PostAsync("runrurun2"+doc.Html()); // WebClient webClient = new WebClient(); //webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); // String HtmlString = Encoding.GetEncoding("utf-8").GetString(webClient.DownloadData("https://jingyan.baidu.com/search?word=" + thecache)); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(docsource.body().toString()); NSoup.Select.Elements e = doc.Select(".search-list").Select("dl").First.Select("dt").Select("a"); await context.PostAsync(e.ToString()); string url = e.Attr("href"); String aim_Html = org.jsoup.Jsoup.connect("https://jingyan.baidu.com" + url).get().toString(); NSoup.Nodes.Document doc_1 = NSoup.NSoupClient.Parse(aim_Html); string aimcontent = doc_1.Select("div.exp-content-block").Select("li[class^=exp-content-list]").Text; aimcontent.Replace("步骤阅读", ""); aimcontent.Replace("END", ""); Regex.Replace(aimcontent, "\\s[0-9]{0,2}\\s", "\\n\\s[0-9]{0,2}\\s"); await context.PostAsync(aimcontent); } else { thecache = thecache.Replace("是什么", ""); String HtmlString = Encoding.GetEncoding("utf-8").GetString(new WebClient().DownloadData("https://baike.baidu.com/item/" + thecache)); NSoup.Nodes.Document doc = NSoup.NSoupClient.Parse(HtmlString); await context.PostAsync(doc.Select(".lemma-summary").First.Text()); } //NSoup.Nodes.Document doc = NSoup.NSoupClient.Connect("https://baike.baidu.com/item/JJ").Get(); //Console.Write(doc.Text()); await context.PostAsync("查询成功,欢迎下次光临"); } } } } if (intention.Equals("action") || intention == "action") { foreach (Match item in mc_action) { await context.PostAsync("您要放" + item.Groups["musician"].ToString() + "歌吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "[对是好嗯]+的*"); if (confirm) { await context.PostAsync("正在为您执行命令...."); Process myProcess = new Process(); // try{ myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.FileName = "C:\\Program Files (x86)\\kuwo\\kuwomusic\\8.5.2.0_UG6\\bin\\KwMusic.exe"; myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); //} //catch (Exception e){ //} } } } if (intention.Equals("study") || intention == "study") { foreach (Match item in mc_study_words) { await context.PostAsync("您要教我" + item.Groups[100].ToString() + "词吗?"); } int temp = talking_depth + 1; if (temp == 1) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { talking_depth++; await context.PostAsync("请问它们将要在句子中作什么成分?我暂时只能学习主语,谓语和宾语哟~~"); } } if (temp == 2) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^(?<grammer>[主谓宾])语{0,1}"); bool confirm = Regex.IsMatch(ikoktest, "^(?<grammer>[主谓宾])语{0,1}"); if (confirm) { foreach (Match item in mc_study_words_confirm) { thecache = item.Groups["grammer"].Value; } talking_depth++; await context.PostAsync("好的,我知道了。。您请说吧!"); } } if (temp == 3) { MatchCollection mc_study_words_content = Regex.Matches(ikoktest, "([^\\d]{1,2})[,,\\.。;;]*"); string aim_table = ""; if (thecache.Equals("主")) { aim_table = "Ssubject"; } if (thecache.Equals("谓")) { aim_table = "predicate"; } if (thecache.Equals("宾")) { aim_table = "accusative"; } if (aim_table != null && !aim_table.Equals("")) { foreach (Match item in mc_study_words_content) { await context.PostAsync("您说的词语是:" + item.Groups[1].Value + ",正在学习...."); MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); Regex reg = new Regex("[,,\\.。;;]"); string content = reg.Replace(item.Groups[1].Value, ""); MySqlCommand mycmd = new MySqlCommand("insert into " + aim_table + "(value) values('" + content + "')", conn); if (mycmd.ExecuteNonQuery() > 0) { await context.PostAsync("已成功学习!"); } conn.Close(); } } intention = ""; } } //@@@intention_body if (intention == "" || intention.Equals("")) { await context.PostAsync("不知道您要干什么呢~~需要学习吗?"); int temp = talking_depth + 1; if (temp == 1) { current_intent_id = new Train().train_intentid(ikoktest, allSsubjects, allPredicate, allAccusative); MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm) { MySqlConnection conn = new Init_DB().get_Init_DB(); conn.Open(); MySqlDataReader mr = new MySqlCommand("select id from intent where value = '" + thecache + "';", conn).ExecuteReader(); while (mr.Read()) { Int16.Parse(mr[0].ToString()); } mr.Close(); intention = "studying"; await context.PostAsync("好的,请遵照提示配置您的对话模板。"); await context.PostAsync("请输入您的命令类型:(a)对话型命令或者(b)功能型!默认为对话型"); } } } if (intention == "studying" || intention.Equals("studying")) { MatchCollection mc_study_words_confirm = Regex.Matches(ikoktest, "b"); bool confirm = Regex.IsMatch(ikoktest, "b"); int temp = talking_depth + 1; if (temp == 1) { if (confirm) { talking_unit.type = 1; talking_depth++; await context.PostAsync("请输入您要完成的功能:(目前已经实现的功能有:打开,关闭,查询,订购。更多功能正在开发中!请根据提示回复必要的信息已实现具体的功能!)"); } else { talking_depth++; await context.PostAsync("请输入您的问题"); } } if (temp == 2) { MatchCollection mc_function_confirm = Regex.Matches(ikoktest, "^(?<function>打开|关闭|查询|订购)(?=\\s)"); bool confirm1 = Regex.IsMatch(ikoktest, "^(?<function>打开|关闭|查询|订购)(?=\\s)"); if (confirm1) { foreach (Match item in mc_function_confirm) { thecache = item.Groups["function"].Value; } talking_unit.order = thecache; talking_units.Add(talking_unit); talking_unit.initTalking_unit(); await context.PostAsync("是否需要进一步的操作?"); talking_depth = 3; } else { string question = new GetIntent().getMain(ikoktest, allSsubjects, allPredicate, allAccusative); talking_unit.question = question; talking_depth++; await context.PostAsync("请输入该问题的回答"); } } if (temp == 3) { string answer = ikoktest; talking_unit.answer = answer; talking_depth++; await context.PostAsync("是否需要进一步的操作?"); } if (temp == 4) { MatchCollection mc_confirm = Regex.Matches(ikoktest, "^[对是好嗯]+的*"); bool confirm2 = Regex.IsMatch(ikoktest, "^[对是好嗯]+的*"); if (confirm2) { talking_units.Add(talking_unit); talking_unit.initTalking_unit(); await context.PostAsync("好的,请遵照提示配置您的对话模板。"); await context.PostAsync("请输入您的命令类型:(a)对话型命令或者(b)功能型!默认为对话型"); talking_depth = 0; } else { MySqlConnection conn1 = new Init_DB().get_Init_DB(); conn1.Open(); //将记录存到数据库中 foreach (Talking_unit item in talking_units) { int i = 0; new MySqlCommand("insert into talking_unit(depth,intended_id,type,question,answer,order) values(" + i + "," + current_intent_id + "," + item.type + "," + item.question + "," + item.answer + "," + item.order + ")", conn1); i++; } conn1.Close(); } } } await context.PostAsync("本次的基本意图是:" + intention); await context.PostAsync("本次的谈话深度是:" + talking_depth); // if (activity.Text.Contains("你好")) // { // await context.PostAsync("你好,老铁"); // } // else if (activity.Text.Contains("你叫什么名字")) // { // await context.PostAsync("你就叫我特浪铺吧。"); // } // else if (activity.Text.Contains("你有对象吗")) // { // await context.PostAsync("不要问这么悲伤的问题啊! 扎心了 老铁。。。"); // } // else { // await context.PostAsync("你在用脸滚键盘么。。。你发的什么我看不懂"); // } // int length = (activity.Text ?? string.Empty).Length; // await context.PostAsync($"you sent {activity.Text}which was{length} characters"); // context.Wait(MessageReceivedAsync); }