/// <summary> /// Parses the wikipedia. /// </summary> /// <param name="searchWord">The search word.</param> /// <param name="outputType">Type of the output.</param> /// <param name="parseType">Type of the parse.</param> public void ParseWikipedia(string searchWord, WikiOutputType outputType, WikiParseType parseType) { string uri = ""; StringBuilder str = new StringBuilder(); switch (parseType) { case WikiParseType.Article: this.lblTitle.Text = "Wikipedia Artikel"; Uri webUri; Uri downloadUri; if (WebValidator.IsWebAddress(searchWord)) { // http://de.wikipedia.org/wiki/Die_Entf%C3%BChrung_der_U-Bahn_Pelham_123 //searchWord = searchWord.Substring(searchWord.IndexOf("wiki/") + 5); //uri = WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord); if (uri.Contains(WP_EXPORT_SCRIPT_ADD)) { //uri = searchWord; uri = searchWord.Replace("/wiki/" + WP_EXPORT_SCRIPT_ADD, "/wiki/"); } else { //uri = searchWord.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD); uri = searchWord; } if (!searchWord.Contains(WP_EXPORT_SCRIPT_ADD)) { searchWord = searchWord.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD); } webUri = new Uri(uri); downloadUri = new Uri(searchWord); //.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD)); } else { searchWord = this.ToCamelCase(searchWord); searchWord = searchWord.Replace(' ', '_'); if (uri.Contains(WP_EXPORT_SCRIPT_ADD)) { uri = WP_URL + searchWord; } else { uri = WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord); } webUri = new Uri(WP_URL + searchWord); downloadUri = new Uri(WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord)); } this.wbResponse.Stop(); //this.wbResponse = new WebBrowser(); //this.wbResponse.Url = null; this.wbResponse.Url = webUri; this.wbResponse.Navigate(webUri); WebClient wc = new WebClient(); wc.Headers.Add("user-agent", "MovieMatic (compatible; .NET 3.5;)"); string response = wc.DownloadString(downloadUri.AbsoluteUri); this.txtResponse.Text = response; //this.wbResponse.Refresh(WebBrowserRefreshOption.Completely); break; case WikiParseType.Search: this.lblTitle.Text = "Suchergebnis"; searchWord = searchWord.Replace(' ', '+'); uri = WP_URL + string.Format(WP_SEARCH_SCRIPT, searchWord); break; default: break; } //Stopwatch watch = new Stopwatch(); //watch.Start(); //// start request //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); //webRequest.Credentials = CredentialCache.DefaultCredentials; ////webRequest.Accept = "text/xml"; //HttpWebResponse webResponse = null; //Stream responseStream = null; //try { // webResponse = (HttpWebResponse)webRequest.GetResponse(); // responseStream = webResponse.GetResponseStream(); // using(StreamReader streamReader = new StreamReader(responseStream)) { // while(!streamReader.EndOfStream) { // str.Append(streamReader.ReadLine()); // str.Append("\r\n"); // } // //this.txtResponse.Text = str.ToString(); // } //} //catch(WebException webex) { // str.Append("WebException:\r\n"); // str.Append(webex.ToString()); //} //catch(Exception ex) { // str.Append("Exception:\r\n"); // str.Append(ex.ToString()); //} //finally { // if(responseStream != null) { // responseStream.Close(); // responseStream.Dispose(); // } // if(webResponse != null) { // webResponse.Close(); // } //} ////watch.Stop(); ////TimeSpan span = watch.Elapsed; ////this.lblRequestTime.Text = string.Format( //// "Request time: {0} - {1}ms ({2} Ticks)", //// span.ToString(), //// watch.ElapsedMilliseconds, //// watch.ElapsedTicks ////); //// show result //this.txtResponse.Text = str.ToString(); //if(str.Length > 0) { // //this.txtResponse.Text = str.ToString(); // switch(parseType) { // case WikiParseType.Article: // this.lbResult.Visible = false; // this.txtResponse.Visible = false; // this.pBrowser.Visible = true; // //this.wbResponse.Visible = true; // this.wbResponse.Show(); // if(str.ToString() != Resources.WikipediaText.EmptyExportResult + "\r\n") { // this.btnOpenEditor.Enabled = true; // if(str.ToString().Contains("#REDIRECT")) { // // <text xml:space="preserve">#REDIRECT [[Star Trek (2009)]]</text> // string tmp = str.ToString(); // int start = tmp.IndexOf("<text xml:space=\"preserve\">") + ("<text xml:space=\"preserve\">").Length; // int end = tmp.IndexOf("</text>", start) - start; // tmp = tmp.Substring(start, end); // this.ReOpenDialog(WikipediaParser.ParseRedirect(tmp)); // } // else { // this.txtResponse.Text = str.ToString(); // } // } // else { // this.btnOpenEditor.Enabled = false; // this.Cursor = Cursors.WaitCursor; // this.ParseWikipedia( // searchWord, // WikiOutputType.Html, // WikiParseType.Search // ); // this.Cursor = Cursors.Default; // } // break; // case WikiParseType.Search: // this.btnOpenEditor.Enabled = false; // this.lbResult.Visible = true; // this.txtResponse.Visible = false; // this.pBrowser.Visible = false; // //this.wbResponse.Visible = false; // this.wbResponse.Hide(); // //<!-- Search results fetched via search=[search6], highlight=[search7], interwiki=[search10] in 104 ms --> // //<ul class='mw-search-results'> // string buffer = str.ToString(); // if(buffer.Contains("<ul class='mw-search-results'>")) { // int start = buffer.IndexOf("<ul class='mw-search-results'>") + ( "<ul class='mw-search-results'>" ).Length; // int end = buffer.IndexOf("</div><p class='mw-search-pager-bottom'>") - start; // string output = buffer.Substring(start, end); // //</ul> // //</div><p class='mw-search-pager-bottom'> // string[] items = output.Split(new string[] { "<li>" }, StringSplitOptions.RemoveEmptyEntries); // foreach(string item in items) { // if(item != "\r\n") { // if(item.Contains("<a href=\"/wiki/")) { // int linkStart = item.IndexOf("<a href=\"/wiki/") + ( "<a href=\"/wiki/" ).Length; // int linkEnd = item.IndexOf("\" title=\"") - linkStart; // int titleStart = item.IndexOf("\">", linkEnd) + ( "\">" ).Length; // int titleEnd = item.IndexOf("</a>", linkEnd) - titleStart; // string link = item.Substring(linkStart, linkEnd); // string title = item.Substring(titleStart, titleEnd); // Regex textreplace = new Regex("(<[^>]*>)"); // title = textreplace.Replace(title, ""); // this.lbResult.Items.Add(title + " (Wiki Titel: " + link + ")"); // } // } // } // //<li><a href="/wiki/American_History_X" title="American History X"> // //<span class='searchmatch'>American</span> <span class='searchmatch'>History</span> X</a> // //<div class='searchresult'><span class='searchmatch'>American</span> <span class='searchmatch'>History</span> X ist ein Film aus dem Jahre 1998 . Er beschäftigt sich mit der US-amerikanischen Neonazi -Szene. Tony Kaye führte <b>…</b> // //</div> // //</li> // //this.txtResponse.Text = output; // } // else { // StaticWindows.InfoBox( // "Es konnte kein Eintrag gefunden werden, bitte ändern Sie Ihre Suchanfrage." // ); // } // break; // default: // break; // } //} }
/// <summary> /// Parses the wikipedia. /// </summary> /// <param name="searchWord">The search word.</param> /// <param name="outputType">Type of the output.</param> /// <param name="parseType">Type of the parse.</param> public void ParseWikipedia(string searchWord, WikiOutputType outputType, WikiParseType parseType) { string uri = ""; StringBuilder str = new StringBuilder(); switch(parseType) { case WikiParseType.Article: this.lblTitle.Text = "Wikipedia Artikel"; Uri webUri; Uri downloadUri; if(WebValidator.IsWebAddress(searchWord)) { // http://de.wikipedia.org/wiki/Die_Entf%C3%BChrung_der_U-Bahn_Pelham_123 //searchWord = searchWord.Substring(searchWord.IndexOf("wiki/") + 5); //uri = WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord); if(uri.Contains(WP_EXPORT_SCRIPT_ADD)) { //uri = searchWord; uri = searchWord.Replace("/wiki/" + WP_EXPORT_SCRIPT_ADD, "/wiki/"); } else { //uri = searchWord.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD); uri = searchWord; } if(!searchWord.Contains(WP_EXPORT_SCRIPT_ADD)) { searchWord = searchWord.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD); } webUri = new Uri(uri); downloadUri = new Uri(searchWord);//.Replace("/wiki/", "/wiki/" + WP_EXPORT_SCRIPT_ADD)); } else { searchWord = this.ToCamelCase(searchWord); searchWord = searchWord.Replace(' ', '_'); if(uri.Contains(WP_EXPORT_SCRIPT_ADD)) { uri = WP_URL + searchWord; } else { uri = WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord); } webUri = new Uri(WP_URL + searchWord); downloadUri = new Uri(WP_URL + string.Format(WP_EXPORT_SCRIPT, searchWord)); } this.wbResponse.Stop(); //this.wbResponse = new WebBrowser(); //this.wbResponse.Url = null; this.wbResponse.Url = webUri; this.wbResponse.Navigate(webUri); WebClient wc = new WebClient(); wc.Headers.Add("user-agent", "MovieMatic (compatible; .NET 3.5;)"); string response = wc.DownloadString(downloadUri.AbsoluteUri); this.txtResponse.Text = response; //this.wbResponse.Refresh(WebBrowserRefreshOption.Completely); break; case WikiParseType.Search: this.lblTitle.Text = "Suchergebnis"; searchWord = searchWord.Replace(' ', '+'); uri = WP_URL + string.Format(WP_SEARCH_SCRIPT, searchWord); break; default: break; } //Stopwatch watch = new Stopwatch(); //watch.Start(); //// start request //HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); //webRequest.Credentials = CredentialCache.DefaultCredentials; ////webRequest.Accept = "text/xml"; //HttpWebResponse webResponse = null; //Stream responseStream = null; //try { // webResponse = (HttpWebResponse)webRequest.GetResponse(); // responseStream = webResponse.GetResponseStream(); // using(StreamReader streamReader = new StreamReader(responseStream)) { // while(!streamReader.EndOfStream) { // str.Append(streamReader.ReadLine()); // str.Append("\r\n"); // } // //this.txtResponse.Text = str.ToString(); // } //} //catch(WebException webex) { // str.Append("WebException:\r\n"); // str.Append(webex.ToString()); //} //catch(Exception ex) { // str.Append("Exception:\r\n"); // str.Append(ex.ToString()); //} //finally { // if(responseStream != null) { // responseStream.Close(); // responseStream.Dispose(); // } // if(webResponse != null) { // webResponse.Close(); // } //} ////watch.Stop(); ////TimeSpan span = watch.Elapsed; ////this.lblRequestTime.Text = string.Format( //// "Request time: {0} - {1}ms ({2} Ticks)", //// span.ToString(), //// watch.ElapsedMilliseconds, //// watch.ElapsedTicks ////); //// show result //this.txtResponse.Text = str.ToString(); //if(str.Length > 0) { // //this.txtResponse.Text = str.ToString(); // switch(parseType) { // case WikiParseType.Article: // this.lbResult.Visible = false; // this.txtResponse.Visible = false; // this.pBrowser.Visible = true; // //this.wbResponse.Visible = true; // this.wbResponse.Show(); // if(str.ToString() != Resources.WikipediaText.EmptyExportResult + "\r\n") { // this.btnOpenEditor.Enabled = true; // if(str.ToString().Contains("#REDIRECT")) { // // <text xml:space="preserve">#REDIRECT [[Star Trek (2009)]]</text> // string tmp = str.ToString(); // int start = tmp.IndexOf("<text xml:space=\"preserve\">") + ("<text xml:space=\"preserve\">").Length; // int end = tmp.IndexOf("</text>", start) - start; // tmp = tmp.Substring(start, end); // this.ReOpenDialog(WikipediaParser.ParseRedirect(tmp)); // } // else { // this.txtResponse.Text = str.ToString(); // } // } // else { // this.btnOpenEditor.Enabled = false; // this.Cursor = Cursors.WaitCursor; // this.ParseWikipedia( // searchWord, // WikiOutputType.Html, // WikiParseType.Search // ); // this.Cursor = Cursors.Default; // } // break; // case WikiParseType.Search: // this.btnOpenEditor.Enabled = false; // this.lbResult.Visible = true; // this.txtResponse.Visible = false; // this.pBrowser.Visible = false; // //this.wbResponse.Visible = false; // this.wbResponse.Hide(); // //<!-- Search results fetched via search=[search6], highlight=[search7], interwiki=[search10] in 104 ms --> // //<ul class='mw-search-results'> // string buffer = str.ToString(); // if(buffer.Contains("<ul class='mw-search-results'>")) { // int start = buffer.IndexOf("<ul class='mw-search-results'>") + ( "<ul class='mw-search-results'>" ).Length; // int end = buffer.IndexOf("</div><p class='mw-search-pager-bottom'>") - start; // string output = buffer.Substring(start, end); // //</ul> // //</div><p class='mw-search-pager-bottom'> // string[] items = output.Split(new string[] { "<li>" }, StringSplitOptions.RemoveEmptyEntries); // foreach(string item in items) { // if(item != "\r\n") { // if(item.Contains("<a href=\"/wiki/")) { // int linkStart = item.IndexOf("<a href=\"/wiki/") + ( "<a href=\"/wiki/" ).Length; // int linkEnd = item.IndexOf("\" title=\"") - linkStart; // int titleStart = item.IndexOf("\">", linkEnd) + ( "\">" ).Length; // int titleEnd = item.IndexOf("</a>", linkEnd) - titleStart; // string link = item.Substring(linkStart, linkEnd); // string title = item.Substring(titleStart, titleEnd); // Regex textreplace = new Regex("(<[^>]*>)"); // title = textreplace.Replace(title, ""); // this.lbResult.Items.Add(title + " (Wiki Titel: " + link + ")"); // } // } // } // //<li><a href="/wiki/American_History_X" title="American History X"> // //<span class='searchmatch'>American</span> <span class='searchmatch'>History</span> X</a> // //<div class='searchresult'><span class='searchmatch'>American</span> <span class='searchmatch'>History</span> X ist ein Film aus dem Jahre 1998 . Er beschäftigt sich mit der US-amerikanischen Neonazi -Szene. Tony Kaye führte <b>…</b> // //</div> // //</li> // //this.txtResponse.Text = output; // } // else { // StaticWindows.InfoBox( // "Es konnte kein Eintrag gefunden werden, bitte ändern Sie Ihre Suchanfrage." // ); // } // break; // default: // break; // } //} }